schemacop 3.0.11 → 3.0.15
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 +19 -0
- data/README.md +1 -1
- data/README_V3.md +36 -8
- 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 +2 -2
- data/lib/schemacop/v3/string_node.rb +6 -32
- data/lib/schemacop.rb +74 -0
- data/schemacop.gemspec +30 -18
- data/test/unit/schemacop/v3/boolean_node_test.rb +41 -2
- data/test/unit/schemacop/v3/string_node_test.rb +93 -27
- 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: 2e43238e69ebe95beaac85536a9dadbf3233bd4fa5751c7e019ca6d07aa05860
|
4
|
+
data.tar.gz: 8bafd412f0572e0c12a7c163cba48b6f6f69900ec903684dc3c9495b4913cf52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9da291f938673bf76f55f4f4fddbf5a3355fe9c72ae30f8be8c9e224b9bea2f1a89dbcb2da8312ddbaca09dcd25da5511e904963400633d9ef193014da9ee0ec
|
7
|
+
data.tar.gz: 21793a6a7f3fc730a123f7ed8ceacde1e930620b77fef63c7e30c919a9e9ebf3372a8a5208dbdf177880e011322f529d04558bd68d363cbfd85b023a1f2ed265
|
@@ -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.15 (2021-10-11)
|
4
|
+
|
5
|
+
* Add support for custom string formatters
|
6
|
+
|
7
|
+
## 3.0.14 (2021-10-11)
|
8
|
+
|
9
|
+
* Add string format `integer_list`
|
10
|
+
|
11
|
+
* Improve handling for booleans
|
12
|
+
|
13
|
+
## 3.0.13 (2021-10-04)
|
14
|
+
|
15
|
+
* When using `boolean` with `cast_str: true`, `"0"` is now casted to `false` and
|
16
|
+
`"1"` to `true`. This in addition to already casting `"true", "false"`.
|
17
|
+
|
18
|
+
## 3.0.12 (2021-09-14)
|
19
|
+
|
20
|
+
* Fix compatibility issue with `ruby <= 2.5`
|
21
|
+
|
3
22
|
## 3.0.11 (2021-03-26)
|
4
23
|
|
5
24
|
* Treat blank string as nil when using `cast_str` option
|
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
@@ -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,9 +246,30 @@ 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
|
|
255
|
+
#### Custom Formats
|
256
|
+
|
257
|
+
You can also implement your custom formats or override the behavior of the
|
258
|
+
standard formats. This can be done in the initializer configuration (in case of
|
259
|
+
a Rails appliation):
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
# config/initializers/schemacop.rb
|
263
|
+
Schemacop.register_string_formatter(
|
264
|
+
:character_array, # Formatter name
|
265
|
+
pattern: /^[a-zA-Z](,[a-zA-Z])*/, # Regex pattern for validation
|
266
|
+
handler: ->(value) { value.split(',') } # Casting callback
|
267
|
+
)
|
268
|
+
|
269
|
+
# In your schema
|
270
|
+
str! :my_list, format: :character_array
|
271
|
+
```
|
272
|
+
|
252
273
|
#### Examples
|
253
274
|
|
254
275
|
```ruby
|
@@ -501,8 +522,9 @@ The boolean type is used to validate Ruby booleans, i.e. the `TrueClass` and `Fa
|
|
501
522
|
#### Options
|
502
523
|
|
503
524
|
* `cast_str`
|
504
|
-
When set to `true`, this node also accepts strings that can be casted to a
|
505
|
-
the values `'true'` and `'
|
525
|
+
When set to `true`, this node also accepts strings that can be casted to a
|
526
|
+
boolean, namely the values `'true'`, `'false'`, `'1'` and `'0'`. Blank strings
|
527
|
+
will be treated equally as `nil`.
|
506
528
|
|
507
529
|
#### Examples
|
508
530
|
|
@@ -514,6 +536,11 @@ schema.validate!(false) # => false
|
|
514
536
|
schema.validate!(:false) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Symbol", expected "boolean".
|
515
537
|
schema.validate!('false') # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "String", expected "boolean".
|
516
538
|
schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invalid type, got type "Integer", expected "boolean".
|
539
|
+
|
540
|
+
schema.validate!('0', cast_str: true) # => false
|
541
|
+
schema.validate!('1', cast_str: true) # => true
|
542
|
+
schema.validate!('false', cast_str: true) # => false
|
543
|
+
schema.validate!('true', cast_str: true) # => true
|
517
544
|
```
|
518
545
|
|
519
546
|
With `cast_str` enabled:
|
@@ -1341,7 +1368,8 @@ files. This is especially useful is you have schemas in your application which
|
|
1341
1368
|
are used multiple times throughout the application.
|
1342
1369
|
|
1343
1370
|
For each schema, you define the schema in a separate file, and after loading the
|
1344
|
-
schemas, you can reference them in other schemas.
|
1371
|
+
schemas, you can reference them in other schemas. The schema can be retrieved
|
1372
|
+
by using the file name, e.g. `user` in the example `app/schemas/user.rb` below.
|
1345
1373
|
|
1346
1374
|
The default load path is `'app/schemas'`, but this can be configured by setting
|
1347
1375
|
the value of the `load_paths` attribute of the `Schemacop` module.
|
@@ -1358,7 +1386,7 @@ Where:
|
|
1358
1386
|
* context schemas: Defined in the current context using `context.schema`
|
1359
1387
|
* global schemas: Defined in a ruby file in the load path
|
1360
1388
|
|
1361
|
-
### Rails applications
|
1389
|
+
### External schemas in Rails applications
|
1362
1390
|
|
1363
1391
|
In Rails applications, your schemas are automatically eager-loaded from the load
|
1364
1392
|
path `'app/schemas'` when your application is started, unless your application
|
@@ -1385,7 +1413,7 @@ end
|
|
1385
1413
|
```
|
1386
1414
|
|
1387
1415
|
```ruby
|
1388
|
-
# app/schemas/nested/
|
1416
|
+
# app/schemas/nested/group.rb
|
1389
1417
|
schema :hash do
|
1390
1418
|
str! :name
|
1391
1419
|
end
|
@@ -1409,7 +1437,7 @@ schema.validate!({usr: {first_name: 'Joe', last_name: 'Doe', groups: [{name: 'fo
|
|
1409
1437
|
# => {"usr"=>{"first_name"=>"Joe", "last_name"=>"Doe", "groups"=>[{"name"=>"foo"}, {"name"=>"bar"}]}}
|
1410
1438
|
```
|
1411
1439
|
|
1412
|
-
### Non-Rails applications
|
1440
|
+
### External schemas in Non-Rails applications
|
1413
1441
|
|
1414
1442
|
Usage in non-Rails applications is the same as with usage in Rails applications,
|
1415
1443
|
however you might need to eager load the schemas yourself:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.
|
1
|
+
3.0.15
|
data/lib/schemacop/v3/node.rb
CHANGED
@@ -178,7 +178,7 @@ module Schemacop
|
|
178
178
|
json[:title] = @title if @title
|
179
179
|
json[context.swagger_json? ? :example : :examples] = @examples if @examples
|
180
180
|
json[:description] = @description if @description
|
181
|
-
json[:default] = @default
|
181
|
+
json[:default] = @default unless @default.nil?
|
182
182
|
json[:enum] = @enum.to_a if @enum
|
183
183
|
|
184
184
|
return json.as_json
|
@@ -197,7 +197,7 @@ module Schemacop
|
|
197
197
|
|
198
198
|
# Apply default #
|
199
199
|
if data.nil?
|
200
|
-
if default
|
200
|
+
if !default.nil?
|
201
201
|
data = default
|
202
202
|
else
|
203
203
|
return nil
|
@@ -7,20 +7,6 @@ module Schemacop
|
|
7
7
|
format
|
8
8
|
].freeze
|
9
9
|
|
10
|
-
# rubocop:disable Layout/LineLength
|
11
|
-
FORMAT_PATTERNS = {
|
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)$/,
|
17
|
-
binary: nil,
|
18
|
-
symbol: nil,
|
19
|
-
integer: /^-?[0-9]+$/,
|
20
|
-
number: /^-?[0-9]+(\.[0-9]+)?$/
|
21
|
-
}.freeze
|
22
|
-
# rubocop:enable Layout/LineLength
|
23
|
-
|
24
10
|
def self.allowed_options
|
25
11
|
super + ATTRIBUTES + %i[format_options pattern allow_blank]
|
26
12
|
end
|
@@ -70,8 +56,9 @@ module Schemacop
|
|
70
56
|
end
|
71
57
|
|
72
58
|
# Validate format #
|
73
|
-
if options[:format] &&
|
74
|
-
pattern =
|
59
|
+
if options[:format] && Schemacop.string_formatters.include?(options[:format])
|
60
|
+
pattern = Schemacop.string_formatters[options[:format]][:pattern]
|
61
|
+
|
75
62
|
if pattern && !super_data.match?(pattern)
|
76
63
|
result.error "String does not match format #{options[:format].to_s.inspect}."
|
77
64
|
elsif options[:format_options] && Node.resolve_class(options[:format])
|
@@ -90,21 +77,8 @@ module Schemacop
|
|
90
77
|
return nil
|
91
78
|
end
|
92
79
|
|
93
|
-
|
94
|
-
|
95
|
-
return to_cast == 'true'
|
96
|
-
when :date
|
97
|
-
return Date.parse(to_cast)
|
98
|
-
when :'date-time'
|
99
|
-
return DateTime.parse(to_cast)
|
100
|
-
when :time
|
101
|
-
Time.parse(to_cast)
|
102
|
-
when :integer
|
103
|
-
return Integer(to_cast)
|
104
|
-
when :number
|
105
|
-
return Float(to_cast)
|
106
|
-
when :symbol
|
107
|
-
return to_cast.to_sym
|
80
|
+
if (handler = Schemacop.string_formatters.dig(options[:format], :handler))
|
81
|
+
return handler.call(to_cast)
|
108
82
|
else
|
109
83
|
return to_cast
|
110
84
|
end
|
@@ -119,7 +93,7 @@ module Schemacop
|
|
119
93
|
end
|
120
94
|
|
121
95
|
def validate_self
|
122
|
-
if options.include?(:format) && !
|
96
|
+
if options.include?(:format) && !Schemacop.string_formatters.include?(options[:format])
|
123
97
|
fail "Format #{options[:format].to_s.inspect} is not supported."
|
124
98
|
end
|
125
99
|
|
data/lib/schemacop.rb
CHANGED
@@ -13,6 +13,80 @@ module Schemacop
|
|
13
13
|
mattr_accessor :default_schema_version
|
14
14
|
self.default_schema_version = 3
|
15
15
|
|
16
|
+
mattr_accessor :string_formatters
|
17
|
+
self.string_formatters = {}
|
18
|
+
|
19
|
+
def self.register_string_formatter(name, pattern:, handler:)
|
20
|
+
name = name.to_s.dasherize.to_sym
|
21
|
+
|
22
|
+
string_formatters[name] = {
|
23
|
+
pattern: pattern,
|
24
|
+
handler: handler
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
register_string_formatter(
|
29
|
+
:date,
|
30
|
+
pattern: /^([0-9]{4})-?(1[0-2]|0[1-9])-?(3[01]|0[1-9]|[12][0-9])$/,
|
31
|
+
handler: ->(value) { Date.parse(value) }
|
32
|
+
)
|
33
|
+
|
34
|
+
# rubocop: disable Layout/LineLength
|
35
|
+
register_string_formatter(
|
36
|
+
:'date-time',
|
37
|
+
pattern: /^(-?(?:[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])?$/,
|
38
|
+
handler: ->(value) { DateTime.parse(value) }
|
39
|
+
)
|
40
|
+
# rubocop: enable Layout/LineLength
|
41
|
+
|
42
|
+
register_string_formatter(
|
43
|
+
:time,
|
44
|
+
pattern: /^(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])?$/,
|
45
|
+
handler: ->(value) { Time.parse(value) }
|
46
|
+
)
|
47
|
+
|
48
|
+
register_string_formatter(
|
49
|
+
:email,
|
50
|
+
pattern: URI::MailTo::EMAIL_REGEXP,
|
51
|
+
handler: ->(value) { value }
|
52
|
+
)
|
53
|
+
|
54
|
+
register_string_formatter(
|
55
|
+
:boolean,
|
56
|
+
pattern: /^(true|false|0|1)$/,
|
57
|
+
handler: ->(value) { %w[true 1].include?(value) }
|
58
|
+
)
|
59
|
+
|
60
|
+
register_string_formatter(
|
61
|
+
:binary,
|
62
|
+
pattern: nil,
|
63
|
+
handler: ->(value) { value }
|
64
|
+
)
|
65
|
+
|
66
|
+
register_string_formatter(
|
67
|
+
:symbol,
|
68
|
+
pattern: nil,
|
69
|
+
handler: ->(value) { value.to_sym }
|
70
|
+
)
|
71
|
+
|
72
|
+
register_string_formatter(
|
73
|
+
:integer,
|
74
|
+
pattern: /^-?[0-9]+$/,
|
75
|
+
handler: ->(value) { Integer(value) }
|
76
|
+
)
|
77
|
+
|
78
|
+
register_string_formatter(
|
79
|
+
:number,
|
80
|
+
pattern: /^-?[0-9]+(\.[0-9]+)?$/,
|
81
|
+
handler: ->(value) { Float(value) }
|
82
|
+
)
|
83
|
+
|
84
|
+
register_string_formatter(
|
85
|
+
:'integer-list',
|
86
|
+
pattern: /^(-?[0-9]+)(,-?[0-9]+)*$/,
|
87
|
+
handler: ->(value) { value.split(',').map(&:to_i) }
|
88
|
+
)
|
89
|
+
|
16
90
|
def self.with_context(context)
|
17
91
|
prev_context = Thread.current[CONTEXT_THREAD_KEY]
|
18
92
|
Thread.current[CONTEXT_THREAD_KEY] = context
|
data/schemacop.gemspec
CHANGED
@@ -1,37 +1,49 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: schemacop 3.0.
|
2
|
+
# stub: schemacop 3.0.15 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.15"
|
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,10 +178,13 @@ 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
|
155
190
|
|
@@ -171,7 +206,11 @@ module Schemacop
|
|
171
206
|
assert_cast(true, true)
|
172
207
|
assert_cast(false, false)
|
173
208
|
|
174
|
-
assert_validation('
|
209
|
+
assert_validation('4') do
|
210
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
211
|
+
end
|
212
|
+
|
213
|
+
assert_validation('foo') do
|
175
214
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
176
215
|
end
|
177
216
|
|
@@ -158,6 +158,14 @@ module Schemacop
|
|
158
158
|
assert_cast('2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39))
|
159
159
|
end
|
160
160
|
|
161
|
+
def test_format_time
|
162
|
+
schema :string, format: :time
|
163
|
+
assert_json(type: :string, format: :time)
|
164
|
+
assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
|
165
|
+
|
166
|
+
assert_cast nil, nil
|
167
|
+
end
|
168
|
+
|
161
169
|
def test_format_email
|
162
170
|
schema :string, format: :email
|
163
171
|
|
@@ -183,6 +191,17 @@ module Schemacop
|
|
183
191
|
assert_cast('john.doe@example.com', 'john.doe@example.com')
|
184
192
|
end
|
185
193
|
|
194
|
+
def test_format_boolean
|
195
|
+
schema :string, format: :boolean
|
196
|
+
|
197
|
+
assert_json(type: :string, format: :boolean)
|
198
|
+
|
199
|
+
assert_cast 'true', true
|
200
|
+
assert_cast 'false', false
|
201
|
+
|
202
|
+
assert_cast nil, nil
|
203
|
+
end
|
204
|
+
|
186
205
|
def test_format_symbol
|
187
206
|
schema :string, format: :symbol
|
188
207
|
|
@@ -200,6 +219,80 @@ module Schemacop
|
|
200
219
|
assert_cast('039n23$g- sfk3/', :'039n23$g- sfk3/')
|
201
220
|
end
|
202
221
|
|
222
|
+
def test_format_integer
|
223
|
+
schema :string, format: :integer
|
224
|
+
|
225
|
+
assert_json(type: :string, format: :integer)
|
226
|
+
|
227
|
+
assert_validation '23425'
|
228
|
+
assert_validation '-23425'
|
229
|
+
|
230
|
+
assert_validation 12_312 do
|
231
|
+
error '/', StringNodeTest.invalid_type_error(Integer)
|
232
|
+
end
|
233
|
+
|
234
|
+
assert_validation '24.32' do
|
235
|
+
error '/', 'String does not match format "integer".'
|
236
|
+
end
|
237
|
+
|
238
|
+
assert_cast(nil, nil)
|
239
|
+
assert_cast('2234', 2234)
|
240
|
+
assert_cast('-1', -1)
|
241
|
+
assert_cast('-0', 0)
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_format_integer_list
|
245
|
+
schema :string, format: :integer_list
|
246
|
+
|
247
|
+
assert_json(type: :string, format: :'integer-list')
|
248
|
+
|
249
|
+
assert_validation '1,2,3,4'
|
250
|
+
assert_validation '1,2,-3,-54'
|
251
|
+
assert_validation '2'
|
252
|
+
assert_validation '-2'
|
253
|
+
|
254
|
+
assert_validation 234 do
|
255
|
+
error '/', StringNodeTest.invalid_type_error(Integer)
|
256
|
+
end
|
257
|
+
|
258
|
+
assert_validation 'sd sfdij soidf' do
|
259
|
+
error '/', 'String does not match format "integer-list".'
|
260
|
+
end
|
261
|
+
|
262
|
+
assert_cast nil, nil
|
263
|
+
assert_cast '1,-2,3', [1, -2, 3]
|
264
|
+
assert_cast '1', [1]
|
265
|
+
assert_cast '-1', [-1]
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_format_custom
|
269
|
+
Schemacop.register_string_formatter(
|
270
|
+
:integer_tuple_list,
|
271
|
+
pattern: /^(-?[0-9]+):(-?[0-9]+)(,(-?[0-9]+):(-?[0-9]+))*$/,
|
272
|
+
handler: proc do |value|
|
273
|
+
value.split(',').map { |t| t.split(':').map(&:to_i) }
|
274
|
+
end
|
275
|
+
)
|
276
|
+
|
277
|
+
schema :string, format: :integer_tuple_list
|
278
|
+
|
279
|
+
assert_json(type: :string, format: :'integer-tuple-list')
|
280
|
+
|
281
|
+
assert_validation '1:5,4:2,-4:4,4:-1,0:0'
|
282
|
+
assert_validation '-1:5'
|
283
|
+
|
284
|
+
assert_validation 234 do
|
285
|
+
error '/', StringNodeTest.invalid_type_error(Integer)
|
286
|
+
end
|
287
|
+
|
288
|
+
assert_validation 'sd sfdij soidf' do
|
289
|
+
error '/', 'String does not match format "integer-tuple-list".'
|
290
|
+
end
|
291
|
+
|
292
|
+
assert_cast nil, nil
|
293
|
+
assert_cast '1:2,3:4,5:-6', [[1, 2], [3, 4], [5, -6]]
|
294
|
+
end
|
295
|
+
|
203
296
|
def test_enum
|
204
297
|
schema :string, enum: ['foo', 'some string', 'some other string', 42]
|
205
298
|
|
@@ -224,33 +317,6 @@ module Schemacop
|
|
224
317
|
end
|
225
318
|
end
|
226
319
|
|
227
|
-
def test_boolean_casting
|
228
|
-
schema :string, format: :boolean
|
229
|
-
|
230
|
-
assert_json(type: :string, format: :boolean)
|
231
|
-
|
232
|
-
assert_cast 'true', true
|
233
|
-
assert_cast 'false', false
|
234
|
-
|
235
|
-
assert_cast nil, nil
|
236
|
-
end
|
237
|
-
|
238
|
-
def test_time_casting
|
239
|
-
schema :string, format: :time
|
240
|
-
assert_json(type: :string, format: :time)
|
241
|
-
assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
|
242
|
-
|
243
|
-
assert_cast nil, nil
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_date_casting
|
247
|
-
schema :string, format: :date
|
248
|
-
assert_json(type: :string, format: :date)
|
249
|
-
assert_cast '2018-11-13', Date.new(2018, 11, 13)
|
250
|
-
|
251
|
-
assert_cast nil, nil
|
252
|
-
end
|
253
|
-
|
254
320
|
def test_date_time_casting
|
255
321
|
schema :string, format: :date_time
|
256
322
|
assert_json(type: :string, format: :'date-time')
|
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.15
|
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
|