schemacop 3.0.5 → 3.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,17 @@ 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_validation('true') do
343
+ error '/', 'Matches 0 definitions but should match exactly 1.'
344
+ end
345
+ end
333
346
  end
334
347
  end
335
348
  end
@@ -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,35 @@ 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_cast('1.0', 1.0)
312
+ assert_cast(1.0, 1.0)
313
+
314
+ assert_validation('42')
315
+ assert_validation('0.5')
316
+
317
+ assert_validation('true') do
318
+ error '/', 'Matches 0 definitions but should match exactly 1.'
319
+ end
320
+
321
+ assert_validation('51') do
322
+ error '/', 'Matches 0 definitions but should match exactly 1.'
323
+ end
324
+
325
+ assert_validation('-2') do
326
+ error '/', 'Matches 0 definitions but should match exactly 1.'
327
+ end
328
+
329
+ assert_validation('3.1415') do
330
+ error '/', 'Matches 0 definitions but should match exactly 1.'
331
+ end
332
+ end
302
333
  end
303
334
  end
304
335
  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
 
@@ -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
 
@@ -3,7 +3,9 @@ require 'test_helper'
3
3
  module Schemacop
4
4
  module V3
5
5
  class StringNodeTest < V3Test
6
- EXP_INVALID_TYPE = 'Invalid type, expected "string".'.freeze
6
+ def self.invalid_type_error(type)
7
+ "Invalid type, got type \"#{type}\", expected \"string\"."
8
+ end
7
9
 
8
10
  def test_basic
9
11
  schema :string
@@ -33,7 +35,7 @@ module Schemacop
33
35
  assert_json(type: :string)
34
36
 
35
37
  assert_validation 42 do
36
- error '/', EXP_INVALID_TYPE
38
+ error '/', StringNodeTest.invalid_type_error(Integer)
37
39
  end
38
40
 
39
41
  schema { str! :name }
@@ -41,11 +43,11 @@ module Schemacop
41
43
  assert_json(type: :object, properties: { name: { type: :string } }, required: %i[name], additionalProperties: false)
42
44
 
43
45
  assert_validation name: :foo do
44
- error '/name', EXP_INVALID_TYPE
46
+ error '/name', StringNodeTest.invalid_type_error(Symbol)
45
47
  end
46
48
 
47
49
  assert_validation name: 234 do
48
- error '/name', EXP_INVALID_TYPE
50
+ error '/name', StringNodeTest.invalid_type_error(Integer)
49
51
  end
50
52
  end
51
53
 
@@ -190,7 +192,7 @@ module Schemacop
190
192
  assert_validation ''
191
193
 
192
194
  assert_validation 234 do
193
- error '/', 'Invalid type, expected "string".'
195
+ error '/', StringNodeTest.invalid_type_error(Integer)
194
196
  end
195
197
 
196
198
  assert_cast(nil, nil)
@@ -218,7 +220,7 @@ module Schemacop
218
220
  # Integer value 42 is in the enum of allowed values, but it's not a string,
219
221
  # so the validation still fails
220
222
  assert_validation 42 do
221
- error '/', 'Invalid type, expected "string".'
223
+ error '/', StringNodeTest.invalid_type_error(Integer)
222
224
  end
223
225
  end
224
226
 
@@ -278,7 +280,7 @@ module Schemacop
278
280
  assert_validation(nil)
279
281
  assert_validation('Foo')
280
282
  assert_validation(5) do
281
- error '/', 'Invalid type, expected "string".'
283
+ error '/', StringNodeTest.invalid_type_error(Integer)
282
284
  end
283
285
 
284
286
  assert_cast('Foo', 'Foo')
@@ -297,7 +299,7 @@ module Schemacop
297
299
  assert_validation(nil)
298
300
  assert_validation('123')
299
301
  assert_validation(5) do
300
- error '/', 'Invalid type, expected "string".'
302
+ error '/', StringNodeTest.invalid_type_error(Integer)
301
303
  end
302
304
 
303
305
  assert_cast('123', 123)
@@ -364,13 +366,13 @@ module Schemacop
364
366
  # Even we put those types in the enum, they need to fail the validations,
365
367
  # as they are not strings
366
368
  assert_validation(1) do
367
- error '/', 'Invalid type, expected "string".'
369
+ error '/', StringNodeTest.invalid_type_error(Integer)
368
370
  end
369
371
  assert_validation(:bar) do
370
- error '/', 'Invalid type, expected "string".'
372
+ error '/', StringNodeTest.invalid_type_error(Symbol)
371
373
  end
