paradocs 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0df2185fac7b1e70254c9531622e135c6609f309
4
- data.tar.gz: 701ec92673e9044bc8a8384e251276aa7863d37e
3
+ metadata.gz: a2b76b24aa936e4626071a9563bb2d96626e584d
4
+ data.tar.gz: a562933c44070c959dd170dd704621014f4b6cd0
5
5
  SHA512:
6
- metadata.gz: aa14905a01be3d1d54589aabc53fbc2f14d3a17352742a9eece1e76b324f146d94f5860ae4f59d738007e809c1fb0eb3ae95579d2a8237798e80a7a6f43ff2b9
7
- data.tar.gz: 0c5fedee64178f630d403db573b2d8a4e2e4bc44b88e65df636c88aa1d9857836267d77ed34a1236036ff97bb105e37252f4948d46fcdc600850e682a5db082d
6
+ metadata.gz: 550d27c26a72f9f5ee9037a8072bcf40687f2554dd2822bd5c920218aa92a4c06fcd2c72b819ac479b99977028e395b4fdd32cfca4091874d1ac184e62968e2a
7
+ data.tar.gz: b28aa591c5c8cce7b7d19d638f9ba5ab530972ee6e83603f83eb35ab5a98f756d1b6e800a0000ebdbe87296c7f964ee261e9d2bbc430720d962433e1d3cd3160
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## 1.1.1
4
+ - Fixed bug with missing `errors` meta key in `Structure#all_nested` and `Structure#all_flatten` methods.
5
+ - Fixed bug with absent `nested_name` meta key in `#Structure#all_nested` method.
6
+ - Added opportunity to sort generated by `PayloadBuilder` payload in the way it is described in schema.
7
+
8
+ ## 1.1.0
9
+ > `Schema#structure` is not comptatible with previous versions
10
+
11
+ - Added `Paradocs::Extensions::StructureBuilder`. See [more](payload_builder)
12
+ - `Parardocs::Extensions::Structure` has replaced `Paradocs::Extensions::Insides` and changed `Schema#structure` behavior
13
+ - `Paradocs::Extensions::Structure` got more structure generation methods. See [Documentation Generation](documentation_generation)
14
+
@@ -188,12 +188,16 @@ all_nested[:subschema] # =>
188
188
  required: true,
189
189
  present: true,
190
190
  json_path: "$.data",
191
+ nested_name: "data",
191
192
  structure: {
192
- "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
193
- "extra" => {type: :array, required: true, json_path: "$.data.extra[]", structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"}}},
194
- "test_field" => {required: true, present: true, json_path: "$.data.test_field"},
195
- "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
196
- "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true}
193
+ "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true, nested_name: "data.role"},
194
+ "test_field" => {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"},
195
+ "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
196
+ "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
197
+ "extra" => {
198
+ type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
199
+ structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}
200
+ }
197
201
  }
198
202
  }
199
203
  }
@@ -205,12 +209,16 @@ all_nested[:test_subschema] # =>
205
209
  required: true,
206
210
  present: true,
207
211
  json_path: "$.data",
212
+ nested_name: "data",
208
213
  structure: {
209
- "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
210
- "extra" => {type: :array, required: true, json_path: "$.data.extra[]", structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"}}},
211
- "test1" => {required: true, present: true, json_path: "$.data.test1"},
212
- "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
213
- "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true}
214
+ "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true, nested_name: "data.role"},
215
+ "test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"},
216
+ "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
217
+ "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
218
+ "extra" => {
219
+ type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
220
+ structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}
221
+ }
214
222
  }
215
223
  }
216
224
  }
@@ -219,33 +227,38 @@ all_nested[:test_subschema] # =>
219
227
  ## Structure#all_flatten
