paradocs 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paradocs/extensions/payload_builder.rb +2 -1
- data/lib/paradocs/extensions/structure.rb +6 -3
- data/lib/paradocs/field_dsl.rb +4 -0
- data/lib/paradocs/schema.rb +2 -1
- data/lib/paradocs/struct.rb +1 -0
- data/lib/paradocs/version.rb +1 -1
- data/paradocs.gemspec +1 -1
- data/spec/extensions/payload_builder_spec.rb +3 -3
- data/spec/extensions/structures_spec.rb +16 -14
- data/spec/schema_spec.rb +6 -6
- data/spec/struct_spec.rb +8 -8
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9ed12715c0df054876f622c3dada9cc4a2d8fd5
|
4
|
+
data.tar.gz: bfe8c1cb2eb4aa9f15eb08349425611deccb3517
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9d827bf00b03d7766600e438d8ee16a9938ab099cd807d0d236cfb37758d104d76f26fdf713c7f39c550ce65df56c30b4dcfbeedb5fa7c5ebc93fa67c66199d
|
7
|
+
data.tar.gz: c4ac5998e89948246c01b6ec04d39ce5332c66da32902ac2b63a06672746dc002a91dc013372bc4cb33410a72e15c1de85bfb08aab8a17659dc9da84f4c793b5
|
@@ -21,7 +21,8 @@ module Paradocs
|
|
21
21
|
next if key.start_with?(Paradocs.config.meta_prefix) # skip all the meta fields
|
22
22
|
ex_value = restore_one(key, value, &block)
|
23
23
|
next if ex_value == @skip_word
|
24
|
-
|
24
|
+
key = value[:alias] || key
|
25
|
+
[key.to_s, ex_value]
|
25
26
|
end.compact.to_h
|
26
27
|
end
|
27
28
|
|
@@ -23,8 +23,10 @@ module Paradocs
|
|
23
23
|
else
|
24
24
|
result[errors] += field.possible_errors
|
25
25
|
end
|
26
|
-
|
27
|
-
|
26
|
+
|
27
|
+
field_key = field.meta_data[:alias] || field.key
|
28
|
+
result[field_key] = meta unless ignore_transparent && field.transparent?
|
29
|
+
yield(field_key, meta) if block_given?
|
28
30
|
|
29
31
|
next unless field.mutates_schema?
|
30
32
|
schema.subschemes.each do |name, subschema|
|
@@ -103,7 +105,8 @@ module Paradocs
|
|
103
105
|
private
|
104
106
|
|
105
107
|
def collect_meta(field, root)
|
106
|
-
|
108
|
+
field_key = field.meta_data[:alias] || field.key
|
109
|
+
json_path = root.empty? ? "$.#{field_key}" : "#{root}.#{field_key}"
|
107
110
|
meta = field.meta_data.merge(json_path: json_path)
|
108
111
|
sc = meta.delete(:schema)
|
109
112
|
meta[:mutates_schema] = true if meta.delete(:mutates_schema)
|
data/lib/paradocs/field_dsl.rb
CHANGED
data/lib/paradocs/schema.rb
CHANGED
@@ -181,7 +181,8 @@ module Paradocs
|
|
181
181
|
invoke_subschemes!(val, context, flds: flds)
|
182
182
|
flds.each_with_object({}) do |(_, field), m|
|
183
183
|
r = field.resolve(val, context.sub(field.key))
|
184
|
-
|
184
|
+
key = field.meta_data[:alias] || field.key
|
185
|
+
m[key] = r.value if r.eligible?
|
185
186
|
end
|
186
187
|
end
|
187
188
|
|
data/lib/paradocs/struct.rb
CHANGED
data/lib/paradocs/version.rb
CHANGED
data/paradocs.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Paradocs::VERSION
|
9
9
|
spec.authors = ["Maxim Tkachenko", "Ismael Celis"]
|
10
10
|
spec.email = ["tkachenko.maxim.w@gmail.com", "ismaelct@gmail.com"]
|
11
|
-
spec.description = %q{Flexible
|
11
|
+
spec.description = %q{Flexible DRY validations with API docs generation done right TLDR; parametrics on steroids.}
|
12
12
|
spec.summary = %q{A huge add-on for original gem mostly focused on retrieving the more metadata from declared schemas as possible.}
|
13
13
|
spec.homepage = "https://paradocs.readthedocs.io/en/latest"
|
14
14
|
spec.license = "MIT"
|
@@ -22,7 +22,7 @@ describe Paradocs::Extensions::PayloadBuilder do
|
|
22
22
|
end
|
23
23
|
subschema(:fooschema) { }
|
24
24
|
subschema(:barschema) do
|
25
|
-
field(:barfield).present.type(:boolean)
|
25
|
+
field(:barfield).present.type(:boolean).as(:bar_field)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -32,7 +32,7 @@ describe Paradocs::Extensions::PayloadBuilder do
|
|
32
32
|
allow_any_instance_of(Array).to receive(:sample) { "bar" }
|
33
33
|
payloads = described_class.new(schema).build!
|
34
34
|
expect(payloads.keys.sort).to eq([:barschema, :fooschema, :subschema1, :subschema2_deep_schema, :subschema2_empty])
|
35
|
-
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => "bar", "
|
35
|
+
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => "bar", "bar_field" => nil}})
|
36
36
|
expect(payloads[:fooschema]).to eq({"test" => nil, "foo" => {"bar" => "bar"}})
|
37
37
|
expect(payloads[:subschema1]).to eq({"test" => nil, "foo" => {"bar" => "bar"}, "subtest1" => nil})
|
38
38
|
expect(payloads[:subschema2_deep_schema]).to eq({
|
@@ -57,7 +57,7 @@ describe Paradocs::Extensions::PayloadBuilder do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
expect(payloads.keys.sort).to eq([:barschema, :fooschema, :subschema1, :subschema2_deep_schema, :subschema2_empty])
|
60
|
-
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => nil, "
|
60
|
+
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => nil, "bar_field" => true}}) # barfield is change to true and bar is nil
|
61
61
|
expect(payloads[:fooschema]).to eq({"test" => nil, "foo" => {"bar" => nil}}) # bar is nil
|
62
62
|
expect(payloads[:subschema1]).to eq({"test" => nil, "foo" => {"bar" => nil}}) # subtest is missing, bar is nil
|
63
63
|
expect(payloads[:subschema2_deep_schema]).to eq({
|
@@ -18,7 +18,7 @@ describe Paradocs::Extensions::Structure do
|
|
18
18
|
subschema(:highest_level) { field(:test).present } # no mutations on this level -> subschema ignored
|
19
19
|
|
20
20
|
field(:data).type(:object).present.schema do
|
21
|
-
field(:id).type(:integer).present.policy(:policy_with_error)
|
21
|
+
field(:id).type(:integer).present.policy(:policy_with_error).as(:user_id)
|
22
22
|
field(:name).type(:string).meta(label: "very important staff").description("Example description").example("John")
|
23
23
|
field(:role).type(:string).declared.options(["admin", "user"]).default("user").mutates_schema! do |*|
|
24
24
|
:test_subschema
|
@@ -64,13 +64,14 @@ describe Paradocs::Extensions::Structure do
|
|
64
64
|
test_field: {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"}
|
65
65
|
}
|
66
66
|
})
|
67
|
-
expect(data_structure[:
|
67
|
+
expect(data_structure[:user_id]).to eq({
|
68
68
|
type: :integer,
|
69
69
|
required: true,
|
70
70
|
present: true,
|
71
71
|
policy_with_error: {errors: [ArgumentError]},
|
72
|
-
|
73
|
-
|
72
|
+
alias: :user_id,
|
73
|
+
json_path: "$.data.user_id",
|
74
|
+
nested_name: "data.user_id"
|
74
75
|
})
|
75
76
|
expect(data_structure[:name]).to eq({
|
76
77
|
type: :string,
|
@@ -131,11 +132,12 @@ describe Paradocs::Extensions::Structure do
|
|
131
132
|
json_path: "$.data.extra[].extra",
|
132
133
|
policy_with_silent_error: {errors: []}
|
133
134
|
},
|
134
|
-
"data.
|
135
|
+
"data.user_id" => {
|
135
136
|
type: :integer,
|
136
137
|
required: true,
|
137
138
|
present: true,
|
138
|
-
|
139
|
+
alias: :user_id,
|
140
|
+
json_path: "$.data.user_id",
|
139
141
|
policy_with_error: {errors: [ArgumentError]}
|
140
142
|
},
|
141
143
|
"data.name" => {
|
@@ -180,7 +182,7 @@ describe Paradocs::Extensions::Structure do
|
|
180
182
|
subschema: {
|
181
183
|
_errors: [ArgumentError],
|
182
184
|
"data" => {type: :object, required: true, present: true, json_path: "$.data"},
|
183
|
-
"data.
|
185
|
+
"data.user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id"},
|
184
186
|
"data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, description: "Example description", example: "John"},
|
185
187
|
"data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
|
186
188
|
"data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
|
@@ -190,7 +192,7 @@ describe Paradocs::Extensions::Structure do
|
|
190
192
|
test_subschema: {
|
191
193
|
_errors: [ArgumentError],
|
192
194
|
"data" => {type: :object, required: true, present: true, json_path: "$.data"},
|
193
|
-
"data.
|
195
|
+
"data.user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id"},
|
194
196
|
"data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, description: "Example description", example: "John"},
|
195
197
|
"data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
|
196
198
|
"data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
|
@@ -215,7 +217,7 @@ describe Paradocs::Extensions::Structure do
|
|
215
217
|
structure: {
|
216
218
|
"role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
|
217
219
|
"test_field" => {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"},
|
218
|
-
"
|
220
|
+
"user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id", nested_name: "data.user_id"},
|
219
221
|
"name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name", description: "Example description", example: "John"},
|
220
222
|
"extra" => {
|
221
223
|
type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
|
@@ -233,11 +235,11 @@ describe Paradocs::Extensions::Structure do
|
|
233
235
|
json_path: "$.data",
|
234
236
|
nested_name: "data",
|
235
237
|
structure: {
|
236
|
-
"role"
|
237
|
-
"test1"
|
238
|
-
"
|
239
|
-
"name"
|
240
|
-
"extra"
|
238
|
+
"role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
|
239
|
+
"test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"},
|
240
|
+
"user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id", nested_name: "data.user_id"},
|
241
|
+
"name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name", description: "Example description", example: "John"},
|
242
|
+
"extra" => {
|
241
243
|
type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
|
242
244
|
structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}}
|
243
245
|
}
|
data/spec/schema_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe Paradocs::Schema do
|
|
16
16
|
|
17
17
|
subject do
|
18
18
|
described_class.new do
|
19
|
-
field(:title).policy(:string).present
|
19
|
+
field(:title).policy(:string).present.as(:article_title)
|
20
20
|
field(:price).policy(:integer).meta(label: "A price")
|
21
21
|
field(:status).policy(:string).options(['visible', 'hidden'])
|
22
22
|
field(:tags).policy(:split).policy(:array)
|
@@ -33,8 +33,8 @@ describe Paradocs::Schema do
|
|
33
33
|
describe "#structure" do
|
34
34
|
it "represents data structure and meta data" do
|
35
35
|
sc = subject.structure.nested
|
36
|
-
expect(sc[:
|
37
|
-
expect(sc[:
|
36
|
+
expect(sc[:article_title][:present]).to be true
|
37
|
+
expect(sc[:article_title][:type]).to eq :string
|
38
38
|
expect(sc[:price][:type]).to eq :integer
|
39
39
|
expect(sc[:price][:label]).to eq "A price"
|
40
40
|
expect(sc[:variants][:type]).to eq :array
|
@@ -76,7 +76,7 @@ describe Paradocs::Schema do
|
|
76
76
|
|
77
77
|
output = subject.resolve(payload).output
|
78
78
|
expect(output).to eq({
|
79
|
-
|
79
|
+
article_title: "title",
|
80
80
|
price: 100,
|
81
81
|
status: "visible",
|
82
82
|
tags: ["tag"],
|
@@ -101,7 +101,7 @@ describe Paradocs::Schema do
|
|
101
101
|
variants: [{name: 'v1', sku: 'ABC', stock: '10', available_if_no_stock: true}]
|
102
102
|
},
|
103
103
|
{
|
104
|
-
|
104
|
+
article_title: 'iPhone 6 Plus',
|
105
105
|
price: 100,
|
106
106
|
status: 'visible',
|
107
107
|
tags: ['tag1', 'tag2'],
|
@@ -114,7 +114,7 @@ describe Paradocs::Schema do
|
|
114
114
|
variants: [{name: 'v1', available_if_no_stock: '1'}]
|
115
115
|
},
|
116
116
|
{
|
117
|
-
|
117
|
+
article_title: 'iPhone 6 Plus',
|
118
118
|
variants: [{name: 'v1', stock: 1, available_if_no_stock: true}]
|
119
119
|
})
|
120
120
|
|
data/spec/struct_spec.rb
CHANGED
@@ -16,13 +16,13 @@ describe Paradocs::Struct do
|
|
16
16
|
include Paradocs::Struct
|
17
17
|
|
18
18
|
schema do
|
19
|
-
field(:title).type(:string).present
|
19
|
+
field(:title).type(:string).present.as(:example_title)
|
20
20
|
field(:friends).type(:array).default([]).schema friend_class
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
new_instance = klass.new
|
25
|
-
expect(new_instance.
|
25
|
+
expect(new_instance.example_title).to eq ''
|
26
26
|
expect(new_instance.friends).to eq []
|
27
27
|
expect(new_instance.valid?).to be false
|
28
28
|
expect(new_instance.errors['$.title']).not_to be_nil
|
@@ -35,7 +35,7 @@ describe Paradocs::Struct do
|
|
35
35
|
]
|
36
36
|
})
|
37
37
|
|
38
|
-
expect(instance.
|
38
|
+
expect(instance.example_title).to eq 'foo'
|
39
39
|
expect(instance.friends.size).to eq 2
|
40
40
|
expect(instance.friends.first.name).to eq 'Ismael'
|
41
41
|
expect(instance.friends.first).to be_a friend_class
|
@@ -154,7 +154,7 @@ describe Paradocs::Struct do
|
|
154
154
|
schema do
|
155
155
|
field(:title).type(:string).present
|
156
156
|
field(:friends).type(:array).schema do
|
157
|
-
field(:name).type(:string)
|
157
|
+
field(:name).type(:string).as(:person_name)
|
158
158
|
field(:age).type(:integer).default(20)
|
159
159
|
end
|
160
160
|
end
|
@@ -171,8 +171,8 @@ describe Paradocs::Struct do
|
|
171
171
|
expect(instance.to_h).to eq({
|
172
172
|
title: 'foo',
|
173
173
|
friends: [
|
174
|
-
{
|
175
|
-
{
|
174
|
+
{person_name: 'Jane', age: 20},
|
175
|
+
{person_name: 'Joe', age: 39},
|
176
176
|
]
|
177
177
|
})
|
178
178
|
|
@@ -190,7 +190,7 @@ describe Paradocs::Struct do
|
|
190
190
|
include Paradocs::Struct
|
191
191
|
|
192
192
|
schema do
|
193
|
-
field(:title).type(:string).present
|
193
|
+
field(:title).type(:string).present.as(:example_title)
|
194
194
|
field(:friends).type(:array).schema do
|
195
195
|
field(:name).type(:string)
|
196
196
|
field(:age).type(:integer).default(20)
|
@@ -213,7 +213,7 @@ describe Paradocs::Struct do
|
|
213
213
|
]
|
214
214
|
)
|
215
215
|
|
216
|
-
expect(instance.
|
216
|
+
expect(instance.example_title).to eq 'foo'
|
217
217
|
expect(instance.email).to eq 'email@me.com'
|
218
218
|
expect(instance.friends.size).to eq 2
|
219
219
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paradocs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxim Tkachenko
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-11-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -67,8 +67,8 @@ dependencies:
|
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
|
-
description: Flexible
|
71
|
-
|
70
|
+
description: Flexible DRY validations with API docs generation done right TLDR; parametrics
|
71
|
+
on steroids.
|
72
72
|
email:
|
73
73
|
- tkachenko.maxim.w@gmail.com
|
74
74
|
- ismaelct@gmail.com
|