372
374
  assert_validation({ qux: 42 }) do
373
- error '/', 'Invalid type, expected "string".'
375
+ error '/', StringNodeTest.invalid_type_error(Hash)
374
376
  end
375
377
 
376
378
  # These need to fail validation, as they are not in the enum list
@@ -400,6 +402,53 @@ module Schemacop
400
402
  ]
401
403
  })
402
404
  end
405
+
406
+ def test_cast_empty_or_whitespace_string
407
+ schema :string
408
+
409
+ assert_cast(nil, nil)
410
+ assert_cast('', '')
411
+ assert_cast(' ', ' ')
412
+ assert_cast("\n", "\n")
413
+ assert_cast("\t", "\t")
414
+ end
415
+
416
+ def test_cast_empty_or_whitespace_string_required
417
+ schema :string, required: true
418
+
419
+ assert_validation(nil) do
420
+ error '/', 'Value must be given.'
421
+ end
422
+
423
+ assert_cast('', '')
424
+ assert_cast(' ', ' ')
425
+ assert_cast("\n", "\n")
426
+ assert_cast("\t", "\t")
427
+ end
428
+
429
+ def test_empty_or_whitespace_string_blank_not_allowed
430
+ schema :string, allow_blank: false
431
+
432
+ assert_validation(nil) do
433
+ error '/', 'String is blank but must not be blank!'
434
+ end
435
+
436
+ assert_validation('') do
437
+ error '/', 'String is blank but must not be blank!'
438
+ end
439
+
440
+ assert_validation(' ') do
441
+ error '/', 'String is blank but must not be blank!'
442
+ end
443
+
444
+ assert_validation("\n") do
445
+ error '/', 'String is blank but must not be blank!'
446
+ end
447
+
448
+ assert_validation("\t") do
449
+ error '/', 'String is blank but must not be blank!'
450
+ end
451
+ end
403
452
  end
404
453
  end
405
454
  end
@@ -3,7 +3,10 @@ require 'test_helper'
3
3
  module Schemacop
4
4
  module V3
5
5
  class SymbolNodeTest < V3Test
6
- EXP_INVALID_TYPE = 'Invalid type, expected "Symbol".'.freeze
6
+ def self.invalid_type_error(type)
7
+ type = type.class unless type.class == Class
8
+ "Invalid type, got type \"#{type}\", expected \"Symbol\"."
9
+ end
7
10
 
8
11
  def test_basic
9
12
  schema :symbol
@@ -11,10 +14,10 @@ module Schemacop
11
14
  assert_validation :foo
12
15
  assert_validation :'n0238n)Q(hqr3hrw3'
13
16
  assert_validation 42 do
14
- error '/', EXP_INVALID_TYPE
17
+ error '/', SymbolNodeTest.invalid_type_error(Integer)
15
18
  end
16
19
  assert_validation '42' do
17
- error '/', EXP_INVALID_TYPE
20
+ error '/', SymbolNodeTest.invalid_type_error(String)
18
21
  end
19
22
  assert_json({})
20
23
  end
@@ -56,13 +59,13 @@ module Schemacop
56
59
  # Even we put those types in the enum, they need to fail the validations,
57
60
  # as they are not symbols
58
61
  assert_validation('foo') do
59
- error '/', 'Invalid type, expected "Symbol".'
62
+ error '/', SymbolNodeTest.invalid_type_error(String)
60
63
  end
61
64
  assert_validation(1) do
62
- error '/', 'Invalid type, expected "Symbol".'
65
+ error '/', SymbolNodeTest.invalid_type_error(Integer)
63
66
  end
64
67
  assert_validation({ qux: 42 }) do
65
- error '/', 'Invalid type, expected "Symbol".'
68
+ error '/', SymbolNodeTest.invalid_type_error(Hash)
66
69
  end
67
70
 
68
71
  # These need to fail validation, as they are not in the enum list
@@ -70,6 +73,20 @@ module Schemacop
70
73
  error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
71
74
  end
72
75
  end
76
+
77
+ # rubocop:disable Lint/BooleanSymbol
78
+ def test_cast_str
79
+ schema :symbol, cast_str: true
80
+
81
+ assert_cast('true', :true)
82
+ assert_cast('foo', :foo)
83
+ assert_cast('1', :'1')
84
+
85
+ assert_cast(:true, :true)
86
+ assert_cast(:foo, :foo)
87
+ assert_cast(:'1', :'1')
88
+ end
89
+ # rubocop:enable Lint/BooleanSymbol
73
90
  end
74
91
  end
75
92
  end