schemacop 3.0.8 → 3.0.12
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 +18 -0
- data/README.md +1 -1
- data/README_V3.md +129 -40
- 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 +8 -2
- data/schemacop.gemspec +30 -18
- data/test/unit/schemacop/v3/array_node_test.rb +19 -17
- data/test/unit/schemacop/v3/boolean_node_test.rb +40 -7
- 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 +60 -11
- data/test/unit/schemacop/v3/symbol_node_test.rb +9 -6
- metadata +4 -4
- 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: b89073617fde4588d3cdf44b86c76be5b07c23cf9036c2f44c8b9599dda73779
|
4
|
+
data.tar.gz: 5d3860807fc84a14043dda84a2bdf7fc5696668171b2bc5560607f3de14be242
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63a492f2523c049f3a720ca9525d72ddf15e76a3e092dcb3b295af118fe2a1cd1e0cc92c8dffb4c20f1e5e7b98c466efde3d96dd26180279ffad9ab27157948f
|
7
|
+
data.tar.gz: f06fd83b902c3fa67ef554fd47930b39dbdfa77f57e753d14b74b88b23fe32da5043cdebc563d4296e80af53118505420f3f0d6d88f122554891fa8e2e3ca048
|
@@ -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,23 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## 3.0.12 (2021-09-14)
|
4
|
+
|
5
|
+
* Fix compatibility issue with `ruby <= 2.5`
|
6
|
+
|
7
|
+
## 3.0.11 (2021-03-26)
|
8
|
+
|
9
|
+
* Treat blank string as nil when using `cast_str` option
|
10
|
+
|
11
|
+
## 3.0.10 (2021-03-19)
|
12
|
+
|
13
|
+
* If wrong type is given, report class of wrong type in error message
|
14
|
+
|
15
|
+
## 3.0.9 (2021-02-26)
|
16
|
+
|
17
|
+
* Fix casting of blank strings
|
18
|
+
|
19
|
+
* Add `allow_blank` option to `String` nodes
|
20
|
+
|
3
21
|
## 3.0.8 (2021-02-23)
|
4
22
|
|
5
23
|
* Fix `object` nodes in hashes with no classes
|
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
|
@@ -208,6 +208,10 @@ transformed into various types.
|
|
208
208
|
string values that are commonly used. See section *formats* for more
|
209
209
|
information on the available formats. Note that strings with a format are also
|
210
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`.
|
211
215
|
|
212
216
|
#### Formats
|
213
217
|
|
@@ -247,6 +251,38 @@ transformed into various types.
|
|
247
251
|
|
248
252
|
#### Examples
|
249
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
|
+
|
250
286
|
```ruby
|
251
287
|
# By using a format, string values are casted to that respective format
|
252
288
|
schema = Schemacop::Schema3.new(:string, format: :date)
|
@@ -281,7 +317,7 @@ integer can be done.
|
|
281
317
|
* `cast_str`
|
282
318
|
When set to `true`, this node also accepts strings that can be casted to an integer, e.g.
|
283
319
|
the values `'-5'` or `'42'`. Please note that you can only validate numbers which
|
284
|
-
are in the `Integer` format.
|
320
|
+
are in the `Integer` format. Blank strings will be treated equally as `nil`.
|
285
321
|
|
286
322
|
#### Examples
|
287
323
|
|
@@ -292,10 +328,10 @@ schema.validate!(42) # => 42
|
|
292
328
|
schema.validate!(43) # => Schemacop::Exceptions::ValidationError: /: Value must be a multiple of 2.
|
293
329
|
schema.validate!(-2) # => Schemacop::Exceptions::ValidationError: /: Value must have a minimum of 0.
|
294
330
|
schema.validate!(102) # => Schemacop::Exceptions::ValidationError: /: Value must have a maximum of 100.
|
295
|
-
schema.validate!(42.1) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
296
|
-
schema.validate!(4r) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
297
|
-
schema.validate!((4 + 0i)) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "integer".
|
298
|
-
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".
|
299
335
|
```
|
300
336
|
|
301
337
|
With `cast_str` enabled:
|
@@ -310,6 +346,19 @@ schema.validate!('102') # => Schemacop::Exceptions::ValidationError: /
|
|
310
346
|
schema.validate!('42.1') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
311
347
|
schema.validate!('4r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
312
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.
|
313
362
|
```
|
314
363
|
|
315
364
|
### Number
|
@@ -350,7 +399,7 @@ With the various available options, validations on the value of the number can b
|
|
350
399
|
When set to `true`, this node also accepts strings that can be casted to a number, e.g.
|
351
400
|
the values `'0.1'` or `'3.1415'`. Please note that you can only validate numbers which
|
352
401
|
are in the `Integer` or `Float` format, i.e. values like `'1.5r'` or `'(4 + 0i)'` will
|
353
|
-
not work.
|
402
|
+
not work. Blank strings will be treated equally as `nil`.
|
354
403
|
|
355
404
|
#### Examples
|
356
405
|
|
@@ -364,7 +413,7 @@ schema.validate!(51) # => Schemacop::Exceptions::ValidationError: /:
|
|
364
413
|
schema.validate!(42.5) # => 42.5
|
365
414
|
schema.validate!(1.5r) # => (3/2)
|
366
415
|
schema.validate!(BigDecimal(5)) # => 0.5e1
|
367
|
-
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"
|
368
417
|
```
|
369
418
|
|
370
419
|
With `cast_str` enabled:
|
@@ -378,6 +427,19 @@ schema.validate!('51') # => Schemacop::Exceptions::ValidationError: /: Ma
|
|
378
427
|
schema.validate!('42.5') # => 42.5
|
379
428
|
schema.validate!('1.5r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
380
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.
|
381
443
|
```
|
382
444
|
|
383
445
|
### Symbol
|
@@ -390,7 +452,7 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
|
|
390
452
|
#### Options
|
391
453
|
|
392
454
|
* `cast_str`
|
393
|
-
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`.
|
394
456
|
|
395
457
|
#### Examples
|
396
458
|
|
@@ -398,9 +460,9 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
|
|
398
460
|
# Validates that the input is a symbol
|
399
461
|
schema = Schemacop::Schema3.new(:symbol)
|
400
462
|
schema.validate!(:foo) # => :foo
|
401
|
-
schema.validate!('foo') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
|
402
|
-
schema.validate!(123) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "Symbol".
|
403
|
-
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".
|
404
466
|
schema.validate!(:false) # => :false
|
405
467
|
```
|
406
468
|
|
@@ -414,6 +476,19 @@ schema.validate!('foo') # => :foo
|
|
414
476
|
schema.validate!('123') # => :"123"
|
415
477
|
schema.validate!('false') # => :false
|
416
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.
|
417
492
|
```
|
418
493
|
|
419
494
|
### Boolean
|
@@ -427,7 +502,7 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
427
502
|
|
428
503
|
* `cast_str`
|
429
504
|
When set to `true`, this node also accepts strings that can be casted to a boolean, i.e.
|
430
|
-
the values `'true'` and `'false'`
|
505
|
+
the values `'true'` and `'false'`. Blank strings will be treated equally as `nil`.
|
431
506
|
|
432
507
|
#### Examples
|
433
508
|
|
@@ -436,9 +511,9 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
436
511
|
schema = Schemacop::Schema3.new(:boolean)
|
437
512
|
schema.validate!(true) # => true
|
438
513
|
schema.validate!(false) # => false
|
439
|
-
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
440
|
-
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
441
|
-
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "boolean".
|
514
|
+
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Symbol", expected "boolean".
|
515
|
+
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "boolean".
|
516
|
+
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "boolean".
|
442
517
|
```
|
443
518
|
|
444
519
|
With `cast_str` enabled:
|
@@ -450,6 +525,19 @@ schema.validate!(false) # => false
|
|
450
525
|
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
451
526
|
schema.validate!('false') # => false
|
452
527
|
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
528
|
+
schema.validate!(nil) # => nil
|
529
|
+
schema.validate!('') # => nil
|
530
|
+
```
|
531
|
+
|
532
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
533
|
+
and validating a blank string will return `nil`.
|
534
|
+
If you need a value, use the `required` option:
|
535
|
+
|
536
|
+
```ruby
|
537
|
+
schema = Schemacop::Schema3.new(:boolean, cast_str: true, required: true)
|
538
|
+
schema.validate!('false') # => false
|
539
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
540
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
453
541
|
```
|
454
542
|
|
455
543
|
### Array
|
@@ -492,7 +580,7 @@ end
|
|
492
580
|
|
493
581
|
schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}.
|
494
582
|
schema.validate!([1, 5]) # => [1, 5]
|
495
|
-
schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, expected "integer". /: At least one entry must match schema {"type"=>"integer", "minimum"=>5}
|
583
|
+
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}
|
496
584
|
```
|
497
585
|
|
498
586
|
You can also use it with the tuple validation (see below), e.g. if you want
|
@@ -533,10 +621,10 @@ schema = Schemacop::Schema3.new :array do
|
|
533
621
|
list :integer, minimum: 1, maximum: 5
|
534
622
|
end
|
535
623
|
|
536
|
-
schema.validate!([])
|
537
|
-
schema.validate!([1, 3])
|
538
|
-
schema.validate!([0, 6])
|
539
|
-
schema.validate!
|
624
|
+
schema.validate!([]) # => []
|
625
|
+
schema.validate!([1, 3]) # => [1, 3]
|
626
|
+
schema.validate!([0, 6]) # => Schemacop::Exceptions::ValidationError: /[0]: Value must have a minimum of 1. /[1]: Value must have a maximum of 5.
|
627
|
+
schema.validate!(['foo']) # => Schemacop::Exceptions::ValidationError: /[0]: Invalid type, got type "String", expected "integer".
|
540
628
|
```
|
541
629
|
|
542
630
|
You can also build more complex structures, e.g. an array containing an arbitrary
|
@@ -551,7 +639,7 @@ end
|
|
551
639
|
|
552
640
|
schema.validate!([]) # => []
|
553
641
|
schema.validate!([[1], [2, 3]]) # => [[1], [2, 3]]
|
554
|
-
schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, expected "integer".
|
642
|
+
schema.validate!([['foo'], [2, 3]]) # => Schemacop::Exceptions::ValidationError: /[0]/[0]: Invalid type, got type "String", expected "integer".
|
555
643
|
```
|
556
644
|
|
557
645
|
Please note that you can only specify *one* `list` item:
|
@@ -613,7 +701,7 @@ end
|
|
613
701
|
|
614
702
|
schema.validate!([]) # => Schemacop::Exceptions::ValidationError: /: Array has 0 items but must have exactly 2.
|
615
703
|
schema.validate!([1, 'foo']) # => [1, "foo"]
|
616
|
-
schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, expected "integer".
|
704
|
+
schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError: /[2]: Invalid type, got type "String", expected "integer".
|
617
705
|
schema.validate!([1, 'foo', 2, 3]) # => [1, "foo", 2, 3]
|
618
706
|
```
|
619
707
|
|
@@ -772,7 +860,7 @@ schema = Schemacop::Schema3.new :hash do
|
|
772
860
|
str? :foo
|
773
861
|
end
|
774
862
|
|
775
|
-
schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
|
863
|
+
schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
|
776
864
|
schema.validate!({foo: 'bar'}) # => {"foo"=>"bar"}
|
777
865
|
```
|
778
866
|
|
@@ -856,7 +944,7 @@ end
|
|
856
944
|
|
857
945
|
schema.validate!({id: 1}) # => {"id"=>1}
|
858
946
|
schema.validate!({id: 1, foo: 'bar'}) # => {"id"=>1, "foo"=>"bar"}
|
859
|
-
schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
|
947
|
+
schema.validate!({id: 1, foo: 42}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Integer", expected "string".
|
860
948
|
```
|
861
949
|
|
862
950
|
Using the option `property_names`, you can additionaly specify a pattern that
|
@@ -880,8 +968,8 @@ end
|
|
880
968
|
|
881
969
|
schema.validate!({}) # => {}
|
882
970
|
schema.validate!({foo: [1, 2, 3]}) # => {"foo"=>[1, 2, 3]}
|
883
|
-
schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "array".
|
884
|
-
schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, expected "array".
|
971
|
+
schema.validate!({foo: :bar}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, got type "Symbol", expected "array".
|
972
|
+
schema.validate!({Foo: :bar}) # => Schemacop::Exceptions::ValidationError: /: Property name :Foo does not match "^[a-z]+$". /Foo: Invalid type, got type "Symbol", expected "array".
|
885
973
|
```
|
886
974
|
|
887
975
|
##### Dependencies
|
@@ -955,10 +1043,10 @@ of allowed classes:
|
|
955
1043
|
schema = Schemacop::Schema3.new :object, classes: [String]
|
956
1044
|
|
957
1045
|
schema.validate!(nil) # => nil
|
958
|
-
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
959
|
-
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1046
|
+
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
|
1047
|
+
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
|
960
1048
|
schema.validate!('foo') # => "foo"
|
961
|
-
schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1049
|
+
schema.validate!('foo'.html_safe) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "ActiveSupport::SafeBuffer", expected "String".
|
962
1050
|
```
|
963
1051
|
|
964
1052
|
Here, the node checks if the given value is an instance of any of the given
|
@@ -970,8 +1058,8 @@ If you want to allow subclasses, you can specify this by using the `strict` opti
|
|
970
1058
|
schema = Schemacop::Schema3.new :object, classes: [String], strict: false
|
971
1059
|
|
972
1060
|
schema.validate!(nil) # => nil
|
973
|
-
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
974
|
-
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, expected "String".
|
1061
|
+
schema.validate!(true) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "TrueClass", expected "String".
|
1062
|
+
schema.validate!(Object.new) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Object", expected "String".
|
975
1063
|
schema.validate!('foo') # => "foo"
|
976
1064
|
schema.validate!('foo'.html_safe) # => "foo"
|
977
1065
|
```
|
@@ -1144,7 +1232,7 @@ schema.validate!({
|
|
1144
1232
|
shipping_address: 'foo',
|
1145
1233
|
billing_address: 42
|
1146
1234
|
})
|
1147
|
-
# => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, expected "object". /billing_address: Invalid type, expected "object".
|
1235
|
+
# => Schemacop::Exceptions::ValidationError: /shipping_address: Invalid type, got type "String", expected "object". /billing_address: Invalid type, got type "Integer", expected "object".
|
1148
1236
|
|
1149
1237
|
schema.validate!({
|
1150
1238
|
shipping_address: {
|
@@ -1253,7 +1341,8 @@ files. This is especially useful is you have schemas in your application which
|
|
1253
1341
|
are used multiple times throughout the application.
|
1254
1342
|
|
1255
1343
|
For each schema, you define the schema in a separate file, and after loading the
|
1256
|
-
schemas, you can reference them in other schemas.
|
1344
|
+
schemas, you can reference them in other schemas. The schema can be retrieved
|
1345
|
+
by using the file name, e.g. `user` in the example `app/schemas/user.rb` below.
|
1257
1346
|
|
1258
1347
|
The default load path is `'app/schemas'`, but this can be configured by setting
|
1259
1348
|
the value of the `load_paths` attribute of the `Schemacop` module.
|
@@ -1270,7 +1359,7 @@ Where:
|
|
1270
1359
|
* context schemas: Defined in the current context using `context.schema`
|
1271
1360
|
* global schemas: Defined in a ruby file in the load path
|
1272
1361
|
|
1273
|
-
### Rails applications
|
1362
|
+
### External schemas in Rails applications
|
1274
1363
|
|
1275
1364
|
In Rails applications, your schemas are automatically eager-loaded from the load
|
1276
1365
|
path `'app/schemas'` when your application is started, unless your application
|
@@ -1297,7 +1386,7 @@ end
|
|
1297
1386
|
```
|
1298
1387
|
|
1299
1388
|
```ruby
|
1300
|
-
# app/schemas/nested/
|
1389
|
+
# app/schemas/nested/group.rb
|
1301
1390
|
schema :hash do
|
1302
1391
|
str! :name
|
1303
1392
|
end
|
@@ -1321,7 +1410,7 @@ schema.validate!({usr: {first_name: 'Joe', last_name: 'Doe', groups: [{name: 'fo
|
|
1321
1410
|
# => {"usr"=>{"first_name"=>"Joe", "last_name"=>"Doe", "groups"=>[{"name"=>"foo"}, {"name"=>"bar"}]}}
|
1322
1411
|
```
|
1323
1412
|
|
1324
|
-
### Non-Rails applications
|
1413
|
+
### External schemas in Non-Rails applications
|
1325
1414
|
|
1326
1415
|
Usage in non-Rails applications is the same as with usage in Rails applications,
|
1327
1416
|
however you might need to eager load the schemas yourself:
|