schemacop 3.0.6 → 3.0.11

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.
@@ -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
- EXP_INVALID_TYPE = 'Invalid type, expected "integer".'.freeze
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 '/', EXP_INVALID_TYPE
64
+ error '/', IntegerNodeTest.invalid_type_error(Float)
63
65
  end
64
66
 
65
67
  assert_validation '42.5' do
66
- error '/', EXP_INVALID_TYPE
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', EXP_INVALID_TYPE
83
+ error '/age', IntegerNodeTest.invalid_type_error(Symbol)
82
84
  end
83
85
 
84
86
  assert_validation age: '234' do
85
- error '/age', EXP_INVALID_TYPE
87
+ error '/age', IntegerNodeTest.invalid_type_error(String)
86
88
  end
87
89
 
88
90
  assert_validation age: 10.0 do
89
- error '/age', EXP_INVALID_TYPE
91
+ error '/age', IntegerNodeTest.invalid_type_error(Float)
90
92
  end
91
93
 
92
94
  assert_validation age: 4r do
93
- error '/age', EXP_INVALID_TYPE
95
+ error '/age', IntegerNodeTest.invalid_type_error(Rational)
94
96
  end
95
97
 
96
98
  assert_validation age: (4 + 0i) do
97
- error '/age', EXP_INVALID_TYPE
99
+ error '/age', IntegerNodeTest.invalid_type_error(Complex)
98
100
  end
99
101
 
100
102
  assert_validation age: BigDecimal(5) do
101
- error '/age', EXP_INVALID_TYPE
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 '/', 'Invalid type, expected "integer".'
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 '/', 'Invalid type, expected "integer".'
294
+ error '/', IntegerNodeTest.invalid_type_error(String)
293
295
  end
294
296
  assert_validation(:bar) do
295
- error '/', 'Invalid type, expected "integer".'
297
+ error '/', IntegerNodeTest.invalid_type_error(Symbol)
296
298
  end
297
299
  assert_validation({ qux: 42 }) do
298
- error '/', 'Invalid type, expected "integer".'
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 '/', 'Invalid type, expected "integer".'
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
@@ -330,6 +332,39 @@ module Schemacop
330
332
  ]
331
333
  })
332
334
  end
335
+
336
+ def test_cast_str
337
+ schema :integer, cast_str: true
338
+
339
+ assert_cast('1', 1)
340
+ assert_cast(1, 1)
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
+
364
+ assert_validation('true') do
365
+ error '/', 'Matches 0 definitions but should match exactly 1.'
366
+ end
367
+ end
333
368
  end
334
369
  end
335
370
  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
- EXP_INVALID_TYPE = 'Invalid type, expected "big_decimal" or "float" or "integer" or "rational".'.freeze
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 '/', EXP_INVALID_TYPE
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]', EXP_INVALID_TYPE
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 '/', EXP_INVALID_TYPE
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', EXP_INVALID_TYPE
75
+ error '/age', NumberNodeTest.invalid_type_error(Symbol)
74
76
  end
75
77
 
76
78
  assert_validation age: '234' do
77
- error '/age', EXP_INVALID_TYPE
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 '/', EXP_INVALID_TYPE
266
+ error '/', NumberNodeTest.invalid_type_error(String)
265
267
  end
266
268
  assert_validation(:bar) do
267
- error '/', EXP_INVALID_TYPE
269
+ error '/', NumberNodeTest.invalid_type_error(Symbol)
268
270
  end
269
271
  assert_validation({ qux: 42 }) do
270
- error '/', EXP_INVALID_TYPE
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
@@ -299,6 +301,38 @@ module Schemacop
299
301
  ]
300
302
  })
301
303
  end
304
+
305
+ def test_cast_str
306
+ schema :number, cast_str: true, minimum: 0.0, maximum: 50r, multiple_of: BigDecimal('0.5')
307
+
308
+ assert_cast('1', 1)
309
+ assert_cast(1, 1)
310
+
311
+ assert_validation(nil)
312
+ assert_validation('')
313
+
314
+ assert_cast('1.0', 1.0)
315
+ assert_cast(1.0, 1.0)
316
+
317
+ assert_validation('42')
318
+ assert_validation('0.5')
319
+
320
+ assert_validation('true') do
321
+ error '/', 'Matches 0 definitions but should match exactly 1.'
322
+ end
323
+
324
+ assert_validation('51') do
325
+ error '/', 'Matches 0 definitions but should match exactly 1.'
326
+ end
327
+
328
+ assert_validation('-2') do
329
+ error '/', 'Matches 0 definitions but should match exactly 1.'
330
+ end
331
+
332
+ assert_validation('3.1415') do
333
+ error '/', 'Matches 0 definitions but should match exactly 1.'
334
+ end
335
+ end
302
336
  end
303
337
  end
304
338
  end
@@ -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
 
@@ -59,7 +59,10 @@ module Schemacop
59
59
  end
60
60
 
61
61
  def test_hash
62
- schema { obj! :myobj, String }
62
+ schema do
63
+ obj! :myobj, classes: String
64
+ end
65
+
63
66
  assert_json(
64
67
  type: :object,
65
68
  properties: {
@@ -71,13 +74,23 @@ module Schemacop
71
74
  assert_validation myobj: ''
72
75
  assert_validation myobj: '42'
73
76
  assert_validation myobj: Date.today do
74
- error '/myobj', 'Invalid type, expected "String".'
77
+ error '/myobj', 'Invalid type, got type "Date", expected "String".'
75
78
  end
76
79
  assert_validation({}) do
77
80
  error '/myobj', 'Value must be given.'
78
81
  end
79
82
  end
80
83
 
84
+ def test_hash_no_class
85
+ schema do
86
+ obj! :myobj
87
+ end
88
+
89
+ assert_validation(myobj: 'str')
90
+ assert_validation(myobj: 123)
91
+ assert_validation(myobj: Object.new)
92
+ end
93
+
81
94
  def test_enum_schema
82
95
  schema :object, enum: [true, 'foo', :baz, [], { qux: '123' }]
83
96
 
@@ -121,13 +134,13 @@ module Schemacop
121
134
 
122
135
  # These will fail, as they aren't of one of the classed we defined above
123
136
  assert_validation([]) do
124
- error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
137
+ error '/', 'Invalid type, got type "Array", expected "String" or "Symbol" or "TrueClass".'
125
138
  end
126
139
  assert_validation({ qux: '123' }) do
127
- error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
140
+ error '/', 'Invalid type, got type "Hash", expected "String" or "Symbol" or "TrueClass".'
128
141
  end
129
142
  assert_validation(false) do
130
- error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
143
+ error '/', 'Invalid type, got type "FalseClass", expected "String" or "Symbol" or "TrueClass".'
131
144
  end
132
145
  end
133
146