schemacop 3.0.9 → 3.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +27 -0
- data/.releaser_config +0 -1
- data/CHANGELOG.md +17 -0
- data/README.md +1 -1
- data/README_V3.md +102 -43
- data/VERSION +1 -1
- data/lib/schemacop/v3/combination_node.rb +1 -1
- data/lib/schemacop/v3/node.rb +6 -5
- data/lib/schemacop/v3/one_of_node.rb +23 -1
- data/lib/schemacop/v3/string_node.rb +2 -2
- data/schemacop.gemspec +4 -4
- data/test/unit/schemacop/v3/array_node_test.rb +19 -17
- data/test/unit/schemacop/v3/boolean_node_test.rb +48 -8
- data/test/unit/schemacop/v3/hash_node_test.rb +49 -11
- data/test/unit/schemacop/v3/integer_node_test.rb +38 -14
- data/test/unit/schemacop/v3/node_test.rb +10 -0
- data/test/unit/schemacop/v3/number_node_test.rb +14 -9
- data/test/unit/schemacop/v3/object_node_test.rb +7 -7
- data/test/unit/schemacop/v3/one_of_node_test.rb +12 -0
- data/test/unit/schemacop/v3/reference_node_test.rb +4 -4
- data/test/unit/schemacop/v3/string_node_test.rb +13 -11
- data/test/unit/schemacop/v3/symbol_node_test.rb +9 -6
- metadata +3 -3
- data/.travis.yml +0 -9
@@ -3,7 +3,9 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class ArrayNodeTest < V3Test
|
6
|
-
|
6
|
+
def self.invalid_type_error(type)
|
7
|
+
"Invalid type, got type \"#{type}\", expected \"array\"."
|
8
|
+
end
|
7
9
|
|
8
10
|
def test_basic
|
9
11
|
schema :array
|
@@ -54,7 +56,7 @@ module Schemacop
|
|
54
56
|
error '/', 'Array has 2 items but must have exactly 1.'
|
55
57
|
end
|
56
58
|
assert_validation [123] do
|
57
|
-
error '/[0]', 'Invalid type, expected "object".'
|
59
|
+
error '/[0]', 'Invalid type, got type "Integer", expected "object".'
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
@@ -64,7 +66,7 @@ module Schemacop
|
|
64
66
|
assert_json(type: :array)
|
65
67
|
|
66
68
|
assert_validation 42 do
|
67
|
-
error '/',
|
69
|
+
error '/', ArrayNodeTest.invalid_type_error(Integer)
|
68
70
|
end
|
69
71
|
|
70
72
|
schema { ary! :foo }
|
@@ -79,11 +81,11 @@ module Schemacop
|
|
79
81
|
)
|
80
82
|
|
81
83
|
assert_validation foo: 42 do
|
82
|
-
error '/foo',
|
84
|
+
error '/foo', ArrayNodeTest.invalid_type_error(Integer)
|
83
85
|
end
|
84
86
|
|
85
87
|
assert_validation foo: {} do
|
86
|
-
error '/foo',
|
88
|
+
error '/foo', ArrayNodeTest.invalid_type_error(ActiveSupport::HashWithIndifferentAccess)
|
87
89
|
end
|
88
90
|
end
|
89
91
|
|
@@ -196,7 +198,7 @@ module Schemacop
|
|
196
198
|
end
|
197
199
|
|
198
200
|
assert_validation %i[foo] do
|
199
|
-
error '/[0]', 'Invalid type, expected "string".'
|
201
|
+
error '/[0]', 'Invalid type, got type "Symbol", expected "string".'
|
200
202
|
end
|
201
203
|
end
|
202
204
|
|
@@ -230,7 +232,7 @@ module Schemacop
|
|
230
232
|
end
|
231
233
|
|
232
234
|
assert_validation([42, 42, { name: 'Hello' }]) do
|
233
|
-
error '/[0]', 'Invalid type, expected "string".'
|
235
|
+
error '/[0]', 'Invalid type, got type "Integer", expected "string".'
|
234
236
|
end
|
235
237
|
|
236
238
|
assert_validation(['foo', 42, { namex: 'Hello' }]) do
|
@@ -305,7 +307,7 @@ module Schemacop
|
|
305
307
|
assert_validation(['foo', 42])
|
306
308
|
assert_validation(['foo', 42, 42])
|
307
309
|
assert_validation(['foo', :foo]) do
|
308
|
-
error '/[1]', 'Invalid type, expected "integer".'
|
310
|
+
error '/[1]', 'Invalid type, got type "Symbol", expected "integer".'
|
309
311
|
end
|
310
312
|
end
|
311
313
|
|
@@ -328,7 +330,7 @@ module Schemacop
|
|
328
330
|
assert_validation(['foo', 42])
|
329
331
|
assert_validation(['foo', 42, 'additional', 'another'])
|
330
332
|
assert_validation(['foo', 42, 'additional', 42, 'another']) do
|
331
|
-
error '/[3]', 'Invalid type, expected "string".'
|
333
|
+
error '/[3]', 'Invalid type, got type "Integer", expected "string".'
|
332
334
|
end
|
333
335
|
|
334
336
|
assert_cast(['foo', 42], ['foo', 42])
|
@@ -354,7 +356,7 @@ module Schemacop
|
|
354
356
|
assert_validation(['foo', 42])
|
355
357
|
assert_validation(['foo', 42, '1990-01-01'])
|
356
358
|
assert_validation(['foo', 42, '1990-01-01', 42]) do
|
357
|
-
error '/[3]', 'Invalid type, expected "string".'
|
359
|
+
error '/[3]', 'Invalid type, got type "Integer", expected "string".'
|
358
360
|
end
|
359
361
|
assert_validation(['foo', 42, '1990-01-01', 'foo']) do
|
360
362
|
error '/[3]', 'String does not match format "date".'
|
@@ -439,7 +441,7 @@ module Schemacop
|
|
439
441
|
assert_validation(['foo', 42, { foo: '1990-01-01', bar: :baz }])
|
440
442
|
|
441
443
|
assert_validation(['foo', 42, { foo: 1234 }]) do
|
442
|
-
error '/[2]/foo', 'Invalid type, expected "string".'
|
444
|
+
error '/[2]/foo', 'Invalid type, got type "Integer", expected "string".'
|
443
445
|
end
|
444
446
|
assert_validation(['foo', 42, { foo: 'String' }]) do
|
445
447
|
error '/[2]/foo', 'String does not match format "date".'
|
@@ -555,16 +557,16 @@ module Schemacop
|
|
555
557
|
# Even we put those types in the enum, they need to fail the validations,
|
556
558
|
# as they are not arrays
|
557
559
|
assert_validation('foo') do
|
558
|
-
error '/',
|
560
|
+
error '/', ArrayNodeTest.invalid_type_error(String)
|
559
561
|
end
|
560
562
|
assert_validation(1) do
|
561
|
-
error '/',
|
563
|
+
error '/', ArrayNodeTest.invalid_type_error(Integer)
|
562
564
|
end
|
563
565
|
assert_validation(:bar) do
|
564
|
-
error '/',
|
566
|
+
error '/', ArrayNodeTest.invalid_type_error(Symbol)
|
565
567
|
end
|
566
568
|
assert_validation({ qux: 42 }) do
|
567
|
-
error '/',
|
569
|
+
error '/', ArrayNodeTest.invalid_type_error(Hash)
|
568
570
|
end
|
569
571
|
|
570
572
|
# These need to fail validation, as they are not in the enum list
|
@@ -669,8 +671,8 @@ module Schemacop
|
|
669
671
|
assert_validation([1, 2, 3, 4, 5, 6])
|
670
672
|
|
671
673
|
assert_validation([1, :foo, 'bar']) do
|
672
|
-
error '/[1]', 'Invalid type, expected "integer".'
|
673
|
-
error '/[2]', 'Invalid type, expected "integer".'
|
674
|
+
error '/[1]', 'Invalid type, got type "Symbol", expected "integer".'
|
675
|
+
error '/[2]', 'Invalid type, got type "String", expected "integer".'
|
674
676
|
end
|
675
677
|
end
|
676
678
|
|
@@ -4,7 +4,10 @@ require 'test_helper'
|
|
4
4
|
module Schemacop
|
5
5
|
module V3
|
6
6
|
class BooleanNodeTest < V3Test
|
7
|
-
|
7
|
+
def self.invalid_type_error(type)
|
8
|
+
type = type.class unless type.class == Class
|
9
|
+
"Invalid type, got type \"#{type}\", expected \"boolean\"."
|
10
|
+
end
|
8
11
|
|
9
12
|
def test_basic
|
10
13
|
schema :boolean
|
@@ -47,12 +50,12 @@ module Schemacop
|
|
47
50
|
assert_json(type: :boolean)
|
48
51
|
|
49
52
|
assert_validation 42 do
|
50
|
-
error '/',
|
53
|
+
error '/', BooleanNodeTest.invalid_type_error(Integer)
|
51
54
|
end
|
52
55
|
|
53
56
|
[:true, 'true', :false, 'false', 0, 1].each do |value|
|
54
57
|
assert_validation value do
|
55
|
-
error '/',
|
58
|
+
error '/', BooleanNodeTest.invalid_type_error(value)
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
@@ -68,7 +71,7 @@ module Schemacop
|
|
68
71
|
|
69
72
|
[:true, 'true', :false, 'false', 0, 1].each do |value|
|
70
73
|
assert_validation name: value do
|
71
|
-
error '/name',
|
74
|
+
error '/name', BooleanNodeTest.invalid_type_error(value)
|
72
75
|
end
|
73
76
|
end
|
74
77
|
end
|
@@ -87,13 +90,13 @@ module Schemacop
|
|
87
90
|
# Even we put those types in the enum, they need to fail the validations,
|
88
91
|
# as they are not booleans
|
89
92
|
assert_validation('foo') do
|
90
|
-
error '/',
|
93
|
+
error '/', BooleanNodeTest.invalid_type_error(String)
|
91
94
|
end
|
92
95
|
assert_validation(:bar) do
|
93
|
-
error '/',
|
96
|
+
error '/', BooleanNodeTest.invalid_type_error(Symbol)
|
94
97
|
end
|
95
98
|
assert_validation({ qux: 42 }) do
|
96
|
-
error '/',
|
99
|
+
error '/', BooleanNodeTest.invalid_type_error(Hash)
|
97
100
|
end
|
98
101
|
|
99
102
|
# These need to fail validation, as they are not in the enum list
|
@@ -143,12 +146,49 @@ module Schemacop
|
|
143
146
|
assert_cast('true', true)
|
144
147
|
assert_cast('false', false)
|
145
148
|
|
149
|
+
assert_cast('1', true)
|
150
|
+
assert_cast('0', false)
|
151
|
+
|
152
|
+
assert_cast(true, true)
|
153
|
+
assert_cast(false, false)
|
154
|
+
|
155
|
+
assert_validation('5') do
|
156
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
157
|
+
end
|
158
|
+
|
159
|
+
# Nil can be validated, as it's not required
|
160
|
+
assert_validation(nil)
|
161
|
+
|
162
|
+
assert_validation('')
|
163
|
+
|
164
|
+
assert_cast('', nil)
|
165
|
+
assert_cast(nil, nil)
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_cast_str_required
|
169
|
+
schema :boolean, cast_str: true, required: true
|
170
|
+
|
171
|
+
assert_cast('true', true)
|
172
|
+
assert_cast('false', false)
|
173
|
+
|
146
174
|
assert_cast(true, true)
|
147
175
|
assert_cast(false, false)
|
148
176
|
|
149
|
-
assert_validation('
|
177
|
+
assert_validation('4') do
|
178
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
179
|
+
end
|
180
|
+
|
181
|
+
assert_validation('foo') do
|
150
182
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
151
183
|
end
|
184
|
+
|
185
|
+
assert_validation(nil) do
|
186
|
+
error '/', 'Value must be given.'
|
187
|
+
end
|
188
|
+
|
189
|
+
assert_validation('') do
|
190
|
+
error '/', 'Value must be given.'
|
191
|
+
end
|
152
192
|
end
|
153
193
|
end
|
154
194
|
end
|
@@ -3,8 +3,6 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class HashNodeTest < V3Test
|
6
|
-
EXP_INVALID_TYPE = 'Invalid type, expected "hash".'.freeze
|
7
|
-
|
8
6
|
def test_basic
|
9
7
|
schema
|
10
8
|
assert_validation({})
|
@@ -54,7 +52,7 @@ module Schemacop
|
|
54
52
|
|
55
53
|
assert_validation(foo: 'bar', baz: 'foo', answer: '42')
|
56
54
|
assert_validation(foo: 'bar', baz: 'foo', answer: 42) do
|
57
|
-
error '/answer', 'Invalid type, expected "string".'
|
55
|
+
error '/answer', 'Invalid type, got type "Integer", expected "string".'
|
58
56
|
end
|
59
57
|
|
60
58
|
assert_json(
|
@@ -70,7 +68,7 @@ module Schemacop
|
|
70
68
|
@schema.validate!({ foo: 'foo' })
|
71
69
|
end
|
72
70
|
|
73
|
-
assert_raises_with_message Exceptions::ValidationError, '/bar: Invalid type, expected "string".' do
|
71
|
+
assert_raises_with_message Exceptions::ValidationError, '/bar: Invalid type, got type "Symbol", expected "string".' do
|
74
72
|
@schema.validate!({ foo: 'foo', bar: :baz })
|
75
73
|
end
|
76
74
|
end
|
@@ -247,7 +245,7 @@ module Schemacop
|
|
247
245
|
|
248
246
|
assert_validation(name: 'John', xy: 'John', bar_baz: 'Doe') do
|
249
247
|
error '/', 'Obsolete property "xy".'
|
250
|
-
error '/bar_baz', 'Invalid type, expected "integer".'
|
248
|
+
error '/bar_baz', 'Invalid type, got type "String", expected "integer".'
|
251
249
|
end
|
252
250
|
|
253
251
|
assert_json(
|
@@ -276,7 +274,7 @@ module Schemacop
|
|
276
274
|
assert_validation(keyword: 'value')
|
277
275
|
|
278
276
|
assert_validation(keyword: 42) do
|
279
|
-
error '/keyword', 'Invalid type, expected "string".'
|
277
|
+
error '/keyword', 'Invalid type, got type "Integer", expected "string".'
|
280
278
|
end
|
281
279
|
|
282
280
|
assert_json(
|
@@ -759,10 +757,10 @@ module Schemacop
|
|
759
757
|
# Even we put those types in the enum, they need to fail the validations,
|
760
758
|
# as they are not strings
|
761
759
|
assert_validation({ foo: 123 }) do
|
762
|
-
error '/foo', 'Invalid type, expected "string".'
|
760
|
+
error '/foo', 'Invalid type, got type "Integer", expected "string".'
|
763
761
|
end
|
764
762
|
assert_validation({ foo: :faz }) do
|
765
|
-
error '/foo', 'Invalid type, expected "string".'
|
763
|
+
error '/foo', 'Invalid type, got type "Symbol", expected "string".'
|
766
764
|
end
|
767
765
|
|
768
766
|
# These need to fail validation, as they are not in the enum list
|
@@ -918,7 +916,7 @@ module Schemacop
|
|
918
916
|
end
|
919
917
|
|
920
918
|
assert_validation({ foo: '13' }) do
|
921
|
-
error '/foo', 'Invalid type, expected "integer".'
|
919
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
922
920
|
end
|
923
921
|
|
924
922
|
assert_cast(nil, nil)
|
@@ -937,7 +935,7 @@ module Schemacop
|
|
937
935
|
assert_validation({ foo: 42, bar: 13 })
|
938
936
|
|
939
937
|
assert_validation({ foo: '13' }) do
|
940
|
-
error '/foo', 'Invalid type, expected "integer".'
|
938
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
941
939
|
end
|
942
940
|
|
943
941
|
# assert_cast(nil, nil)
|
@@ -959,7 +957,7 @@ module Schemacop
|
|
959
957
|
end
|
960
958
|
|
961
959
|
assert_validation({ foo: '13' }) do
|
962
|
-
error '/foo', 'Invalid type, expected "integer".'
|
960
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
963
961
|
end
|
964
962
|
|
965
963
|
assert_cast(nil, nil)
|
@@ -967,6 +965,46 @@ module Schemacop
|
|
967
965
|
assert_cast({ foo: 42, bar: 13 }, { bar: 42 }.with_indifferent_access)
|
968
966
|
assert_cast({ bar: 13, foo: 42 }, { bar: 42 }.with_indifferent_access)
|
969
967
|
end
|
968
|
+
|
969
|
+
def test_cast_str_required
|
970
|
+
schema :hash do
|
971
|
+
boo! :active, cast_str: true
|
972
|
+
int! :id, cast_str: true
|
973
|
+
end
|
974
|
+
|
975
|
+
assert_validation({ active: true, id: 1 })
|
976
|
+
assert_validation({ active: 'true', id: '1' })
|
977
|
+
|
978
|
+
assert_validation({}) do
|
979
|
+
error '/active', 'Value must be given.'
|
980
|
+
error '/id', 'Value must be given.'
|
981
|
+
end
|
982
|
+
|
983
|
+
assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
|
984
|
+
|
985
|
+
assert_validation({ active: '', id: '' }) do
|
986
|
+
error '/active', 'Value must be given.'
|
987
|
+
error '/id', 'Value must be given.'
|
988
|
+
end
|
989
|
+
end
|
990
|
+
|
991
|
+
def test_cast_str_optional
|
992
|
+
schema :hash do
|
993
|
+
boo? :active, cast_str: true
|
994
|
+
int? :id, cast_str: true
|
995
|
+
end
|
996
|
+
|
997
|
+
assert_validation({ active: true, id: 1 })
|
998
|
+
assert_validation({ active: 'true', id: '1' })
|
999
|
+
|
1000
|
+
assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
|
1001
|
+
|
1002
|
+
assert_validation({})
|
1003
|
+
assert_validation({ active: nil, id: nil })
|
1004
|
+
|
1005
|
+
assert_validation({ active: '', id: '' })
|
1006
|
+
assert_cast({ active: '', id: '' }, { active: nil, id: nil }.with_indifferent_access)
|
1007
|
+
end
|
970
1008
|
end
|
971
1009
|
end
|
972
1010
|
end
|
@@ -3,7 +3,9 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class IntegerNodeTest < V3Test
|
6
|
-
|
6
|
+
def self.invalid_type_error(type)
|
7
|
+
"Invalid type, got type \"#{type}\", expected \"integer\"."
|
8
|
+
end
|
7
9
|
|
8
10
|
def test_basic
|
9
11
|
schema :integer
|
@@ -59,11 +61,11 @@ module Schemacop
|
|
59
61
|
)
|
60
62
|
|
61
63
|
assert_validation 42.5 do
|
62
|
-
error '/',
|
64
|
+
error '/', IntegerNodeTest.invalid_type_error(Float)
|
63
65
|
end
|
64
66
|
|
65
67
|
assert_validation '42.5' do
|
66
|
-
error '/',
|
68
|
+
error '/', IntegerNodeTest.invalid_type_error(String)
|
67
69
|
end
|
68
70
|
|
69
71
|
schema { int! :age }
|
@@ -78,27 +80,27 @@ module Schemacop
|
|
78
80
|
)
|
79
81
|
|
80
82
|
assert_validation age: :foo do
|
81
|
-
error '/age',
|
83
|
+
error '/age', IntegerNodeTest.invalid_type_error(Symbol)
|
82
84
|
end
|
83
85
|
|
84
86
|
assert_validation age: '234' do
|
85
|
-
error '/age',
|
87
|
+
error '/age', IntegerNodeTest.invalid_type_error(String)
|
86
88
|
end
|
87
89
|
|
88
90
|
assert_validation age: 10.0 do
|
89
|
-
error '/age',
|
91
|
+
error '/age', IntegerNodeTest.invalid_type_error(Float)
|
90
92
|
end
|
91
93
|
|
92
94
|
assert_validation age: 4r do
|
93
|
-
error '/age',
|
95
|
+
error '/age', IntegerNodeTest.invalid_type_error(Rational)
|
94
96
|
end
|
95
97
|
|
96
98
|
assert_validation age: (4 + 0i) do
|
97
|
-
error '/age',
|
99
|
+
error '/age', IntegerNodeTest.invalid_type_error(Complex)
|
98
100
|
end
|
99
101
|
|
100
102
|
assert_validation age: BigDecimal(5) do
|
101
|
-
error '/age',
|
103
|
+
error '/age', IntegerNodeTest.invalid_type_error(BigDecimal)
|
102
104
|
end
|
103
105
|
end
|
104
106
|
|
@@ -210,7 +212,7 @@ module Schemacop
|
|
210
212
|
assert_validation(nil)
|
211
213
|
assert_validation(50)
|
212
214
|
assert_validation(5.2) do
|
213
|
-
error '/',
|
215
|
+
error '/', IntegerNodeTest.invalid_type_error(Float)
|
214
216
|
end
|
215
217
|
|
216
218
|
assert_cast(5, 5)
|
@@ -289,18 +291,18 @@ module Schemacop
|
|
289
291
|
# Even we put those types in the enum, they need to fail the validations,
|
290
292
|
# as they are not integers
|
291
293
|
assert_validation('foo') do
|
292
|
-
error '/',
|
294
|
+
error '/', IntegerNodeTest.invalid_type_error(String)
|
293
295
|
end
|
294
296
|
assert_validation(:bar) do
|
295
|
-
error '/',
|
297
|
+
error '/', IntegerNodeTest.invalid_type_error(Symbol)
|
296
298
|
end
|
297
299
|
assert_validation({ qux: 42 }) do
|
298
|
-
error '/',
|
300
|
+
error '/', IntegerNodeTest.invalid_type_error(Hash)
|
299
301
|
end
|
300
302
|
|
301
303
|
# This needs to fail as it is a number (float) and not an integer
|
302
304
|
assert_validation(4.2) do
|
303
|
-
error '/',
|
305
|
+
error '/', IntegerNodeTest.invalid_type_error(Float)
|
304
306
|
end
|
305
307
|
|
306
308
|
# These need to fail validation, as they are not in the enum list
|
@@ -337,6 +339,28 @@ module Schemacop
|
|
337
339
|
assert_cast('1', 1)
|
338
340
|
assert_cast(1, 1)
|
339
341
|
|
342
|
+
assert_cast(nil, nil)
|
343
|
+
assert_cast('', nil)
|
344
|
+
|
345
|
+
assert_validation('true') do
|
346
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_cast_str_required
|
351
|
+
schema :integer, cast_str: true, required: true
|
352
|
+
|
353
|
+
assert_cast('1', 1)
|
354
|
+
assert_cast(1, 1)
|
355
|
+
|
356
|
+
assert_validation(nil) do
|
357
|
+
error '/', 'Value must be given.'
|
358
|
+
end
|
359
|
+
|
360
|
+
assert_validation('') do
|
361
|
+
error '/', 'Value must be given.'
|
362
|
+
end
|
363
|
+
|
340
364
|
assert_validation('true') do
|
341
365
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
342
366
|
end
|
@@ -93,6 +93,10 @@ module Schemacop
|
|
93
93
|
error '/', 'Value must be given.'
|
94
94
|
end
|
95
95
|
|
96
|
+
assert_validation('') do
|
97
|
+
error '/', 'Value must be given.'
|
98
|
+
end
|
99
|
+
|
96
100
|
assert_validation('5')
|
97
101
|
assert_validation('5.3') do
|
98
102
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
@@ -156,6 +160,12 @@ module Schemacop
|
|
156
160
|
assert_validation([nil]) do
|
157
161
|
error '/[0]', 'Value must be given.'
|
158
162
|
end
|
163
|
+
assert_validation(['']) do
|
164
|
+
error '/[0]', 'Value must be given.'
|
165
|
+
end
|
166
|
+
assert_validation([]) do
|
167
|
+
error '/', 'Array has 0 items but must have exactly 1.'
|
168
|
+
end
|
159
169
|
end
|
160
170
|
|
161
171
|
def test_not_support_block
|
@@ -3,7 +3,9 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class NumberNodeTest < V3Test
|
6
|
-
|
6
|
+
def self.invalid_type_error(type)
|
7
|
+
"Invalid type, got type \"#{type}\", expected \"big_decimal\" or \"float\" or \"integer\" or \"rational\"."
|
8
|
+
end
|
7
9
|
|
8
10
|
def test_basic
|
9
11
|
schema :number
|
@@ -16,7 +18,7 @@ module Schemacop
|
|
16
18
|
assert_validation(BigDecimal(6))
|
17
19
|
|
18
20
|
assert_validation((6 + 0i)) do
|
19
|
-
error '/',
|
21
|
+
error '/', NumberNodeTest.invalid_type_error(Complex)
|
20
22
|
end
|
21
23
|
|
22
24
|
assert_json(type: :number)
|
@@ -52,7 +54,7 @@ module Schemacop
|
|
52
54
|
assert_validation [30.3, 42.0]
|
53
55
|
assert_validation [30, 30r, 30.0, BigDecimal(30)]
|
54
56
|
assert_validation ['30.3', 30.3] do
|
55
|
-
error '/[0]',
|
57
|
+
error '/[0]', NumberNodeTest.invalid_type_error(String)
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
@@ -64,17 +66,17 @@ module Schemacop
|
|
64
66
|
)
|
65
67
|
|
66
68
|
assert_validation '42.5' do
|
67
|
-
error '/',
|
69
|
+
error '/', NumberNodeTest.invalid_type_error(String)
|
68
70
|
end
|
69
71
|
|
70
72
|
schema { num! :age }
|
71
73
|
|
72
74
|
assert_validation age: :foo do
|
73
|
-
error '/age',
|
75
|
+
error '/age', NumberNodeTest.invalid_type_error(Symbol)
|
74
76
|
end
|
75
77
|
|
76
78
|
assert_validation age: '234' do
|
77
|
-
error '/age',
|
79
|
+
error '/age', NumberNodeTest.invalid_type_error(String)
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
@@ -261,13 +263,13 @@ module Schemacop
|
|
261
263
|
# Even we put those types in the enum, they need to fail the validations,
|
262
264
|
# as they are not numbers
|
263
265
|
assert_validation('foo') do
|
264
|
-
error '/',
|
266
|
+
error '/', NumberNodeTest.invalid_type_error(String)
|
265
267
|
end
|
266
268
|
assert_validation(:bar) do
|
267
|
-
error '/',
|
269
|
+
error '/', NumberNodeTest.invalid_type_error(Symbol)
|
268
270
|
end
|
269
271
|
assert_validation({ qux: 42 }) do
|
270
|
-
error '/',
|
272
|
+
error '/', NumberNodeTest.invalid_type_error(Hash)
|
271
273
|
end
|
272
274
|
|
273
275
|
# These need to fail validation, as they are not in the enum list
|
@@ -306,6 +308,9 @@ module Schemacop
|
|
306
308
|
assert_cast('1', 1)
|
307
309
|
assert_cast(1, 1)
|
308
310
|
|
311
|
+
assert_validation(nil)
|
312
|
+
assert_validation('')
|
313
|
+
|
309
314
|
assert_cast('1.0', 1.0)
|
310
315
|
assert_cast(1.0, 1.0)
|
311
316
|
|
@@ -28,10 +28,10 @@ module Schemacop
|
|
28
28
|
assert_validation 'foo'
|
29
29
|
assert_validation Date.today
|
30
30
|
assert_validation({}.with_indifferent_access) do
|
31
|
-
error '/', 'Invalid type, expected "Date" or "String".'
|
31
|
+
error '/', 'Invalid type, got type "ActiveSupport::HashWithIndifferentAccess", expected "Date" or "String".'
|
32
32
|
end
|
33
33
|
assert_validation DateTime.now do
|
34
|
-
error '/', 'Invalid type, expected "Date" or "String".'
|
34
|
+
error '/', 'Invalid type, got type "DateTime", expected "Date" or "String".'
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -44,7 +44,7 @@ module Schemacop
|
|
44
44
|
assert_validation DateTime.now
|
45
45
|
assert_validation({}.with_indifferent_access)
|
46
46
|
assert_validation Time.now do
|
47
|
-
error '/', 'Invalid type, expected "Date" or "Hash" or "String".'
|
47
|
+
error '/', 'Invalid type, got type "Time", expected "Date" or "Hash" or "String".'
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -74,7 +74,7 @@ module Schemacop
|
|
74
74
|
assert_validation myobj: ''
|
75
75
|
assert_validation myobj: '42'
|
76
76
|
assert_validation myobj: Date.today do
|
77
|
-
error '/myobj', 'Invalid type, expected "String".'
|
77
|
+
error '/myobj', 'Invalid type, got type "Date", expected "String".'
|
78
78
|
end
|
79
79
|
assert_validation({}) do
|
80
80
|
error '/myobj', 'Value must be given.'
|
@@ -134,13 +134,13 @@ module Schemacop
|
|
134
134
|
|
135
135
|
# These will fail, as they aren't of one of the classed we defined above
|
136
136
|
assert_validation([]) do
|
137
|
-
error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
|
137
|
+
error '/', 'Invalid type, got type "Array", expected "String" or "Symbol" or "TrueClass".'
|
138
138
|
end
|
139
139
|
assert_validation({ qux: '123' }) do
|
140
|
-
error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
|
140
|
+
error '/', 'Invalid type, got type "Hash", expected "String" or "Symbol" or "TrueClass".'
|
141
141
|
end
|
142
142
|
assert_validation(false) do
|
143
|
-
error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
|
143
|
+
error '/', 'Invalid type, got type "FalseClass", expected "String" or "Symbol" or "TrueClass".'
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
@@ -182,6 +182,18 @@ module Schemacop
|
|
182
182
|
schema :one_of
|
183
183
|
end
|
184
184
|
end
|
185
|
+
|
186
|
+
def test_treat_blank_as_nil
|
187
|
+
schema :one_of, treat_blank_as_nil: true do
|
188
|
+
boo
|
189
|
+
str format: :boolean
|
190
|
+
end
|
191
|
+
|
192
|
+
assert_validation(nil)
|
193
|
+
assert_validation('')
|
194
|
+
assert_validation('true')
|
195
|
+
assert_validation(true)
|
196
|
+
end
|
185
197
|
end
|
186
198
|
end
|
187
199
|
end
|
@@ -26,13 +26,13 @@ module Schemacop
|
|
26
26
|
assert_validation(bar: { foo: 'String', baz: { foo: 42 } })
|
27
27
|
|
28
28
|
assert_validation(foo: 42) do
|
29
|
-
error '/foo', 'Invalid type, expected "string".'
|
29
|
+
error '/foo', 'Invalid type, got type "Integer", expected "string".'
|
30
30
|
end
|
31
31
|
assert_validation(bar: { foo: 42 }) do
|
32
|
-
error '/bar/foo', 'Invalid type, expected "string".'
|
32
|
+
error '/bar/foo', 'Invalid type, got type "Integer", expected "string".'
|
33
33
|
end
|
34
34
|
assert_validation(bar: { foo: 'String', baz: { foo: '42' } }) do
|
35
|
-
error '/bar/baz/foo', 'Invalid type, expected "integer".'
|
35
|
+
error '/bar/baz/foo', 'Invalid type, got type "String", expected "integer".'
|
36
36
|
end
|
37
37
|
|
38
38
|
assert_json({
|
@@ -306,7 +306,7 @@ module Schemacop
|
|
306
306
|
with_context context do
|
307
307
|
assert_validation(first_name: 'John', last_name: 'Doe')
|
308
308
|
assert_validation(first_name: 'John', last_name: 42) do
|
309
|
-
error '/last_name', 'Invalid type, expected "string".'
|
309
|
+
error '/last_name', 'Invalid type, got type "Integer", expected "string".'
|
310
310
|
end
|
311
311
|
end
|
312
312
|
|