schemacop 3.0.5 → 3.0.10

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
  SHA256:
3
- metadata.gz: 27400e248bfdb65fc7cc229fbbb625cfa68a1421996fc5cbbb3283eb82d28a1e
4
- data.tar.gz: 4b435374b7d2c0ce05e4410235995983dc0f96c8d6c21b69d88e80abdae7aed0
3
+ metadata.gz: 8700be126ea3aced42828ce418ad665095584ba0e217eebb0cf2e42faf5b4aef
4
+ data.tar.gz: 9774973e128d64f02a06e84cfa19990b0a7b0890cf57d4fb94c0f5058337dd20
5
5
  SHA512:
6
- metadata.gz: 194c2f72fcadad2e311e1c96ca9bfd004e5f27c5d591bd1188a1fb177d56a75aa2670b50fddc07e7afaee99fd48f98195156a8f98a250485fd6a75a0ac57bda8
7
- data.tar.gz: 798a3dfaf53b6c2996e2799cc841bdf2172ef4ebd89c022c67a2d21424add61f1c0ef0c686e11c17b589b417961343f603012395e5510284573cb8a7e78a3468
6
+ metadata.gz: 0cdd515a0d47c7d707b8418fa5f0c7e6e73375ccb28bd2552b8c5f960e0279c75fb3a89c99b4ff677069c7a75e3d2f2376645d720d0d65980688602af09e4d28
7
+ data.tar.gz: 27cb5b5b8c4c8e14601c907c443d4fd904457ad0762c31af26aabf7d75febbf49459555e19665a1e56674b05601b4a223ccc18cb12fcebf9fddd526c3ec37442
data/.releaser_config CHANGED
@@ -1,3 +1,4 @@
1
1
  version_file: VERSION
2
2
  always_from_master: true
3
3
  gem_style: github
4
+ ruby_command: ruby -S
data/CHANGELOG.md CHANGED
@@ -1,14 +1,34 @@
1
1
  # Change log
2
2
 
3
- <!--
4
- ## master (unreleased)
3
+ ## 3.0.10 (2021-03-19)
5
4
 
6
- ### New features
5
+ * If wrong type is given, report class of wrong type in error message
7
6
 
8
- ### Bug fixes
7
+ ## 3.0.9 (2021-02-26)
9
8
 
10
- ### Changes
11
- -->
9
+ * Fix casting of blank strings
10
+
11
+ * Add `allow_blank` option to `String` nodes
12
+
13
+ ## 3.0.8 (2021-02-23)
14
+
15
+ * Fix `object` nodes in hashes with no classes
16
+
17
+ * Document `cast_str` option
18
+
19
+ ## 3.0.7 (2021-02-22)
20
+
21
+ * Adapt schema definitions handling when using Swagger-standard JSON
22
+
23
+ ## 3.0.6 (2021-02-14)
24
+
25
+ * Remove option `json_format` from {Schemacop::Schema3.as_json as_json} again.
26
+ If you need to use the swagger format, use
27
+ {Schemacop::V3::Context.with_json_format} instead.
28
+
29
+ * Rename `Schemacop::V3::Context.spawn_with` to
30
+ {Schemacop::V3::Context.with_json_format} and make keyword argument
31
+ `json_format` a positional argument.
12
32
 
13
33
  ## 3.0.5 (2021-02-14)
14
34
 
data/README_V3.md CHANGED
@@ -75,12 +75,12 @@ Schemacop can raise the following exceptions:
75
75
  Example:
76
76
 
77
77
  ```ruby
78
- schema = Schemacop::Schema3.new do
78
+ schema = Schemacop::Schema3.new :hash do
79
79
  int! :foo
80
80
  end
81
81
 
82
82
  schema.validate!(foo: 'bar')
83
- # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "integer".
83
+ # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "String", expected "integer".
84
84
  ```
85
85
 
86
86
  * `Schemacop::Exceptions::InvalidSchemaError`: This exception is raised when the
@@ -141,7 +141,7 @@ schema = Schemacop::Schema3.new :string, enum: ['foo', 'bar', 42]
141
141
 
142
142
  schema.validate!('foo') # => "foo"
