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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86a824c8ccca5369721dff7f7ccebb581cb34685a31aa50126444a92593ee20c
|
4
|
+
data.tar.gz: a336258896003d683a4b9b3d5c8a1a7e5070c1cef06eb4975ba0ba8d872c8f0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4faf09edddbbd5fa892ee9b7f4dc75a0e551744e625f2b8552fe68678113abfd07dc2b8f3344f0d75c7f37aa84281b7c37195f8dd9aa70b2b89e5742a98452c
|
7
|
+
data.tar.gz: 2f07ee47aba8a6147a6088f7f3fe6e0c35168b9a59ee540c00cd3f8693104b74e74227fa68c96604410098d774ea9774c50f135f20a51448f349b8b297d4a2f1
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
fail-fast: false
|
14
|
+
matrix:
|
15
|
+
ruby-version: ['2.6.2', '2.7.1', '3.0.1']
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- name: Set up Ruby
|
20
|
+
uses: ruby/setup-ruby@v1
|
21
|
+
with:
|
22
|
+
ruby-version: ${{ matrix.ruby-version }}
|
23
|
+
bundler-cache: true
|
24
|
+
- name: Run rake tests
|
25
|
+
run: bundle exec rake test
|
26
|
+
- name: Run rubocop
|
27
|
+
run: bundle exec rubocop
|
data/.releaser_config
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## 3.0.13 (2021-10-04)
|
4
|
+
|
5
|
+
* When using `boolean` with `cast_str: true`, `"0"` is now casted to `false` and
|
6
|
+
`"1"` to `true`. This in addition to already casting `"true", "false"`.
|
7
|
+
|
8
|
+
## 3.0.12 (2021-09-14)
|
9
|
+
|
10
|
+
* Fix compatibility issue with `ruby <= 2.5`
|
11
|
+
|
12
|
+
## 3.0.11 (2021-03-26)
|
13
|
+
|
14
|
+
* Treat blank string as nil when using `cast_str` option
|
15
|
+
|
16
|
+
## 3.0.10 (2021-03-19)
|
17
|
+
|
18
|
+
* If wrong type is given, report class of wrong type in error message
|
19
|
+
|
3
20
|
## 3.0.9 (2021-02-26)
|
4
21
|
|
5
22
|
* Fix casting of blank strings
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://github.com/sitrox/schemacop/actions/workflows/ruby.yml)
|
2
2
|
[](https://badge.fury.io/rb/schemacop)
|
3
3
|
|
4
4
|
# Schemacop
|
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
|
@@ -233,8 +233,8 @@ transformed into various types.
|
|
233
233
|
addresses do not have their own ruby type.
|
234
234
|
|
235
235
|
* `boolean`
|
236
|
-
The string must be either `true` or `
|
237
|
-
Ruby's `TrueClass` or `FalseClass`.
|
236
|
+
The string must be either `true`, `false`, `0` or `1`. This value will be
|
237
|
+
casted to Ruby's `TrueClass` or `FalseClass`.
|
238
238
|
|
239
239
|
* `binary`
|
240
240
|
The string is expected to contain binary contents. No casting or additional
|
@@ -317,7 +317,7 @@ integer can be done.
|
|
317
317
|
* `cast_str`
|
318
318
|
When set to `true`, this node also accepts strings that can be casted to an integer, e.g.
|
319
319
|
the values `'-5'` or `'42'`. Please note that you can only validate numbers which
|
320
|
-
are in the `Integer` format.
|
320
|
+
are in the `Integer` format. Blank strings will be treated equally as `nil`.
|
321
321
|
|
322
322
|
#### Examples
|
323
323
|
|
@@ -328,10 +328,10 @@ schema.validate!(42) # => 42
|
|
328
328
|
schema.validate!(43) # => Schemacop::Exceptions::ValidationError: /: Value must be a multiple of 2.
|
329
329
|
schema.validate!(-2) # => Schemacop::Exceptions::ValidationError: /: Value must have a minimum of 0.
|
330
330
|
schema.validate!(102) # => Schemacop::Exceptions::ValidationError: /: Value must have a maximum of 100.
|
331
|
-
schema.validate!(42.1) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
332
|
-
schema.validate!(4r) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
333
|
-
schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
334
|
-
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
335
|
```
|
336
336
|
|
337
337
|
With `cast_str` enabled:
|
@@ -346,6 +346,19 @@ schema.validate!('102') # => Schemacop::Exceptions::ValidationError: /
|
|
346
346
|
schema.validate!('42.1') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
347
347
|
schema.validate!('4r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
348
348
|
schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
349
|
+
schema.validate!(nil) # => nil
|
350
|
+
schema.validate!('') # => nil
|
351
|
+
```
|
352
|
+
|
353
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
354
|
+
and validating a blank string will return `nil`.
|
355
|
+
If you need a value, use the `required` option:
|
356
|
+
|
357
|
+
```ruby
|
358
|
+
schema = Schemacop::Schema3.new(:integer, minimum: 0, maximum: 100, multiple_of: 2, cast_str: true, required: true)
|
359
|
+
schema.validate!('42') # => 42
|
360
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
361
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
349
362
|
```
|
350
363
|
|
351
364
|
### Number
|
@@ -386,7 +399,7 @@ With the various available options, validations on the value of the number can b
|
|
386
399
|
When set to `true`, this node also accepts strings that can be casted to a number, e.g.
|
387
400
|
the values `'0.1'` or `'3.1415'`. Please note that you can only validate numbers which
|
388
401
|
are in the `Integer` or `Float` format, i.e. values like `'1.5r'` or `'(4 + 0i)'` will
|
389
|
-
not work.
|
402
|
+
not work. Blank strings will be treated equally as `nil`.
|
390
403
|
|
391
404
|
#### Examples
|
392
405
|
|
@@ -400,7 +413,7 @@ schema.validate!(51) # => Schemacop::Exceptions::ValidationError: /:
|
|
400
413
|
schema.validate!(42.5) # => 42.5
|
401
414
|
schema.validate!(1.5r) # => (3/2)
|
402
415
|
schema.validate!(BigDecimal(5)) # => 0.5e1
|
403
|
-
schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "big_decimal" or "float" or "integer" or "rational"
|
416
|
+
schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Complex", expected "big_decimal" or "float" or "integer" or "rational"
|
404
417
|
```
|
405
418
|
|
406
419
|
With `cast_str` enabled:
|
@@ -414,6 +427,19 @@ schema.validate!('51') # => Schemacop::Exceptions::ValidationError: /: Ma
|
|
414
427
|
schema.validate!('42.5') # => 42.5
|
415
428
|
schema.validate!('1.5r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
416
429
|
schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
430
|
+
schema.validate!(nil) # => nil
|
431
|
+
schema.validate!('') # => nil
|
432
|
+
```
|
433
|
+
|
434
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
435
|
+
and validating a blank string will return `nil`.
|
436
|
+
If you need a value, use the `required` option:
|
437
|
+
|
438
|
+
```ruby
|
439
|
+
schema = Schemacop::Schema3.new(:number, cast_str: true, minimum: 0.0, maximum: (50r), multiple_of: BigDecimal('0.5'), require: true)
|
440
|
+
schema.validate!('42.5') # => 42.5
|
441
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
442
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
417
443
|
```
|
418
444
|
|
419
445
|
### Symbol
|
@@ -426,7 +452,7 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
|
|
426
452
|
#### Options
|
427
453
|
|
428
454
|
* `cast_str`
|
429
|
-
When set to `true`, this node also accepts strings that can be casted to a symbol.
|
455
|
+
When set to `true`, this node also accepts strings that can be casted to a symbol. Blank strings will be treated equally as `nil`.
|
430
456
|
|
431
457
|
#### Examples
|
432
458
|
|
@@ -434,9 +460,9 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
|
|
434
460
|
# Validates that the input is a symbol
|
435
461
|
schema = Schemacop::Schema3.new(:symbol)
|
436
462
|
schema.validate!(:foo) # => :foo
|
437
|
-
schema.validate!('foo') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
|
438
|
-
schema.validate!(123) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
|
439
|
-
schema.validate!(false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
|
463
|
+
schema.validate!('foo') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "Symbol".
|
464
|
+
schema.validate!(123) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "Symbol".
|
465
|
+
schema.validate!(false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "FalseClass", expected "Symbol".
|
440
466
|
schema.validate!(:false) # => :false
|
441
467
|
```
|
442
468
|
|
@@ -450,6 +476,19 @@ schema.validate!('foo') # => :foo
|
|
450
476
|
schema.validate!('123') # => :"123"
|
451
477
|
schema.validate!('false') # => :false
|
452
478
|
schema.validate!(':false') # => :":false"
|
479
|
+
schema.validate!(nil) # => nil
|
480
|
+
schema.validate!('') # => nil
|
481
|
+
```
|
482
|
+
|
483
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
484
|
+
and validating a blank string will return `nil`.
|
485
|
+
If you need a value, use the `required` option:
|
486
|
+
|
487
|
+
```ruby
|
488
|
+
schema = Schemacop::Schema3.new(:symbol, cast_str: true, required: true)
|
489
|
+
schema.validate!('foo') # => :foo
|
490
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
491
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
453
492
|
```
|
454
493
|
|
455
494
|
### Boolean
|
@@ -462,8 +501,9 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
462
501
|
#### Options
|
463
502
|
|
464
503
|
* `cast_str`
|
465
|
-
When set to `true`, this node also accepts strings that can be casted to a
|
466
|
-
the values `'true'` and `'
|
504
|
+
When set to `true`, this node also accepts strings that can be casted to a
|
505
|
+
boolean, namely the values `'true'`, `'false'`, `'1'` and `'0'`. Blank strings
|
506
|
+
will be treated equally as `nil`.
|
467
507
|
|
468
508
|
#### Examples
|
469
509
|
|
@@ -472,9 +512,14 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
472
512
|
schema = Schemacop::Schema3.new(:boolean)
|
473
513
|
schema.validate!(true) # => true
|
474
514
|
schema.validate!(false) # => false
|
475
|
-
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
476
|
-
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
477
|
-
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
515
|
+
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Symbol", expected "boolean".
|
516
|
+
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "boolean".
|
517
|
+
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "boolean".
|
518
|
+
|
519
|
+
schema.validate!('0', cast_str: true) # => false
|
520
|
+
schema.validate!('1', cast_str: true) # => true
|
521
|
+
schema.validate!('false', cast_str: true) # => false
|
522
|
+
schema.validate!('true', cast_str: true) # => true
|
478
523
|
```
|
479
524
|
|
480
525
|
With `cast_str` enabled:
|
@@ -486,6 +531,19 @@ schema.validate!(false) # => false
|
|
486
531
|
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
487
532
|
schema.validate!('false') # => false
|
488
533
|
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
534
|
+
schema.validate!(nil) # => nil
|
535
|
+
schema.validate!('') # => nil
|
536
|
+
```
|
537
|
+
|
538
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
539
|
+
and validating a blank string will return `nil`.
|
540
|
+
If you need a value, use the `required` option:
|
541
|
+
|
542
|
+
```ruby
|
543
|
+
schema = Schemacop::Schema3.new(:boolean, cast_str: true, required: true)
|
544
|
+
schema.validate!('false') # => false
|
545
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
546
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
489
547
|
```
|
490
548
|
|
491
549
|
### Array
|
@@ -528,7 +586,7 @@ end
|
|
528
586
|
|
529
587
|
schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}.
|
530
588
|
schema.validate!([1, 5]) # => [1, 5]
|
531
|
-
schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, expected "integer". /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}
|
589
|
+
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}
|
532
590
|
```
|
533
591
|
|
534
592
|
You can also use it with the tuple validation (see below), e.g. if you want
|
@@ -569,10 +627,10 @@ schema = Schemacop::Schema3.new :array do
|
|
569
627
|
list :integer, minimum: 1, maximum: 5
|
570
628
|
end
|
571
629
|
|
572
|
-
schema.validate!([])
|
573
|
-
schema.validate!([1, 3])
|
574
|
-
schema.validate!([0, 6])
|
575
|
-
schema.validate!
|
630
|
+
schema.validate!([]) # => []
|
631
|
+
schema.validate!([1, 3]) # => [1, 3]
|
632
|
+
schema.validate!([0, 6]) # => Schemacop::Exceptions::ValidationError: /[0]: Value must have a minimum of 1. /[1]: Value must have a maximum of 5.
|
633
|
+
schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, got type "String", expected "integer".
|
576
634
|
```
|
577
635
|
|
578
636
|
You can also build more complex structures, e.g. an array containing an arbitrary
|
@@ -587,7 +645,7 @@ end
|
|
587
645
|
|
588
646
|
schema.validate!([]) # => []
|
589
647
|
schema.validate!([[1], [2, 3]]) # => [[1], [2, 3]]
|
590
|
-
schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, expected "integer".
|
648
|
+
schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, got type "String", expected "integer".
|
591
649
|
```
|
592
650
|
|
593
651
|
Please note that you can only specify *one* `list` item:
|
@@ -649,7 +707,7 @@ end
|
|
649
707
|
|
650
708
|
schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: Array has 0 items but must have exactly 2.
|
651
709
|
schema.validate!([1, 'foo']) # => [1, "foo"]
|
652
|
-
schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, expected "integer".
|
710
|
+
schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, got type "String", expected "integer".
|
653
711
|
schema.validate!([1, 'foo', 2, 3]) # => [1, "foo", 2, 3]
|
654
712
|
```
|
655
713
|
|
@@ -808,7 +866,7 @@ schema = Schemacop::Schema3.new :hash do
|
|
808
866
|
str? :foo
|
809
867
|
end
|
810
868
|
|
811
|
-
schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
|
869
|
+
schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
|
812
870
|
schema.validate!({foo: 'bar'}) # => {"foo"=>"bar"}
|
813
871
|
```
|
814
872
|
|
@@ -892,7 +950,7 @@ end
|
|
892
950
|
|
893
951
|
schema.validate!({id: 1}) # => {"id"=>1}
|
894
952
|
schema.validate!({id: 1, foo: 'bar'}) # => {"id"=>1, "foo"=>"bar"}
|
895
|
-
schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
|
953
|
+
schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
|
896
954
|
```
|
897
955
|
|
898
956
|
Using the option `property_names`, you can additionaly specify a pattern that
|
@@ -916,8 +974,8 @@ end
|
|
916
974
|
|
917
975
|
schema.validate!({}) # => {}
|
918
976
|
schema.validate!({foo: [1, 2, 3]}) # => {"foo"=>[1, 2, 3]}
|
919
|
-
schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "array".
|
920
|
-
schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, expected "array".
|
977
|
+
schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Symbol", expected "array".
|
978
|
+
schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, got type "Symbol", expected "array".
|
921
979
|
```
|
922
980
|
|
923
981
|
##### Dependencies
|
@@ -991,10 +1049,10 @@ of allowed classes:
|
|
991
1049
|
schema = Schemacop::Schema3.new :object, classes: [String]
|
992
1050
|
|
993
1051
|
schema.validate!(nil) # => nil
|
994
|
-
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
995
|
-
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1052
|
+
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
|
1053
|
+
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
|
996
1054
|
schema.validate!('foo') # => "foo"
|
997
|
-
schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1055
|
+
schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "ActiveSupport::SafeBuffer", expected "String".
|
998
1056
|
```
|
999
1057
|
|
1000
1058
|
Here, the node checks if the given value is an instance of any of the given
|
@@ -1006,8 +1064,8 @@ If you want to allow subclasses, you can specify this by using the `strict` opti
|
|
1006
1064
|
schema = Schemacop::Schema3.new :object, classes: [String], strict: false
|
1007
1065
|
|
1008
1066
|
schema.validate!(nil) # => nil
|
1009
|
-
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1010
|
-
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1067
|
+
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
|
1068
|
+
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
|
1011
1069
|
schema.validate!('foo') # => "foo"
|
1012
1070
|
schema.validate!('foo'.html_safe) # => "foo"
|
1013
1071
|
```
|
@@ -1180,7 +1238,7 @@ schema.validate!({
|
|
1180
1238
|
shipping_address: 'foo',
|
1181
1239
|
billing_address: 42
|
1182
1240
|
})
|
1183
|
-
# => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, expected "object". /billing_address: Invalid type, expected "object".
|
1241
|
+
# => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, got type "String", expected "object". /billing_address: Invalid type, got type "Integer", expected "object".
|
1184
1242
|
|
1185
1243
|
schema.validate!({
|
1186
1244
|
shipping_address: {
|
@@ -1289,7 +1347,8 @@ files. This is especially useful is you have schemas in your application which
|
|
1289
1347
|
are used multiple times throughout the application.
|
1290
1348
|
|
1291
1349
|
For each schema, you define the schema in a separate file, and after loading the
|
1292
|
-
schemas, you can reference them in other schemas.
|
1350
|
+
schemas, you can reference them in other schemas. The schema can be retrieved
|
1351
|
+
by using the file name, e.g. `user` in the example `app/schemas/user.rb` below.
|
1293
1352
|
|
1294
1353
|
The default load path is `'app/schemas'`, but this can be configured by setting
|
1295
1354
|
the value of the `load_paths` attribute of the `Schemacop` module.
|
@@ -1306,7 +1365,7 @@ Where:
|
|
1306
1365
|
* context schemas: Defined in the current context using `context.schema`
|
1307
1366
|
* global schemas: Defined in a ruby file in the load path
|
1308
1367
|
|
1309
|
-
### Rails applications
|
1368
|
+
### External schemas in Rails applications
|
1310
1369
|
|
1311
1370
|
In Rails applications, your schemas are automatically eager-loaded from the load
|
1312
1371
|
path `'app/schemas'` when your application is started, unless your application
|
@@ -1333,7 +1392,7 @@ end
|
|
1333
1392
|
```
|
1334
1393
|
|
1335
1394
|
```ruby
|
1336
|
-
# app/schemas/nested/
|
1395
|
+
# app/schemas/nested/group.rb
|
1337
1396
|
schema :hash do
|
1338
1397
|
str! :name
|
1339
1398
|
end
|
@@ -1357,7 +1416,7 @@ schema.validate!({usr: {first_name: 'Joe', last_name: 'Doe', groups: [{name: 'fo
|
|
1357
1416
|
# => {"usr"=>{"first_name"=>"Joe", "last_name"=>"Doe", "groups"=>[{"name"=>"foo"}, {"name"=>"bar"}]}}
|
1358
1417
|
```
|
1359
1418
|
|
1360
|
-
### Non-Rails applications
|
1419
|
+
### External schemas in Non-Rails applications
|
1361
1420
|
|
1362
1421
|
Usage in non-Rails applications is the same as with usage in Rails applications,
|
1363
1422
|
however you might need to eager load the schemas yourself:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.
|
1
|
+
3.0.13
|
data/lib/schemacop/v3/node.rb
CHANGED
@@ -33,10 +33,11 @@ module Schemacop
|
|
33
33
|
if options.delete(:cast_str)
|
34
34
|
format = NodeRegistry.name(klass)
|
35
35
|
one_of_options = {
|
36
|
-
required:
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
required: options.delete(:required),
|
37
|
+
treat_blank_as_nil: true,
|
38
|
+
name: options.delete(:name),
|
39
|
+
as: options.delete(:as),
|
40
|
+
description: options.delete(:description)
|
40
41
|
}
|
41
42
|
node = create(:one_of, **one_of_options) do
|
42
43
|
self.node node
|
@@ -206,7 +207,7 @@ module Schemacop
|
|
206
207
|
# Validate type #
|
207
208
|
if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
|
208
209
|
collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
|
209
|
-
result.error
|
210
|
+
result.error "Invalid type, got type \"#{data.class}\", expected #{collection}."
|
210
211
|
return nil
|
211
212
|
end
|
212
213
|
|
@@ -5,8 +5,30 @@ module Schemacop
|
|
5
5
|
:oneOf
|
6
6
|
end
|
7
7
|
|
8
|
+
def self.allowed_options
|
9
|
+
super + %i[treat_blank_as_nil]
|
10
|
+
end
|
11
|
+
|
12
|
+
def cast(value)
|
13
|
+
item = match(value)
|
14
|
+
|
15
|
+
unless item
|
16
|
+
if options[:treat_blank_as_nil] && value.blank? && !value.is_a?(FalseClass)
|
17
|
+
return nil
|
18
|
+
else
|
19
|
+
return value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
return item.cast(value)
|
24
|
+
end
|
25
|
+
|
8
26
|
def _validate(data, result:)
|
9
|
-
|
27
|
+
if options[:treat_blank_as_nil] && data.blank? && !data.is_a?(FalseClass)
|
28
|
+
data = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
super_data = super(data, result: result)
|
10
32
|
return if super_data.nil?
|
11
33
|
|
12
34
|
matches = matches(super_data)
|
@@ -13,7 +13,7 @@ module Schemacop
|
|
13
13
|
'date-time': /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
|
14
14
|
time: /^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
|
15
15
|
email: URI::MailTo::EMAIL_REGEXP,
|
16
|
-
boolean: /^(true|false)$/,
|
16
|
+
boolean: /^(true|false|0|1)$/,
|
17
17
|
binary: nil,
|
18
18
|
symbol: nil,
|
19
19
|
integer: /^-?[0-9]+$/,
|
@@ -92,7 +92,7 @@ module Schemacop
|
|
92
92
|
|
93
93
|
case options[:format]
|
94
94
|
when :boolean
|
95
|
-
return to_cast == 'true'
|
95
|
+
return to_cast == 'true' || to_cast == '1'
|
96
96
|
when :date
|
97
97
|
return Date.parse(to_cast)
|
98
98
|
when :'date-time'
|
data/schemacop.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: schemacop 3.0.
|
2
|
+
# stub: schemacop 3.0.13 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "schemacop".freeze
|
6
|
-
s.version = "3.0.
|
6
|
+
s.version = "3.0.13"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib".freeze]
|
10
10
|
s.authors = ["Sitrox".freeze]
|
11
|
-
s.date = "2021-
|
12
|
-
s.files = [".
|
11
|
+
s.date = "2021-10-04"
|
12
|
+
s.files = [".github/workflows/ruby.yml".freeze, ".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "README_V2.md".freeze, "README_V3.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/base_schema.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/railtie.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/schema2.rb".freeze, "lib/schemacop/schema3.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/v2.rb".freeze, "lib/schemacop/v2/caster.rb".freeze, "lib/schemacop/v2/collector.rb".freeze, "lib/schemacop/v2/dupper.rb".freeze, "lib/schemacop/v2/field_node.rb".freeze, "lib/schemacop/v2/node.rb".freeze, "lib/schemacop/v2/node_resolver.rb".freeze, "lib/schemacop/v2/node_supporting_field.rb".freeze, "lib/schemacop/v2/node_supporting_type.rb".freeze, "lib/schemacop/v2/node_with_block.rb".freeze, "lib/schemacop/v2/validator/array_validator.rb".freeze, "lib/schemacop/v2/validator/boolean_validator.rb".freeze, "lib/schemacop/v2/validator/float_validator.rb".freeze, "lib/schemacop/v2/validator/hash_validator.rb".freeze, "lib/schemacop/v2/validator/integer_validator.rb".freeze, "lib/schemacop/v2/validator/nil_validator.rb".freeze, "lib/schemacop/v2/validator/number_validator.rb".freeze, "lib/schemacop/v2/validator/object_validator.rb".freeze, "lib/schemacop/v2/validator/string_validator.rb".freeze, "lib/schemacop/v2/validator/symbol_validator.rb".freeze, "lib/schemacop/v3.rb".freeze, "lib/schemacop/v3/all_of_node.rb".freeze, "lib/schemacop/v3/any_of_node.rb".freeze, "lib/schemacop/v3/array_node.rb".freeze, "lib/schemacop/v3/boolean_node.rb".freeze, "lib/schemacop/v3/combination_node.rb".freeze, "lib/schemacop/v3/context.rb".freeze, "lib/schemacop/v3/dsl_scope.rb".freeze, "lib/schemacop/v3/global_context.rb".freeze, "lib/schemacop/v3/hash_node.rb".freeze, "lib/schemacop/v3/integer_node.rb".freeze, "lib/schemacop/v3/is_not_node.rb".freeze, "lib/schemacop/v3/node.rb".freeze, "lib/schemacop/v3/node_registry.rb".freeze, "lib/schemacop/v3/number_node.rb".freeze, "lib/schemacop/v3/numeric_node.rb".freeze, "lib/schemacop/v3/object_node.rb".freeze, "lib/schemacop/v3/one_of_node.rb".freeze, "lib/schemacop/v3/reference_node.rb".freeze, "lib/schemacop/v3/result.rb".freeze, "lib/schemacop/v3/string_node.rb".freeze, "lib/schemacop/v3/symbol_node.rb".freeze, "schemacop.gemspec".freeze, "test/lib/test_helper.rb".freeze, "test/schemas/nested/group.rb".freeze, "test/schemas/user.rb".freeze, "test/unit/schemacop/v2/casting_test.rb".freeze, "test/unit/schemacop/v2/collector_test.rb".freeze, "test/unit/schemacop/v2/custom_check_test.rb".freeze, "test/unit/schemacop/v2/custom_if_test.rb".freeze, "test/unit/schemacop/v2/defaults_test.rb".freeze, "test/unit/schemacop/v2/empty_test.rb".freeze, "test/unit/schemacop/v2/nil_dis_allow_test.rb".freeze, "test/unit/schemacop/v2/node_resolver_test.rb".freeze, "test/unit/schemacop/v2/short_forms_test.rb".freeze, "test/unit/schemacop/v2/types_test.rb".freeze, "test/unit/schemacop/v2/validator_array_test.rb".freeze, "test/unit/schemacop/v2/validator_boolean_test.rb".freeze, "test/unit/schemacop/v2/validator_float_test.rb".freeze, "test/unit/schemacop/v2/validator_hash_test.rb".freeze, "test/unit/schemacop/v2/validator_integer_test.rb".freeze, "test/unit/schemacop/v2/validator_nil_test.rb".freeze, "test/unit/schemacop/v2/validator_number_test.rb".freeze, "test/unit/schemacop/v2/validator_object_test.rb".freeze, "test/unit/schemacop/v2/validator_string_test.rb".freeze, "test/unit/schemacop/v2/validator_symbol_test.rb".freeze, "test/unit/schemacop/v3/all_of_node_test.rb".freeze, "test/unit/schemacop/v3/any_of_node_test.rb".freeze, "test/unit/schemacop/v3/array_node_test.rb".freeze, "test/unit/schemacop/v3/boolean_node_test.rb".freeze, "test/unit/schemacop/v3/global_context_test.rb".freeze, "test/unit/schemacop/v3/hash_node_test.rb".freeze, "test/unit/schemacop/v3/integer_node_test.rb".freeze, "test/unit/schemacop/v3/is_not_node_test.rb".freeze, "test/unit/schemacop/v3/node_test.rb".freeze, "test/unit/schemacop/v3/number_node_test.rb".freeze, "test/unit/schemacop/v3/object_node_test.rb".freeze, "test/unit/schemacop/v3/one_of_node_test.rb".freeze, "test/unit/schemacop/v3/reference_node_test.rb".freeze, "test/unit/schemacop/v3/string_node_test.rb".freeze, "test/unit/schemacop/v3/symbol_node_test.rb".freeze]
|
13
13
|
s.homepage = "https://github.com/sitrox/schemacop".freeze
|
14
14
|
s.licenses = ["MIT".freeze]
|
15
15
|
s.rubygems_version = "3.0.3".freeze
|