schemacop 3.0.10 → 3.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +27 -0
- data/.releaser_config +0 -1
- data/CHANGELOG.md +19 -0
- data/README.md +1 -1
- data/README_V3.md +73 -11
- data/VERSION +1 -1
- data/lib/schemacop/v3/combination_node.rb +1 -1
- data/lib/schemacop/v3/hash_node.rb +1 -1
- data/lib/schemacop/v3/node.rb +7 -6
- data/lib/schemacop/v3/one_of_node.rb +23 -1
- data/lib/schemacop/v3/string_node.rb +13 -10
- data/schemacop.gemspec +30 -18
- data/test/unit/schemacop/v3/boolean_node_test.rb +70 -1
- data/test/unit/schemacop/v3/hash_node_test.rb +40 -0
- data/test/unit/schemacop/v3/integer_node_test.rb +22 -0
- data/test/unit/schemacop/v3/node_test.rb +10 -0
- data/test/unit/schemacop/v3/number_node_test.rb +3 -0
- data/test/unit/schemacop/v3/one_of_node_test.rb +12 -0
- data/test/unit/schemacop/v3/string_node_test.rb +24 -0
- 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: 45a0558e32f2c4a8e4d9db631ac874629ed7e92a378a177266a216b9e35295bd
|
4
|
+
data.tar.gz: fa2da0867bf598a32ba7728b246343f6bf3849aa1ccdddf9093dcd360df2b53e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 557da4f2765fc6a9c081b1b894f57b9acb8f3f16ff72c56589064672d060b1879b4d68a498692aeb4764b78ab1091de9b5b0a8dbd4b0ebc0e8bc401875ff5167
|
7
|
+
data.tar.gz: 54748996bd7ea09dda23163c808e32300e898ceb3910d1decfaa0516d49a268d79afeb9c9292f3a3fbc6d5f128ba8de1470111c66470585772b5903580088cbb
|
@@ -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,24 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## 3.0.14 (2021-10-11)
|
4
|
+
|
5
|
+
* Add string format `integer_list`
|
6
|
+
|
7
|
+
* Improve handling for booleans
|
8
|
+
|
9
|
+
## 3.0.13 (2021-10-04)
|
10
|
+
|
11
|
+
* When using `boolean` with `cast_str: true`, `"0"` is now casted to `false` and
|
12
|
+
`"1"` to `true`. This in addition to already casting `"true", "false"`.
|
13
|
+
|
14
|
+
## 3.0.12 (2021-09-14)
|
15
|
+
|
16
|
+
* Fix compatibility issue with `ruby <= 2.5`
|
17
|
+
|
18
|
+
## 3.0.11 (2021-03-26)
|
19
|
+
|
20
|
+
* Treat blank string as nil when using `cast_str` option
|
21
|
+
|
3
22
|
## 3.0.10 (2021-03-19)
|
4
23
|
|
5
24
|
* If wrong type is given, report class of wrong type in error message
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![
|
1
|
+
[![Ruby](https://github.com/sitrox/schemacop/actions/workflows/ruby.yml/badge.svg?branch=master)](https://github.com/sitrox/schemacop/actions/workflows/ruby.yml)
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/schemacop.svg)](https://badge.fury.io/rb/schemacop)
|
3
3
|
|
4
4
|
# Schemacop
|
data/README_V3.md
CHANGED
@@ -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
|
@@ -246,6 +246,9 @@ transformed into various types.
|
|
246
246
|
* `number`
|
247
247
|
The string must be a number and will be casted to a ruby `Float` object.
|
248
248
|
|
249
|
+
* `integer_list`
|
250
|
+
The string must consist of comma-separated integers casted to a ruby `Array<Integer>` object
|
251
|
+
|
249
252
|
* `symbol`
|
250
253
|
The string can be anything and will be casted to a ruby `Symbol` object.
|
251
254
|
|
@@ -317,7 +320,7 @@ integer can be done.
|
|
317
320
|
* `cast_str`
|
318
321
|
When set to `true`, this node also accepts strings that can be casted to an integer, e.g.
|
319
322
|
the values `'-5'` or `'42'`. Please note that you can only validate numbers which
|
320
|
-
are in the `Integer` format.
|
323
|
+
are in the `Integer` format. Blank strings will be treated equally as `nil`.
|
321
324
|
|
322
325
|
#### Examples
|
323
326
|
|
@@ -346,6 +349,19 @@ schema.validate!('102') # => Schemacop::Exceptions::ValidationError: /
|
|
346
349
|
schema.validate!('42.1') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
347
350
|
schema.validate!('4r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
348
351
|
schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
352
|
+
schema.validate!(nil) # => nil
|
353
|
+
schema.validate!('') # => nil
|
354
|
+
```
|
355
|
+
|
356
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
357
|
+
and validating a blank string will return `nil`.
|
358
|
+
If you need a value, use the `required` option:
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
schema = Schemacop::Schema3.new(:integer, minimum: 0, maximum: 100, multiple_of: 2, cast_str: true, required: true)
|
362
|
+
schema.validate!('42') # => 42
|
363
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
364
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
349
365
|
```
|
350
366
|
|
351
367
|
### Number
|
@@ -386,7 +402,7 @@ With the various available options, validations on the value of the number can b
|
|
386
402
|
When set to `true`, this node also accepts strings that can be casted to a number, e.g.
|
387
403
|
the values `'0.1'` or `'3.1415'`. Please note that you can only validate numbers which
|
388
404
|
are in the `Integer` or `Float` format, i.e. values like `'1.5r'` or `'(4 + 0i)'` will
|
389
|
-
not work.
|
405
|
+
not work. Blank strings will be treated equally as `nil`.
|
390
406
|
|
391
407
|
#### Examples
|
392
408
|
|
@@ -414,6 +430,19 @@ schema.validate!('51') # => Schemacop::Exceptions::ValidationError: /: Ma
|
|
414
430
|
schema.validate!('42.5') # => 42.5
|
415
431
|
schema.validate!('1.5r') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
416
432
|
schema.validate!('(4 + 0i)') # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
433
|
+
schema.validate!(nil) # => nil
|
434
|
+
schema.validate!('') # => nil
|
435
|
+
```
|
436
|
+
|
437
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
438
|
+
and validating a blank string will return `nil`.
|
439
|
+
If you need a value, use the `required` option:
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
schema = Schemacop::Schema3.new(:number, cast_str: true, minimum: 0.0, maximum: (50r), multiple_of: BigDecimal('0.5'), require: true)
|
443
|
+
schema.validate!('42.5') # => 42.5
|
444
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
445
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
417
446
|
```
|
418
447
|
|
419
448
|
### Symbol
|
@@ -426,7 +455,7 @@ The symbol type is used to validate elements for the Ruby `Symbol` class.
|
|
426
455
|
#### Options
|
427
456
|
|
428
457
|
* `cast_str`
|
429
|
-
When set to `true`, this node also accepts strings that can be casted to a symbol.
|
458
|
+
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
459
|
|
431
460
|
#### Examples
|
432
461
|
|
@@ -450,6 +479,19 @@ schema.validate!('foo') # => :foo
|
|
450
479
|
schema.validate!('123') # => :"123"
|
451
480
|
schema.validate!('false') # => :false
|
452
481
|
schema.validate!(':false') # => :":false"
|
482
|
+
schema.validate!(nil) # => nil
|
483
|
+
schema.validate!('') # => nil
|
484
|
+
```
|
485
|
+
|
486
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
487
|
+
and validating a blank string will return `nil`.
|
488
|
+
If you need a value, use the `required` option:
|
489
|
+
|
490
|
+
```ruby
|
491
|
+
schema = Schemacop::Schema3.new(:symbol, cast_str: true, required: true)
|
492
|
+
schema.validate!('foo') # => :foo
|
493
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
494
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
453
495
|
```
|
454
496
|
|
455
497
|
### Boolean
|
@@ -462,8 +504,9 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
462
504
|
#### Options
|
463
505
|
|
464
506
|
* `cast_str`
|
465
|
-
When set to `true`, this node also accepts strings that can be casted to a
|
466
|
-
the values `'true'` and `'
|
507
|
+
When set to `true`, this node also accepts strings that can be casted to a
|
508
|
+
boolean, namely the values `'true'`, `'false'`, `'1'` and `'0'`. Blank strings
|
509
|
+
will be treated equally as `nil`.
|
467
510
|
|
468
511
|
#### Examples
|
469
512
|
|
@@ -475,6 +518,11 @@ schema.validate!(false) # => false
|
|
475
518
|
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Symbol", expected "boolean".
|
476
519
|
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "boolean".
|
477
520
|
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "boolean".
|
521
|
+
|
522
|
+
schema.validate!('0', cast_str: true) # => false
|
523
|
+
schema.validate!('1', cast_str: true) # => true
|
524
|
+
schema.validate!('false', cast_str: true) # => false
|
525
|
+
schema.validate!('true', cast_str: true) # => true
|
478
526
|
```
|
479
527
|
|
480
528
|
With `cast_str` enabled:
|
@@ -486,6 +534,19 @@ schema.validate!(false) # => false
|
|
486
534
|
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
487
535
|
schema.validate!('false') # => false
|
488
536
|
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Matches 0 definitions but should match exactly 1.
|
537
|
+
schema.validate!(nil) # => nil
|
538
|
+
schema.validate!('') # => nil
|
539
|
+
```
|
540
|
+
|
541
|
+
Please note, that `nil` and blank strings are treated equally when using the `cast_str` option,
|
542
|
+
and validating a blank string will return `nil`.
|
543
|
+
If you need a value, use the `required` option:
|
544
|
+
|
545
|
+
```ruby
|
546
|
+
schema = Schemacop::Schema3.new(:boolean, cast_str: true, required: true)
|
547
|
+
schema.validate!('false') # => false
|
548
|
+
schema.validate!(nil) # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
549
|
+
schema.validate!('') # => Schemacop::Exceptions::ValidationError: /: Value must be given.
|
489
550
|
```
|
490
551
|
|
491
552
|
### Array
|
@@ -1289,7 +1350,8 @@ files. This is especially useful is you have schemas in your application which
|
|
1289
1350
|
are used multiple times throughout the application.
|
1290
1351
|
|
1291
1352
|
For each schema, you define the schema in a separate file, and after loading the
|
1292
|
-
schemas, you can reference them in other schemas.
|
1353
|
+
schemas, you can reference them in other schemas. The schema can be retrieved
|
1354
|
+
by using the file name, e.g. `user` in the example `app/schemas/user.rb` below.
|
1293
1355
|
|
1294
1356
|
The default load path is `'app/schemas'`, but this can be configured by setting
|
1295
1357
|
the value of the `load_paths` attribute of the `Schemacop` module.
|
@@ -1306,7 +1368,7 @@ Where:
|
|
1306
1368
|
* context schemas: Defined in the current context using `context.schema`
|
1307
1369
|
* global schemas: Defined in a ruby file in the load path
|
1308
1370
|
|
1309
|
-
### Rails applications
|
1371
|
+
### External schemas in Rails applications
|
1310
1372
|
|
1311
1373
|
In Rails applications, your schemas are automatically eager-loaded from the load
|
1312
1374
|
path `'app/schemas'` when your application is started, unless your application
|
@@ -1333,7 +1395,7 @@ end
|
|
1333
1395
|
```
|
1334
1396
|
|
1335
1397
|
```ruby
|
1336
|
-
# app/schemas/nested/
|
1398
|
+
# app/schemas/nested/group.rb
|
1337
1399
|
schema :hash do
|
1338
1400
|
str! :name
|
1339
1401
|
end
|
@@ -1357,7 +1419,7 @@ schema.validate!({usr: {first_name: 'Joe', last_name: 'Doe', groups: [{name: 'fo
|
|
1357
1419
|
# => {"usr"=>{"first_name"=>"Joe", "last_name"=>"Doe", "groups"=>[{"name"=>"foo"}, {"name"=>"bar"}]}}
|
1358
1420
|
```
|
1359
1421
|
|
1360
|
-
### Non-Rails applications
|
1422
|
+
### External schemas in Non-Rails applications
|
1361
1423
|
|
1362
1424
|
Usage in non-Rails applications is the same as with usage in Rails applications,
|
1363
1425
|
however you might need to eager load the schemas yourself:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.
|
1
|
+
3.0.14
|
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
|
@@ -177,7 +178,7 @@ module Schemacop
|
|
177
178
|
json[:title] = @title if @title
|
178
179
|
json[context.swagger_json? ? :example : :examples] = @examples if @examples
|
179
180
|
json[:description] = @description if @description
|
180
|
-
json[:default] = @default
|
181
|
+
json[:default] = @default unless @default.nil?
|
181
182
|
json[:enum] = @enum.to_a if @enum
|
182
183
|
|
183
184
|
return json.as_json
|
@@ -196,7 +197,7 @@ module Schemacop
|
|
196
197
|
|
197
198
|
# Apply default #
|
198
199
|
if data.nil?
|
199
|
-
if default
|
200
|
+
if !default.nil?
|
200
201
|
data = default
|
201
202
|
else
|
202
203
|
return nil
|
@@ -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)
|
@@ -9,15 +9,16 @@ module Schemacop
|
|
9
9
|
|
10
10
|
# rubocop:disable Layout/LineLength
|
11
11
|
FORMAT_PATTERNS = {
|
12
|
-
date:
|
13
|
-
'date-time':
|
14
|
-
time:
|
15
|
-
email:
|
16
|
-
boolean:
|
17
|
-
binary:
|
18
|
-
symbol:
|
19
|
-
integer:
|
20
|
-
number:
|
12
|
+
date: /^([0-9]{4})-?(1[0-2]|0[1-9])-?(3[01]|0[1-9]|[12][0-9])$/,
|
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
|
+
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
|
+
email: URI::MailTo::EMAIL_REGEXP,
|
16
|
+
boolean: /^(true|false|0|1)$/,
|
17
|
+
binary: nil,
|
18
|
+
symbol: nil,
|
19
|
+
integer: /^-?[0-9]+$/,
|
20
|
+
number: /^-?[0-9]+(\.[0-9]+)?$/,
|
21
|
+
'integer-list': /^(-?[0-9]+)(,-?[0-9]+)*$/
|
21
22
|
}.freeze
|
22
23
|
# rubocop:enable Layout/LineLength
|
23
24
|
|
@@ -92,7 +93,7 @@ module Schemacop
|
|
92
93
|
|
93
94
|
case options[:format]
|
94
95
|
when :boolean
|
95
|
-
|
96
|
+
%w[true 1].include?(to_cast)
|
96
97
|
when :date
|
97
98
|
return Date.parse(to_cast)
|
98
99
|
when :'date-time'
|
@@ -103,6 +104,8 @@ module Schemacop
|
|
103
104
|
return Integer(to_cast)
|
104
105
|
when :number
|
105
106
|
return Float(to_cast)
|
107
|
+
when :'integer-list'
|
108
|
+
return to_cast.split(',').map(&:to_i)
|
106
109
|
when :symbol
|
107
110
|
return to_cast.to_sym
|
108
111
|
else
|
data/schemacop.gemspec
CHANGED
@@ -1,37 +1,49 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: schemacop 3.0.
|
2
|
+
# stub: schemacop 3.0.14 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.14"
|
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-11"
|
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
|
-
s.rubygems_version = "3.
|
15
|
+
s.rubygems_version = "3.0.3".freeze
|
16
16
|
s.summary = "Schemacop validates ruby structures consisting of nested hashes and arrays against simple schema definitions.".freeze
|
17
17
|
s.test_files = ["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]
|
18
18
|
|
19
19
|
if s.respond_to? :specification_version then
|
20
20
|
s.specification_version = 4
|
21
|
-
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
22
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
23
|
+
s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.0"])
|
24
|
+
s.add_runtime_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
|
25
|
+
s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
|
26
|
+
s.add_development_dependency(%q<rake>.freeze, [">= 0"])
|
27
|
+
s.add_development_dependency(%q<minitest>.freeze, [">= 0"])
|
28
|
+
s.add_development_dependency(%q<minitest-reporters>.freeze, [">= 0"])
|
29
|
+
s.add_development_dependency(%q<colorize>.freeze, [">= 0"])
|
30
|
+
s.add_development_dependency(%q<rubocop>.freeze, ["= 0.92.0"])
|
31
|
+
s.add_development_dependency(%q<pry>.freeze, [">= 0"])
|
32
|
+
s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
|
33
|
+
s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
|
36
|
+
s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
|
37
|
+
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
38
|
+
s.add_dependency(%q<rake>.freeze, [">= 0"])
|
39
|
+
s.add_dependency(%q<minitest>.freeze, [">= 0"])
|
40
|
+
s.add_dependency(%q<minitest-reporters>.freeze, [">= 0"])
|
41
|
+
s.add_dependency(%q<colorize>.freeze, [">= 0"])
|
42
|
+
s.add_dependency(%q<rubocop>.freeze, ["= 0.92.0"])
|
43
|
+
s.add_dependency(%q<pry>.freeze, [">= 0"])
|
44
|
+
s.add_dependency(%q<byebug>.freeze, [">= 0"])
|
45
|
+
s.add_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
|
46
|
+
end
|
35
47
|
else
|
36
48
|
s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
|
37
49
|
s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
|
@@ -18,6 +18,38 @@ module Schemacop
|
|
18
18
|
assert_json(type: :boolean)
|
19
19
|
end
|
20
20
|
|
21
|
+
def test_required_default
|
22
|
+
schema do
|
23
|
+
boo? :enabled, default: true
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_validation(enabled: true)
|
27
|
+
assert_validation(enabled: false)
|
28
|
+
|
29
|
+
assert_cast({}, { 'enabled' => true })
|
30
|
+
|
31
|
+
schema do
|
32
|
+
boo? :enabled, default: false
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_validation(enabled: true)
|
36
|
+
assert_validation(enabled: false)
|
37
|
+
|
38
|
+
assert_cast({}, { 'enabled' => false })
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_default_bug
|
42
|
+
schema do
|
43
|
+
str! :send_message
|
44
|
+
boo? :always_show_successful, default: true
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_cast(
|
48
|
+
{ 'send_message' => 'foo' },
|
49
|
+
{ 'send_message' => 'foo', 'always_show_successful' => true }
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
21
53
|
def test_required
|
22
54
|
schema :boolean, required: true
|
23
55
|
|
@@ -146,12 +178,49 @@ module Schemacop
|
|
146
178
|
assert_cast('true', true)
|
147
179
|
assert_cast('false', false)
|
148
180
|
|
181
|
+
assert_cast('1', true)
|
182
|
+
assert_cast('0', false)
|
183
|
+
|
149
184
|
assert_cast(true, true)
|
150
185
|
assert_cast(false, false)
|
151
186
|
|
152
|
-
assert_validation('
|
187
|
+
assert_validation('5') do
|
153
188
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
154
189
|
end
|
190
|
+
|
191
|
+
# Nil can be validated, as it's not required
|
192
|
+
assert_validation(nil)
|
193
|
+
|
194
|
+
assert_validation('')
|
195
|
+
|
196
|
+
assert_cast('', nil)
|
197
|
+
assert_cast(nil, nil)
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_cast_str_required
|
201
|
+
schema :boolean, cast_str: true, required: true
|
202
|
+
|
203
|
+
assert_cast('true', true)
|
204
|
+
assert_cast('false', false)
|
205
|
+
|
206
|
+
assert_cast(true, true)
|
207
|
+
assert_cast(false, false)
|
208
|
+
|
209
|
+
assert_validation('4') do
|
210
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
211
|
+
end
|
212
|
+
|
213
|
+
assert_validation('foo') do
|
214
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
215
|
+
end
|
216
|
+
|
217
|
+
assert_validation(nil) do
|
218
|
+
error '/', 'Value must be given.'
|
219
|
+
end
|
220
|
+
|
221
|
+
assert_validation('') do
|
222
|
+
error '/', 'Value must be given.'
|
223
|
+
end
|
155
224
|
end
|
156
225
|
end
|
157
226
|
end
|
@@ -965,6 +965,46 @@ module Schemacop
|
|
965
965
|
assert_cast({ foo: 42, bar: 13 }, { bar: 42 }.with_indifferent_access)
|
966
966
|
assert_cast({ bar: 13, foo: 42 }, { bar: 42 }.with_indifferent_access)
|
967
967
|
end
|
968
|
+
|
969
|
+
def test_cast_str_required
|
970
|
+
schema :hash do
|
971
|
+
boo! :active, cast_str: true
|
972
|
+
int! :id, cast_str: true
|
973
|
+
end
|
974
|
+
|
975
|
+
assert_validation({ active: true, id: 1 })
|
976
|
+
assert_validation({ active: 'true', id: '1' })
|
977
|
+
|
978
|
+
assert_validation({}) do
|
979
|
+
error '/active', 'Value must be given.'
|
980
|
+
error '/id', 'Value must be given.'
|
981
|
+
end
|
982
|
+
|
983
|
+
assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
|
984
|
+
|
985
|
+
assert_validation({ active: '', id: '' }) do
|
986
|
+
error '/active', 'Value must be given.'
|
987
|
+
error '/id', 'Value must be given.'
|
988
|
+
end
|
989
|
+
end
|
990
|
+
|
991
|
+
def test_cast_str_optional
|
992
|
+
schema :hash do
|
993
|
+
boo? :active, cast_str: true
|
994
|
+
int? :id, cast_str: true
|
995
|
+
end
|
996
|
+
|
997
|
+
assert_validation({ active: true, id: 1 })
|
998
|
+
assert_validation({ active: 'true', id: '1' })
|
999
|
+
|
1000
|
+
assert_cast({ active: 'true', id: '1' }, { active: true, id: 1 }.with_indifferent_access)
|
1001
|
+
|
1002
|
+
assert_validation({})
|
1003
|
+
assert_validation({ active: nil, id: nil })
|
1004
|
+
|
1005
|
+
assert_validation({ active: '', id: '' })
|
1006
|
+
assert_cast({ active: '', id: '' }, { active: nil, id: nil }.with_indifferent_access)
|
1007
|
+
end
|
968
1008
|
end
|
969
1009
|
end
|
970
1010
|
end
|
@@ -339,6 +339,28 @@ module Schemacop
|
|
339
339
|
assert_cast('1', 1)
|
340
340
|
assert_cast(1, 1)
|
341
341
|
|
342
|
+
assert_cast(nil, nil)
|
343
|
+
assert_cast('', nil)
|
344
|
+
|
345
|
+
assert_validation('true') do
|
346
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_cast_str_required
|
351
|
+
schema :integer, cast_str: true, required: true
|
352
|
+
|
353
|
+
assert_cast('1', 1)
|
354
|
+
assert_cast(1, 1)
|
355
|
+
|
356
|
+
assert_validation(nil) do
|
357
|
+
error '/', 'Value must be given.'
|
358
|
+
end
|
359
|
+
|
360
|
+
assert_validation('') do
|
361
|
+
error '/', 'Value must be given.'
|
362
|
+
end
|
363
|
+
|
342
364
|
assert_validation('true') do
|
343
365
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
344
366
|
end
|
@@ -93,6 +93,10 @@ module Schemacop
|
|
93
93
|
error '/', 'Value must be given.'
|
94
94
|
end
|
95
95
|
|
96
|
+
assert_validation('') do
|
97
|
+
error '/', 'Value must be given.'
|
98
|
+
end
|
99
|
+
|
96
100
|
assert_validation('5')
|
97
101
|
assert_validation('5.3') do
|
98
102
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
@@ -156,6 +160,12 @@ module Schemacop
|
|
156
160
|
assert_validation([nil]) do
|
157
161
|
error '/[0]', 'Value must be given.'
|
158
162
|
end
|
163
|
+
assert_validation(['']) do
|
164
|
+
error '/[0]', 'Value must be given.'
|
165
|
+
end
|
166
|
+
assert_validation([]) do
|
167
|
+
error '/', 'Array has 0 items but must have exactly 1.'
|
168
|
+
end
|
159
169
|
end
|
160
170
|
|
161
171
|
def test_not_support_block
|
@@ -182,6 +182,18 @@ module Schemacop
|
|
182
182
|
schema :one_of
|
183
183
|
end
|
184
184
|
end
|
185
|
+
|
186
|
+
def test_treat_blank_as_nil
|
187
|
+
schema :one_of, treat_blank_as_nil: true do
|
188
|
+
boo
|
189
|
+
str format: :boolean
|
190
|
+
end
|
191
|
+
|
192
|
+
assert_validation(nil)
|
193
|
+
assert_validation('')
|
194
|
+
assert_validation('true')
|
195
|
+
assert_validation(true)
|
196
|
+
end
|
185
197
|
end
|
186
198
|
end
|
187
199
|
end
|
@@ -200,6 +200,30 @@ module Schemacop
|
|
200
200
|
assert_cast('039n23$g- sfk3/', :'039n23$g- sfk3/')
|
201
201
|
end
|
202
202
|
|
203
|
+
def test_format_integer_list
|
204
|
+
schema :string, format: :integer_list
|
205
|
+
|
206
|
+
assert_json(type: :string, format: :'integer-list')
|
207
|
+
|
208
|
+
assert_validation '1,2,3,4'
|
209
|
+
assert_validation '1,2,-3,-54'
|
210
|
+
assert_validation '2'
|
211
|
+
assert_validation '-2'
|
212
|
+
|
213
|
+
assert_validation 234 do
|
214
|
+
error '/', StringNodeTest.invalid_type_error(Integer)
|
215
|
+
end
|
216
|
+
|
217
|
+
assert_validation 'sd sfdij soidf' do
|
218
|
+
error '/', 'String does not match format "integer-list".'
|
219
|
+
end
|
220
|
+
|
221
|
+
assert_cast nil, nil
|
222
|
+
assert_cast '1,-2,3', [1, -2, 3]
|
223
|
+
assert_cast '1', [1]
|
224
|
+
assert_cast '-1', [-1]
|
225
|
+
end
|
226
|
+
|
203
227
|
def test_enum
|
204
228
|
schema :string, enum: ['foo', 'some string', 'some other string', 42]
|
205
229
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schemacop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sitrox
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -170,10 +170,10 @@ executables: []
|
|
170
170
|
extensions: []
|
171
171
|
extra_rdoc_files: []
|
172
172
|
files:
|
173
|
+
- ".github/workflows/ruby.yml"
|
173
174
|
- ".gitignore"
|
174
175
|
- ".releaser_config"
|
175
176
|
- ".rubocop.yml"
|
176
|
-
- ".travis.yml"
|
177
177
|
- ".yardopts"
|
178
178
|
- CHANGELOG.md
|
179
179
|
- Gemfile
|
@@ -292,7 +292,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
292
292
|
- !ruby/object:Gem::Version
|
293
293
|
version: '0'
|
294
294
|
requirements: []
|
295
|
-
rubygems_version: 3.
|
295
|
+
rubygems_version: 3.0.3
|
296
296
|
signing_key:
|
297
297
|
specification_version: 4
|
298
298
|
summary: Schemacop validates ruby structures consisting of nested hashes and arrays
|