143
143
  schema.validate!('bar') # => "bar"
144
- schema.validate!(42) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "string".
144
+ schema.validate!(42) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "string".
145
145
  ```
146
146
 
147
147
  The enum will also be provided in the json output:
@@ -178,7 +178,7 @@ Note that the default value you use is also validated against the schema:
178
178
  schema = Schemacop::Schema3.new :string, default: 42
179
179
 
180
180
  schema.validate!('foo') # => "foo"
181
- schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "string".
181
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "string".
182
182
  ```
183
183
 
184
184
  ## Nodes
@@ -199,14 +199,19 @@ transformed into various types.
199
199
  * `max_length`
200
200
  Defines the (inclusive) maximum required string length
201
201
  * `pattern`
202
- Defines a (ruby) regex pattern the value will be matched against. Must be a
203
- string and should generally start with `^` and end with `$` so as to evaluate
204
- the entire string. It should not be enclosed in `/` characters.
202
+ Defines a (ruby) regex pattern the value will be matched against. Must be either
203
+ a string which should not be enclosed in `/` characters, or a Ruby Regexp.
204
+ The pattern should generally start with `^` and end with `$` so as to evaluate
205
+ the entire string.
205
206
  * `format`
206
207
  The `format` option allows for basic semantic validation on certain kinds of
207
208
  string values that are commonly used. See section *formats* for more
208
209
  information on the available formats. Note that strings with a format are also
209
210
  **casted** into that format.
211
+ * `allow_blank`
212
+ By default, blank strings are allowed and left as they are when casted (e.g.
213
+ the string `''` is valid). If you want to disallow blank strings, set this
214
+ option to `false`.
210
215
 
211
216
  #### Formats
212
217
 
@@ -246,6 +251,38 @@ transformed into various types.
246
251
 
247
252
  #### Examples
248
253
 
254
+ ```ruby
255
+ # Basic example
256
+ schema = Schemacop::Schema3.new :string
257
+ schema.validate!(nil) # => nil
258
+ schema.validate!('') # => ""
259
+ schema.validate!('foo') # => "foo"
260
+ schema.validate!("\n") # => "\n"
261
+ ```
262
+
263
+ With the `required` option:
264
+
265
+ ```ruby
266
+ # Basic example
267
+ schema = Schemacop::Schema3.new :string, required: true
268
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
269
+ schema.validate!('') # => ""
270
+ schema.validate!('foo') # => "foo"
271
+ schema.validate!("\n") # => "\n"
272
+ ```
273
+
274
+ With the `allow_blank` option:
275
+
276
+ ```ruby
277
+ # Basic example
278
+ schema = Schemacop::Schema3.new :string, allow_blank: false
279
+ schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: String is blank but must not be blank!
280
+ schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: String is blank but must not be blank!
281
+ schema.validate!('foo') # => "foo"
282
+ schema.validate!("\n") # => Schemacop::Exceptions::ValidationError: /: String is blank but must not be blank!
283
+ ```
284
+ Example of using a `format` option:
285
+
249
286
  ```ruby
250
287
  # By using a format, string values are casted to that respective format
251
288
  schema = Schemacop::Schema3.new(:string, format: :date)
@@ -277,6 +314,10 @@ integer can be done.
277
314
  * `multiple_of`
278
315
  The received number has to be a multiple of the given number for the validation to
279
316
  pass.
317
+ * `cast_str`
318
+ When set to `true`, this node also accepts strings that can be casted to an integer, e.g.
319
+ the values `'-5'` or `'42'`. Please note that you can only validate numbers which
320
+ are in the `Integer` format.
280
321
 
281
322
  #### Examples
282
323
 
@@ -287,10 +328,24 @@ schema.validate!(42) # => 42
287
328
  schema.validate!(43) # => Schemacop::Exceptions::ValidationError: /: Value must be a multiple of 2.
288
329
  schema.validate!(-2) # => Schemacop::Exceptions::ValidationError: /: Value must have a minimum of 0.
289
330
  schema.validate!(102) # => Schemacop::Exceptions::ValidationError: /: Value must have a maximum of 100.
290
- schema.validate!(42.1) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
291
- schema.validate!(4r) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
292
- schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
293
- schema.validate!(BigDecimal(5)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
331
+ schema.validate!(42.1) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Float", expected "integer".
332
+ schema.validate!(4r) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Rational", expected "integer".
333
+ schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Complex", expected "integer".
334
+ schema.validate!(BigDecimal(5)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "BigDecimal", expected "integer".
335
+ ```
336
+
337
+ With `cast_str` enabled:
338
+
339
+ ```ruby
340
+ # Validates that the input is an even number between 0 and 100 (inclusive)
341
+ schema = Schemacop::Schema3.new(:integer, minimum: 0, maximum: 100, multiple_of: 2, cast_str: true)
342
+ schema.validate!('42') # => 42
343
+ schema.validate!('43') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
344
+ schema.validate!('-2') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
345
+ schema.validate!('102') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
346
+ schema.validate!('42.1') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
347
+ schema.validate!('4r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
348
+ schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
294
349
  ```
