schemacop 3.0.0.rc0 → 3.0.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.releaser_config +0 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +34 -1
- data/README.md +13 -3
- data/README_V3.md +717 -147
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/schemacop.rb +1 -0
- data/lib/schemacop/schema2.rb +2 -2
- data/lib/schemacop/scoped_env.rb +1 -1
- data/lib/schemacop/v2.rb +0 -1
- data/lib/schemacop/v2/caster.rb +1 -0
- data/lib/schemacop/v2/node_supporting_field.rb +25 -11
- data/lib/schemacop/v2/node_supporting_type.rb +2 -2
- data/lib/schemacop/v3/array_node.rb +1 -2
- data/lib/schemacop/v3/hash_node.rb +77 -27
- data/lib/schemacop/v3/node.rb +11 -6
- data/lib/schemacop/v3/node_registry.rb +0 -4
- data/lib/schemacop/v3/reference_node.rb +7 -1
- data/lib/schemacop/v3/string_node.rb +15 -7
- data/schemacop.gemspec +7 -4
- data/test/lib/test_helper.rb +17 -2
- data/test/unit/schemacop/v2/casting_test.rb +37 -0
- data/test/unit/schemacop/v2/validator_hash_test.rb +11 -0
- data/test/unit/schemacop/v3/all_of_node_test.rb +1 -2
- data/test/unit/schemacop/v3/any_of_node_test.rb +6 -6
- data/test/unit/schemacop/v3/array_node_test.rb +13 -3
- data/test/unit/schemacop/v3/global_context_test.rb +2 -0
- data/test/unit/schemacop/v3/hash_node_test.rb +211 -14
- data/test/unit/schemacop/v3/node_test.rb +14 -0
- data/test/unit/schemacop/v3/one_of_node_test.rb +6 -6
- data/test/unit/schemacop/v3/reference_node_test.rb +76 -60
- data/test/unit/schemacop/v3/string_node_test.rb +38 -0
- metadata +16 -3
- data/lib/schemacop/v2/root_node.rb +0 -6
@@ -143,6 +143,20 @@ module Schemacop
|
|
143
143
|
error '/[0]', 'Value must be given.'
|
144
144
|
end
|
145
145
|
end
|
146
|
+
|
147
|
+
def test_not_support_block
|
148
|
+
assert_raises_with_message Schemacop::Exceptions::InvalidSchemaError, 'Node Schemacop::V3::IntegerNode does not support blocks.' do
|
149
|
+
schema :integer do
|
150
|
+
int :foo
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_node_no_children
|
156
|
+
@schema = Schemacop::Schema3.new
|
157
|
+
|
158
|
+
assert_equal(@schema.root.children, [])
|
159
|
+
end
|
146
160
|
end
|
147
161
|
end
|
148
162
|
end
|
@@ -102,11 +102,11 @@ module Schemacop
|
|
102
102
|
|
103
103
|
assert_cast(
|
104
104
|
{ created_at: '2020-01-01' },
|
105
|
-
created_at: Date.new(2020, 1, 1)
|
105
|
+
{ created_at: Date.new(2020, 1, 1) }.with_indifferent_access
|
106
106
|
)
|
107
107
|
assert_cast(
|
108
108
|
{ created_at: '2020-01-01T17:38:20' },
|
109
|
-
created_at: DateTime.new(2020, 1, 1, 17, 38, 20)
|
109
|
+
{ created_at: DateTime.new(2020, 1, 1, 17, 38, 20) }.with_indifferent_access
|
110
110
|
)
|
111
111
|
end
|
112
112
|
|
@@ -127,12 +127,12 @@ module Schemacop
|
|
127
127
|
|
128
128
|
assert_cast(
|
129
129
|
{ foo: { bar: nil } },
|
130
|
-
foo: { bar: nil }
|
130
|
+
{ foo: { bar: nil } }.with_indifferent_access
|
131
131
|
)
|
132
132
|
|
133
133
|
assert_cast(
|
134
134
|
{ foo: { baz: nil } },
|
135
|
-
foo: { baz: 'Baz' }
|
135
|
+
{ foo: { baz: 'Baz' } }.with_indifferent_access
|
136
136
|
)
|
137
137
|
|
138
138
|
schema do
|
@@ -144,12 +144,12 @@ module Schemacop
|
|
144
144
|
|
145
145
|
assert_cast(
|
146
146
|
{ foo: { bar: '1990-01-13' } },
|
147
|
-
foo: { bar: Date.new(1990, 1, 13) }
|
147
|
+
{ foo: { bar: Date.new(1990, 1, 13) } }.with_indifferent_access
|
148
148
|
)
|
149
149
|
|
150
150
|
assert_cast(
|
151
151
|
{ foo: { bar: '1990-01-13T10:00:00Z' } },
|
152
|
-
foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) }
|
152
|
+
{ foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) } }.with_indifferent_access
|
153
153
|
)
|
154
154
|
end
|
155
155
|
|
@@ -249,6 +249,8 @@ module Schemacop
|
|
249
249
|
ref? :node, :Node
|
250
250
|
end
|
251
251
|
|
252
|
+
assert_equal(@schema.root.used_external_schemas, [])
|
253
|
+
|
252
254
|
assert_validation({})
|
253
255
|
assert_validation(node: { name: '1', children: [{ name: '1' }, { name: '2' }] })
|
254
256
|
assert_validation(
|
@@ -286,66 +288,80 @@ module Schemacop
|
|
286
288
|
end
|
287
289
|
end
|
288
290
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
291
|
+
def test_external_schemas
|
292
|
+
context = Context.new
|
293
|
+
|
294
|
+
context.schema :Person do
|
295
|
+
str! :first_name
|
296
|
+
str! :last_name
|
297
|
+
ref? :info, :PersonInfo
|
298
|
+
end
|
299
|
+
|
300
|
+
context.schema :PersonInfo do
|
301
|
+
str! :born_at, format: :date
|
302
|
+
end
|
303
|
+
|
304
|
+
schema :reference, path: :Person
|
305
|
+
|
306
|
+
with_context context do
|
307
|
+
assert_validation(first_name: 'John', last_name: 'Doe')
|
308
|
+
assert_validation(first_name: 'John', last_name: 42) do
|
309
|
+
error '/last_name', 'Invalid type, expected "string".'
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
with_context context do
|
314
|
+
schema do
|
315
|
+
ref! :person, :Person
|
316
|
+
end
|
317
|
+
|
318
|
+
assert_equal(@schema.root.used_external_schemas, %i[Person PersonInfo])
|
319
|
+
|
320
|
+
assert_validation(person: { first_name: 'John', last_name: 'Doe' })
|
321
|
+
assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: '1990-01-13' } })
|
322
|
+
assert_validation(person: { first_name_x: 'John', last_name: 'Doe' }) do
|
323
|
+
error '/person', 'Obsolete property "first_name_x".'
|
324
|
+
error '/person/first_name', 'Value must be given.'
|
325
|
+
end
|
326
|
+
assert_validation(person: { first_name: 'John', last_name: 'Doe', info: { born_at: 'never' } }) do
|
327
|
+
error '/person/info/born_at', 'String does not match format "date".'
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
with_context context do
|
332
|
+
schema do
|
333
|
+
scm :PersonNode do
|
334
|
+
ref! :person, :Person
|
335
|
+
end
|
336
|
+
|
337
|
+
ref! :personNode, :PersonNode
|
338
|
+
end
|
339
|
+
|
340
|
+
assert_equal(@schema.root.used_external_schemas, %i[Person PersonInfo])
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_defaults
|
345
|
+
schema do
|
346
|
+
scm :Person do
|
347
|
+
str? :foo, default: 'bar'
|
348
|
+
end
|
349
|
+
ref? :person, :Person, default: {}
|
350
|
+
end
|
351
|
+
|
352
|
+
assert_cast({}, { person: { foo: 'bar' } }.with_indifferent_access)
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_casting
|
356
|
+
schema do
|
357
|
+
scm :Person do
|
358
|
+
str! :born_at, format: :date
|
359
|
+
end
|
360
|
+
ref? :person, :Person, default: {}
|
361
|
+
end
|
362
|
+
|
363
|
+
assert_cast({ person: { born_at: '1990-01-13' } }, { person: { born_at: Date.new(1990, 1, 13) } }.with_indifferent_access)
|
364
|
+
end
|
349
365
|
end
|
350
366
|
end
|
351
367
|
end
|
@@ -111,6 +111,9 @@ module Schemacop
|
|
111
111
|
assert_validation 'foo 2020-01-29 bar' do
|
112
112
|
error '/', 'String does not match format "date".'
|
113
113
|
end
|
114
|
+
|
115
|
+
assert_cast(nil, nil)
|
116
|
+
assert_cast('2020-01-13', Date.new(2020, 1, 13))
|
114
117
|
end
|
115
118
|
|
116
119
|
def test_format_date_time
|
@@ -132,6 +135,9 @@ module Schemacop
|
|
132
135
|
assert_validation '2018-11-13T20:20:39Y' do
|
133
136
|
error '/', 'String does not match format "date-time".'
|
134
137
|
end
|
138
|
+
|
139
|
+
assert_cast(nil, nil)
|
140
|
+
assert_cast('2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39))
|
135
141
|
end
|
136
142
|
|
137
143
|
def test_format_email
|
@@ -154,6 +160,9 @@ module Schemacop
|
|
154
160
|
assert_validation '@john@example.com' do
|
155
161
|
error '/', 'String does not match format "email".'
|
156
162
|
end
|
163
|
+
|
164
|
+
assert_cast(nil, nil)
|
165
|
+
assert_cast('john.doe@example.com', 'john.doe@example.com')
|
157
166
|
end
|
158
167
|
|
159
168
|
def test_enum
|
@@ -187,18 +196,24 @@ module Schemacop
|
|
187
196
|
|
188
197
|
assert_cast 'true', true
|
189
198
|
assert_cast 'false', false
|
199
|
+
|
200
|
+
assert_cast nil, nil
|
190
201
|
end
|
191
202
|
|
192
203
|
def test_time_casting
|
193
204
|
schema :string, format: :time
|
194
205
|
assert_json(type: :string, format: :time)
|
195
206
|
assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
|
207
|
+
|
208
|
+
assert_cast nil, nil
|
196
209
|
end
|
197
210
|
|
198
211
|
def test_date_casting
|
199
212
|
schema :string, format: :date
|
200
213
|
assert_json(type: :string, format: :date)
|
201
214
|
assert_cast '2018-11-13', Date.new(2018, 11, 13)
|
215
|
+
|
216
|
+
assert_cast nil, nil
|
202
217
|
end
|
203
218
|
|
204
219
|
def test_date_time_casting
|
@@ -207,12 +222,16 @@ module Schemacop
|
|
207
222
|
assert_cast '2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39)
|
208
223
|
assert_cast '2018-11-13T20:20:39Z', DateTime.new(2018, 11, 13, 20, 20, 39)
|
209
224
|
assert_cast '2018-11-13T20:20:39+01:00', DateTime.new(2018, 11, 13, 20, 20, 39, '+1')
|
225
|
+
|
226
|
+
assert_cast nil, nil
|
210
227
|
end
|
211
228
|
|
212
229
|
def test_email_casting
|
213
230
|
schema :string, format: :email
|
214
231
|
assert_json(type: :string, format: :email)
|
215
232
|
assert_cast 'support@example.com', 'support@example.com'
|
233
|
+
|
234
|
+
assert_cast nil, nil
|
216
235
|
end
|
217
236
|
|
218
237
|
def test_default
|
@@ -233,6 +252,25 @@ module Schemacop
|
|
233
252
|
assert_cast(nil, 'Hello')
|
234
253
|
end
|
235
254
|
|
255
|
+
def test_default_casting
|
256
|
+
schema :string, format: :integer, default: '42'
|
257
|
+
|
258
|
+
assert_json(
|
259
|
+
type: :string,
|
260
|
+
format: :integer,
|
261
|
+
default: '42'
|
262
|
+
)
|
263
|
+
|
264
|
+
assert_validation(nil)
|
265
|
+
assert_validation('123')
|
266
|
+
assert_validation(5) do
|
267
|
+
error '/', 'Invalid type, expected "string".'
|
268
|
+
end
|
269
|
+
|
270
|
+
assert_cast('123', 123)
|
271
|
+
assert_cast(nil, 42)
|
272
|
+
end
|
273
|
+
|
236
274
|
# Helper function that checks for all the options if the option is
|
237
275
|
# an integer or something else, in which case it needs to raise
|
238
276
|
def validate_self_should_error(value_to_check)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schemacop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sitrox
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ruby2_keywords
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.4
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -188,7 +202,6 @@ files:
|
|
188
202
|
- lib/schemacop/v2/node_supporting_field.rb
|
189
203
|
- lib/schemacop/v2/node_supporting_type.rb
|
190
204
|
- lib/schemacop/v2/node_with_block.rb
|
191
|
-
- lib/schemacop/v2/root_node.rb
|
192
205
|
- lib/schemacop/v2/validator/array_validator.rb
|
193
206
|
- lib/schemacop/v2/validator/boolean_validator.rb
|
194
207
|
- lib/schemacop/v2/validator/float_validator.rb
|