220
228
  > This method returns all available combinations of schema (built on subschema) without nesting (the same way as Structure#flatten method does)
221
229
 
222
- Schema is the same as described in Structure#all_nested
230
+ Schema is the same as described in `Structure#all_nested`
223
231
  ```rb
224
232
  schema.structure.all_flatten # =>
225
233
  {
226
234
  subschema: {
227
235
  _errors: [],
228
- "data" => {type: :object, required: true, present: true, json_path: "$.data"},
229
- "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
230
- "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true},
231
- "data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
232
- "data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
233
- "data.extra.extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"},
234
- "data.test_field" => {required: true, present: true, json_path: "$.data.test_field"}
236
+ "data" => {type: :object, required: true, present: true, json_path: "$.data", nested_name: "data"},
237
+ "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
238
+ "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
239
+ "data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true, nested_name: "data.role"},
240
+ "data.extra" => {type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra"},
241
+ "data.extra.extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"},
242
+ "data.test_field" => {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"}
235
243
  },
236
244
  test_subschema: {
237
245
  _errors: [],
238
- "data" => {type: :object, required: true, present: true, json_path: "$.data"},
239
- "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
240
- "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true},
241
- "data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
242
- "data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
243
- "data.extra.extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"},
244
- "data.test1" => {required: true, present: true, json_path: "$.data.test1"}
246
+ "data" => {type: :object, required: true, present: true, json_path: "$.data", nested_name: "data"},
247
+ "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
248
+ "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
249
+ "data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true, nested_name: "data.role"},
250
+ "data.extra" => {type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra"},
251
+ "data.extra.extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"},
252
+ "data.test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"}
245
253
  }
246
254
  }
247
255
  ```
248
256
 
257
+ ## Passing a block
258
+ Given block to the methods above (`#flatten`, `#nested`, `#all_flatten`, `#all_nested`) will be executed for
259
+ each field, passing you as arguments `field.key` and `field.meta`. Mutating the second argument `field.meta`
260
+ will reflect onto returned `meta`.
261
+
249
262
  ## Schema#walk
250
263
 
251
264
  The `#walk` method can recursively walk a schema definition and extract meta data or field attributes.
@@ -1,9 +1,8 @@
1
1
  # Generate examples from the Schema
2
2
 
3
- > Schema instance provides #example_payloads method that returns example of all possible structures.
3
+ > `Schema` instance provides `#example_payloads` method that returns example of all possible structures.
4
4
 
5
- NOTE: PayloadBuilder sets nil values by default. If options are given - builder will take on of them, if default is set - builder will use it.
6
- > PayloadBuilder#build! method takes a block as argument that may help you adding your custom rules.
5
+ NOTE: `PayloadBuilder` sets nil values by default. If options are given - builder will take on of them, if default is set - builder will use it.
7
6
 
8
7
  #### Example schema
9
8
  ```ruby
@@ -59,12 +58,14 @@ schema.example_payloads.to_json # =>
59
58
  ```
60
59
 
61
60
  ## Customize payload generation logic
62
- PayloadBuilder#build! allows passing a block that will receive the following arguments:
61
+ `PayloadBuilder#build!` arguments:
63
62
 
64
- - key: Field name
65
- - meta: Field meta data (that includes (if provided) field types, presence data, policies and other meta data
66
- - example_value: Provided by generator example value.
67
- - skip_word: Return this argument back if you want this item to be ommitted.
63
+ 1. `sort_by_schema: true` will try to return payload in the same way as declared in the schema.
64
+ 2. `&block` will be executed for each key receiving the following arguments:
65
+ - `key`: Field name
66
+ - `meta`: Field meta data (that includes (if provided) field types, presence data, policies and other meta data
67
+ - `example_value`: Provided by generator example value.
68
+ - `skip_word`: Return this argument back if you want this item to be ommitted.
68
69
 
69
70
  ```rb
70
71
  block = Proc.new do |key, meta, example, skip_word|
@@ -8,8 +8,9 @@ module Paradocs
8
8
  @skip_word = skip_word
9
9
  end
10
10
 
11
- def build!(&block)
12
- structure.all_nested.map { |name, struct| [name, build_simple_structure(struct, &block)] }.to_h
11
+ def build!(sort_by_schema: false, &block)
12
+ result = structure.all_nested.map { |name, struct| [name, build_simple_structure(struct, &block)] }.to_h
13
+ sort_by_schema ? schema.resolve(result).output : result
13
14
  end
14
15
 
15
16
  private
@@ -45,6 +45,7 @@ module Paradocs
45
45
  struct.sort_by { |k, v| k.to_s.count(".") }.each do |key, value|
46
46
  target = obj[name]
47
47
  key, value = key.to_s, value.clone # clone the values, because we do mutation below
48
+ value.merge!(nested_name: key) if value.respond_to?(:merge) # it can be array (_errors)
48
49
  next target[key.to_sym] = value if key.start_with?(Paradocs.config.meta_prefix) # copy meta fields
49
50
 
50
51
  parts = key.split(".")
@@ -68,9 +69,11 @@ module Paradocs
68
69
  end
69
70
  @all_flatten = schema_structure[subschemes].each_with_object({}) do |(name, subschema), result|
70
71
  if subschema[subschemes].empty?
71
- result[name] = schema_structure.merge(subschema)
72
- result[name].delete(subschemes)
73
- next result[name]
72
+ result[name] = schema_structure.merge(subschema)
73
+ result[name][errors] += schema_structure[errors]
74
+ result[name][errors].uniq!
75
+ result[name].delete(subschemes)
76
+ next result[name]
74
77
  end
75
78
 
76
79
  all_flatten(subschema).each do |sub_name, schema|
@@ -1,3 +1,3 @@
1
1
  module Paradocs
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
data/mkdocs.yml CHANGED
@@ -13,4 +13,5 @@ nav:
13
13
  - 'payload_builder.md'
14
14
  - 'custom_configuration.md'
15
15
  - 'faq.md'
16
+ - 'changelog.md'
16
17
 
@@ -174,7 +174,7 @@ describe Paradocs::Extensions::Structure do
174
174
  it "generates N structures, where N = number of unique combinations of applied subschemas" do
175
175
  expect(schema.structure.all_flatten).to eq({
176
176
  subschema: {
177
- _errors: [],
177
+ _errors: [ArgumentError],
178
178
  "data" => {type: :object, required: true, present: true, json_path: "$.data"},
179
179
  "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
180
180
  "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true},
@@ -184,7 +184,7 @@ describe Paradocs::Extensions::Structure do
184
184
  "data.test_field" => {required: true, present: true, json_path: "$.data.test_field"}
185
185
  },
186
186
  test_subschema: {
187
- _errors: [],
187
+ _errors: [ArgumentError],
188
188
  "data" => {type: :object, required: true, present: true, json_path: "$.data"},
189
189
  "data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
190
190
  "data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true},
@@ -201,34 +201,41 @@ describe Paradocs::Extensions::Structure do
201
201
  it "generates N structures, where N = number of unique combinations of applied subschemas" do
202
202
  result = schema.structure.all_nested
203
203
  expect(result[:subschema]).to eq({
204
- _errors: [],
204
+ _errors: [ArgumentError],
205
205
  "data" => {
206
- type: :object,
207
- required: true,
208
- present: true,
209
- json_path: "$.data",
206
+ type: :object,
207
+ required: true,
208
+ present: true,
209
+ json_path: "$.data",
210
+ nested_name: "data",
210
211
  structure: {
211
- "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
212
- "extra" => {type: :array, required: true, json_path: "$.data.extra[]", structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"}}},
213
- "test_field" => {required: true, present: true, json_path: "$.data.test_field"},
214
- "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
215
- "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true}
212
+ "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
213
+ "test_field" => {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"},
214
+ "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
215
+ "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
216
+ "extra" => {
217
+ type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
218
+ structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}
219
+ }
216
220
  }
217
221
  }
218
222
  })
219
223
  expect(result[:test_subschema]).to eq({
220
- _errors: [],
224
+ _errors: [ArgumentError],
221
225
  "data" => {
222
226
  type: :object,
223
227
  required: true,
224
228
  present: true,
225
229
  json_path: "$.data",
230
+ nested_name: "data",
226
231
  structure: {
227
- "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
228
- "extra" => {type: :array, required: true, json_path: "$.data.extra[]", structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra"}}},
229
- "test1" => {required: true, present: true, json_path: "$.data.test1"},
230
- "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
231
- "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true}
232
+ "role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
233
+ "test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"},
234
+ "id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
235
+ "name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name"},
236
+ "extra" => {
237
+ type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
238
+ structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}}
232
239
  }
233
240
  }
234
241
  })
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.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Tkachenko
@@ -88,6 +88,7 @@ files:
88
88
  - README.md
89
89
  - Rakefile
90
90
  - bin/console
91
+ - docs/changelog.md
91
92
  - docs/custom_configuration.md
92
93
  - docs/documentation_generation.md
93
94
  - docs/faq.md