295
350
 
296
351
  ### Number
@@ -327,6 +382,11 @@ With the various available options, validations on the value of the number can b
327
382
  * `multiple_of`
328
383
  The received number has to be a multiple of the given number for the validation to
329
384
  pass.
385
+ * `cast_str`
386
+ When set to `true`, this node also accepts strings that can be casted to a number, e.g.
387
+ the values `'0.1'` or `'3.1415'`. Please note that you can only validate numbers which
388
+ are in the `Integer` or `Float` format, i.e. values like `'1.5r'` or `'(4 + 0i)'` will
389
+ not work.
330
390
 
331
391
  #### Examples
332
392
 
@@ -340,7 +400,20 @@ schema.validate!(51) # => Schemacop::Exceptions::ValidationError: /:
340
400
  schema.validate!(42.5) # => 42.5
341
401
  schema.validate!(1.5r) # => (3/2)
342
402
  schema.validate!(BigDecimal(5)) # => 0.5e1
343
- schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "big_decimal" or "float" or "integer" or "rational".
403
+ schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Complex", expected "big_decimal" or "float" or "integer" or "rational"
404
+ ```
405
+
406
+ With `cast_str` enabled:
407
+
408
+ ```ruby
409
+ schema = Schemacop::Schema3.new(:number, cast_str: true, minimum: 0.0, maximum: (50r), multiple_of: BigDecimal('0.5'))
410
+ schema.validate!('42') # => 42
411
+ schema.validate!('42.2') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
412
+ schema.validate!('-2') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
413
+ schema.validate!('51') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
414
+ schema.validate!('42.5') # => 42.5
415
+ schema.validate!('1.5r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
416
+ schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
344
417
  ```
345
418
 
346
419
  ### Symbol
@@ -350,15 +423,33 @@ DSL: `sym`
350
423
 
351
424
  The symbol type is used to validate elements for the Ruby `Symbol` class.
352
425
 
426
+ #### Options
427
+
428
+ * `cast_str`
429
+ When set to `true`, this node also accepts strings that can be casted to a symbol.
430
+
353
431
  #### Examples
354
432
 
355
433
  ```ruby
356
434
  # Validates that the input is a symbol
357
435
  schema = Schemacop::Schema3.new(:symbol)
358
- schema.validate!(:foo) # => :foo
359
- schema.validate!('foo') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
360
- schema.validate!(123) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
361
- schema.validate!(false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
436
+ schema.validate!(:foo) # => :foo
437
+ schema.validate!('foo') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "Symbol".
438
+ schema.validate!(123) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "Symbol".
439
+ schema.validate!(false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "FalseClass", expected "Symbol".
440
+ schema.validate!(:false) # => :false
441
+ ```
442
+
443
+ With `cast_str` enabled:
444
+
445
+ ```ruby
446
+ # Validates that the input is a symbol
447
+ schema = Schemacop::Schema3.new(:symbol, cast_str: true)
448
+ schema.validate!(':foo') # => :":foo"
449
+ schema.validate!('foo') # => :foo
450
+ schema.validate!('123') # => :"123"
451
+ schema.validate!('false') # => :false
452
+ schema.validate!(':false') # => :":false"
362
453
  ```
363
454
 
364
455
  ### Boolean
@@ -368,6 +459,12 @@ DSL: `boo`
368
459
 
369
460
  The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `FalseClass`
370
461
 
462
+ #### Options
463
+
464
+ * `cast_str`
465
+ When set to `true`, this node also accepts strings that can be casted to a boolean, i.e.
466
+ the values `'true'` and `'false'`
467
+
371
468
  #### Examples
372
469
 
373
470
  ```ruby
@@ -375,9 +472,20 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
375
472
  schema = Schemacop::Schema3.new(:boolean)
376
473
  schema.validate!(true) # => true
377
474
  schema.validate!(false) # => false
378
- schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
379
- schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
380
- schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
475
+ schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Symbol", expected "boolean".
476
+ schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "boolean".
477
+ schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "boolean".
478
+ ```
479
+
480
+ With `cast_str` enabled:
481
+
482
+ ```ruby
483
+ schema = Schemacop::Schema3.new(:boolean, cast_str: true)
484
+ schema.validate!(true) # => true
485
+ schema.validate!(false) # => false
486
+ schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
487
+ schema.validate!('false') # => false
488
+ schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
381
489
  ```
382
490
 
383
491
  ### Array
@@ -420,7 +528,7 @@ end
420
528
 
421
529
  schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}.
422
530
  schema.validate!([1, 5]) # => [1, 5]
423
- schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, expected "integer". /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}
531
+ schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, got type "String", expected "integer". /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}
424
532
  ```
425
533
 
426
534
  You can also use it with the tuple validation (see below), e.g. if you want
@@ -461,10 +569,10 @@ schema = Schemacop::Schema3.new :array do
461
569
  list :integer, minimum: 1, maximum: 5
462
570
  end
463
571
 
464
- schema.validate!([]) # => []
465
- schema.validate!([1, 3]) # => [1, 3]
466
- schema.validate!([0, 6]) # => Schemacop::Exceptions::ValidationError: /[0]: Value must have a minimum of 1. /[1]: Value must have a maximum of 5.
467
- schema.validate! ['foo'] # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, expected "integer".
572
+ schema.validate!([]) # => []
573
+ schema.validate!([1, 3]) # => [1, 3]
574
+ schema.validate!([0, 6]) # => Schemacop::Exceptions::ValidationError: /[0]: Value must have a minimum of 1. /[1]: Value must have a maximum of 5.
575
+ schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, got type "String", expected "integer".
468
576
  ```
469
577
 
470
578
  You can also build more complex structures, e.g. an array containing an arbitrary
@@ -479,7 +587,7 @@ end
479
587
 
480
588
  schema.validate!([]) # => []
481
589
  schema.validate!([[1], [2, 3]]) # => [[1], [2, 3]]
482
- schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, expected "integer".
590
+ schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, got type "String", expected "integer".
483
591
  ```
484
592
 
485
593
  Please note that you can only specify *one* `list` item:
@@ -541,7 +649,7 @@ end
541
649
 
542
650
  schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: Array has 0 items but must have exactly 2.
543
651
  schema.validate!([1, 'foo']) # => [1, "foo"]
544
- schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, expected "integer".
652
+ schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, got type "String", expected "integer".
545
653
  schema.validate!([1, 'foo', 2, 3]) # => [1, "foo", 2, 3]
546
654
  ```
547
655
 
@@ -700,7 +808,7 @@ schema = Schemacop::Schema3.new :hash do
700
808
  str? :foo
701
809
  end
702
810
 
703
- schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
811
+ schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
704
812
  schema.validate!({foo: 'bar'}) # => {"foo"=>"bar"}
705
813
  ```
706
814
 
@@ -784,7 +892,7 @@ end
784
892
 
785
893
  schema.validate!({id: 1}) # => {"id"=>1}
786
894
  schema.validate!({id: 1, foo: 'bar'}) # => {"id"=>1, "foo"=>"bar"}
787
- schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
895
+ schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
788
896
  ```
789
897
 
790
898
  Using the option `property_names`, you can additionaly specify a pattern that
@@ -808,8 +916,8 @@ end
808
916
 
809
917
  schema.validate!({}) # => {}
810
918
  schema.validate!({foo: [1, 2, 3]}) # => {"foo"=>[1, 2, 3]}
811
- schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "array".
812
- schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, expected "array".
919
+ schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Symbol", expected "array".
920
+ schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, got type "Symbol", expected "array".
813
921
  ```
814
922
 
815
923
  ##### Dependencies
@@ -883,10 +991,10 @@ of allowed classes:
883
991
  schema = Schemacop::Schema3.new :object, classes: [String]
884
992
 
885
993
  schema.validate!(nil) # => nil
886
- schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
887
- schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
994
+ schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
995
+ schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
888
996
  schema.validate!('foo') # => "foo"
889
- schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
997
+ schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "ActiveSupport::SafeBuffer", expected "String".
890
998
  ```
891
999
 
892
1000
  Here, the node checks if the given value is an instance of any of the given
@@ -898,8 +1006,8 @@ If you want to allow subclasses, you can specify this by using the `strict` opti
898
1006
  schema = Schemacop::Schema3.new :object, classes: [String], strict: false
899
1007
 
900
1008
  schema.validate!(nil) # => nil
901
- schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
902
- schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
1009
+ schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
1010
+ schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
903
1011
  schema.validate!('foo') # => "foo"
904
1012
  schema.validate!('foo'.html_safe) # => "foo"
905
1013
  ```
@@ -1072,7 +1180,7 @@ schema.validate!({
1072
1180
  shipping_address: 'foo',
1073
1181
  billing_address: 42
1074
1182
  })
1075
- # => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, expected "object". /billing_address: Invalid type, expected "object".
1183
+ # => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, got type "String", expected "object". /billing_address: Invalid type, got type "Integer", expected "object".
1076
1184
 
1077
1185
  schema.validate!({
1078
1186
  shipping_address: {
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.5
1
+ 3.0.10
@@ -14,8 +14,8 @@ module Schemacop
14
14
  root.validate(data)
15
15
  end
16
16
 
17
- def as_json(json_format: V3::Context::DEFAULT_JSON_FORMAT)
18
- Schemacop.context.spawn_with(json_format: json_format) { root.as_json }
17
+ def as_json
18
+ root.as_json
19
19
  end
20
20
  end
21
21
  end
@@ -19,6 +19,10 @@ module Schemacop
19
19
  default
20
20
  end
21
21
  end
22
+
23
+ def self.allowed_options
24
+ super + %i[cast_str]
25
+ end
22
26
  end
23
27
  end
24
28
  end
@@ -17,7 +17,7 @@ module Schemacop
17
17
  ).freeze
18
18
  end
19
19
 
20
- def spawn_with(json_format: @json_format)
20
+ def with_json_format(json_format)
21
21
  prev_json_format = @json_format
22
22
  @json_format = json_format
23
23
  return yield
@@ -48,7 +48,7 @@ module Schemacop
48
48
  end
49
49
 
50
50
  def self.allowed_options
51
- %i[name required default description examples enum parent options cast_str title as]
51
+ %i[name required default description examples enum parent options title as]
52
52
  end
53
53
 
54
54
  def self.dsl_methods
@@ -161,12 +161,13 @@ module Schemacop
161
161
  end
162
162
 
163
163
  def process_json(attrs, json)
164
- if @schemas.any?
164
+ if !context.swagger_json? && @schemas.any?
165
165
  json[:definitions] = {}
166
166
  @schemas.each do |name, subschema|
167
167
  json[:definitions][name] = subschema.as_json
168
168
  end
169
169
  end
170
+
170
171
  attrs.each do |attr|
171
172
  if options.include?(attr)
172
173
  json[attr.to_s.camelize(:lower).to_sym] = options[attr]
@@ -205,7 +206,7 @@ module Schemacop
205
206
  # Validate type #
206
207
  if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
207
208
  collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
208
- result.error %(Invalid type, expected #{collection}.)
209
+ result.error "Invalid type, got type \"#{data.class}\", expected #{collection}."
209
210
  return nil
210
211
  end
211
212