schemacop 2.4.5 → 3.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +25 -1
- data/.travis.yml +3 -1
- data/CHANGELOG.md +32 -1
- data/README.md +53 -710
- data/README_V2.md +775 -0
- data/README_V3.md +1195 -0
- data/Rakefile +8 -12
- data/VERSION +1 -1
- data/lib/schemacop.rb +35 -36
- data/lib/schemacop/base_schema.rb +37 -0
- data/lib/schemacop/railtie.rb +10 -0
- data/lib/schemacop/schema.rb +1 -60
- data/lib/schemacop/schema2.rb +22 -0
- data/lib/schemacop/schema3.rb +21 -0
- data/lib/schemacop/scoped_env.rb +25 -13
- data/lib/schemacop/v2.rb +26 -0
- data/lib/schemacop/{caster.rb → v2/caster.rb} +16 -2
- data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
- data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
- data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
- data/lib/schemacop/v2/node.rb +142 -0
- data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
- data/lib/schemacop/v2/node_supporting_field.rb +70 -0
- data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +14 -11
- data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
- data/lib/schemacop/v2/root_node.rb +6 -0
- data/lib/schemacop/v2/validator/array_validator.rb +32 -0
- data/lib/schemacop/{validator → v2/validator}/boolean_validator.rb +1 -1
- data/lib/schemacop/v2/validator/float_validator.rb +7 -0
- data/lib/schemacop/v2/validator/hash_validator.rb +37 -0
- data/lib/schemacop/v2/validator/integer_validator.rb +7 -0
- data/lib/schemacop/{validator → v2/validator}/nil_validator.rb +1 -1
- data/lib/schemacop/v2/validator/number_validator.rb +21 -0
- data/lib/schemacop/v2/validator/object_validator.rb +29 -0
- data/lib/schemacop/v2/validator/string_validator.rb +39 -0
- data/lib/schemacop/{validator → v2/validator}/symbol_validator.rb +1 -1
- data/lib/schemacop/v3.rb +45 -0
- data/lib/schemacop/v3/all_of_node.rb +27 -0
- data/lib/schemacop/v3/any_of_node.rb +28 -0
- data/lib/schemacop/v3/array_node.rb +218 -0
- data/lib/schemacop/v3/boolean_node.rb +16 -0
- data/lib/schemacop/v3/combination_node.rb +45 -0
- data/lib/schemacop/v3/context.rb +17 -0
- data/lib/schemacop/v3/dsl_scope.rb +46 -0
- data/lib/schemacop/v3/global_context.rb +114 -0
- data/lib/schemacop/v3/hash_node.rb +256 -0
- data/lib/schemacop/v3/integer_node.rb +13 -0
- data/lib/schemacop/v3/is_not_node.rb +32 -0
- data/lib/schemacop/v3/node.rb +215 -0
- data/lib/schemacop/v3/node_registry.rb +49 -0
- data/lib/schemacop/v3/number_node.rb +18 -0
- data/lib/schemacop/v3/numeric_node.rb +76 -0
- data/lib/schemacop/v3/object_node.rb +40 -0
- data/lib/schemacop/v3/one_of_node.rb +28 -0
- data/lib/schemacop/v3/reference_node.rb +49 -0
- data/lib/schemacop/v3/result.rb +58 -0
- data/lib/schemacop/v3/string_node.rb +124 -0
- data/lib/schemacop/v3/symbol_node.rb +13 -0
- data/schemacop.gemspec +24 -27
- data/test/lib/test_helper.rb +152 -0
- data/test/schemas/nested/group.rb +6 -0
- data/test/schemas/user.rb +7 -0
- data/test/unit/schemacop/v2/casting_test.rb +120 -0
- data/test/unit/schemacop/v2/collector_test.rb +47 -0
- data/test/unit/schemacop/v2/custom_check_test.rb +95 -0
- data/test/unit/schemacop/v2/custom_if_test.rb +97 -0
- data/test/unit/schemacop/v2/defaults_test.rb +95 -0
- data/test/unit/schemacop/v2/empty_test.rb +16 -0
- data/test/unit/schemacop/v2/nil_dis_allow_test.rb +43 -0
- data/test/unit/schemacop/v2/node_resolver_test.rb +28 -0
- data/test/unit/schemacop/v2/short_forms_test.rb +351 -0
- data/test/unit/schemacop/v2/types_test.rb +88 -0
- data/test/unit/schemacop/v2/validator_array_test.rb +99 -0
- data/test/unit/schemacop/v2/validator_boolean_test.rb +17 -0
- data/test/unit/schemacop/v2/validator_float_test.rb +59 -0
- data/test/unit/schemacop/v2/validator_hash_test.rb +95 -0
- data/test/unit/schemacop/v2/validator_integer_test.rb +48 -0
- data/test/unit/schemacop/v2/validator_nil_test.rb +15 -0
- data/test/unit/schemacop/v2/validator_number_test.rb +62 -0
- data/test/unit/schemacop/v2/validator_object_test.rb +141 -0
- data/test/unit/schemacop/v2/validator_string_test.rb +78 -0
- data/test/unit/schemacop/v2/validator_symbol_test.rb +18 -0
- data/test/unit/schemacop/v3/all_of_node_test.rb +198 -0
- data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
- data/test/unit/schemacop/v3/array_node_test.rb +815 -0
- data/test/unit/schemacop/v3/boolean_node_test.rb +126 -0
- data/test/unit/schemacop/v3/global_context_test.rb +164 -0
- data/test/unit/schemacop/v3/hash_node_test.rb +884 -0
- data/test/unit/schemacop/v3/integer_node_test.rb +323 -0
- data/test/unit/schemacop/v3/is_not_node_test.rb +173 -0
- data/test/unit/schemacop/v3/node_test.rb +148 -0
- data/test/unit/schemacop/v3/number_node_test.rb +292 -0
- data/test/unit/schemacop/v3/object_node_test.rb +170 -0
- data/test/unit/schemacop/v3/one_of_node_test.rb +187 -0
- data/test/unit/schemacop/v3/reference_node_test.rb +351 -0
- data/test/unit/schemacop/v3/string_node_test.rb +334 -0
- data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
- metadata +152 -145
- data/doc/Schemacop.html +0 -146
- data/doc/Schemacop/ArrayValidator.html +0 -329
- data/doc/Schemacop/BooleanValidator.html +0 -145
- data/doc/Schemacop/Caster.html +0 -379
- data/doc/Schemacop/Collector.html +0 -787
- data/doc/Schemacop/Dupper.html +0 -214
- data/doc/Schemacop/Exceptions.html +0 -115
- data/doc/Schemacop/Exceptions/InvalidSchemaError.html +0 -124
- data/doc/Schemacop/Exceptions/ValidationError.html +0 -124
- data/doc/Schemacop/FieldNode.html +0 -421
- data/doc/Schemacop/FloatValidator.html +0 -158
- data/doc/Schemacop/HashValidator.html +0 -293
- data/doc/Schemacop/IntegerValidator.html +0 -158
- data/doc/Schemacop/NilValidator.html +0 -145
- data/doc/Schemacop/Node.html +0 -1438
- data/doc/Schemacop/NodeResolver.html +0 -258
- data/doc/Schemacop/NodeSupportingField.html +0 -590
- data/doc/Schemacop/NodeSupportingType.html +0 -612
- data/doc/Schemacop/NodeWithBlock.html +0 -289
- data/doc/Schemacop/NumberValidator.html +0 -232
- data/doc/Schemacop/ObjectValidator.html +0 -298
- data/doc/Schemacop/RootNode.html +0 -171
- data/doc/Schemacop/Schema.html +0 -699
- data/doc/Schemacop/StringValidator.html +0 -295
- data/doc/Schemacop/SymbolValidator.html +0 -145
- data/doc/ScopedEnv.html +0 -351
- data/doc/_index.html +0 -379
- data/doc/class_list.html +0 -51
- data/doc/css/common.css +0 -1
- data/doc/css/full_list.css +0 -58
- data/doc/css/style.css +0 -496
- data/doc/file.README.html +0 -833
- data/doc/file_list.html +0 -56
- data/doc/frames.html +0 -17
- data/doc/index.html +0 -833
- data/doc/inheritance.graphml +0 -524
- data/doc/inheritance.pdf +0 -825
- data/doc/js/app.js +0 -303
- data/doc/js/full_list.js +0 -216
- data/doc/js/jquery.js +0 -4
- data/doc/method_list.html +0 -587
- data/doc/top-level-namespace.html +0 -112
- data/lib/schemacop/node.rb +0 -139
- data/lib/schemacop/node_supporting_field.rb +0 -58
- data/lib/schemacop/root_node.rb +0 -4
- data/lib/schemacop/validator/array_validator.rb +0 -30
- data/lib/schemacop/validator/float_validator.rb +0 -5
- data/lib/schemacop/validator/hash_validator.rb +0 -35
- data/lib/schemacop/validator/integer_validator.rb +0 -5
- data/lib/schemacop/validator/number_validator.rb +0 -19
- data/lib/schemacop/validator/object_validator.rb +0 -27
- data/lib/schemacop/validator/string_validator.rb +0 -37
- data/test/casting_test.rb +0 -90
- data/test/collector_test.rb +0 -45
- data/test/custom_check_test.rb +0 -93
- data/test/custom_if_test.rb +0 -95
- data/test/defaults_test.rb +0 -93
- data/test/empty_test.rb +0 -14
- data/test/nil_dis_allow_test.rb +0 -41
- data/test/node_resolver_test.rb +0 -26
- data/test/short_forms_test.rb +0 -349
- data/test/test_helper.rb +0 -13
- data/test/types_test.rb +0 -84
- data/test/validator_array_test.rb +0 -97
- data/test/validator_boolean_test.rb +0 -15
- data/test/validator_float_test.rb +0 -57
- data/test/validator_hash_test.rb +0 -93
- data/test/validator_integer_test.rb +0 -46
- data/test/validator_nil_test.rb +0 -13
- data/test/validator_number_test.rb +0 -60
- data/test/validator_object_test.rb +0 -139
- data/test/validator_string_test.rb +0 -76
- data/test/validator_symbol_test.rb +0 -16
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class ValidatorStringTest < V2Test
|
6
|
+
def test_basic
|
7
|
+
s = Schema.new do
|
8
|
+
type :string
|
9
|
+
end
|
10
|
+
assert_nothing_raised { s.validate! '' }
|
11
|
+
assert_nothing_raised { s.validate! 'abc' }
|
12
|
+
assert_nothing_raised { s.validate! 123.to_s }
|
13
|
+
assert_nothing_raised { s.validate! inspect }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_option_min
|
17
|
+
s = Schema.new do
|
18
|
+
type :string, min: 3
|
19
|
+
end
|
20
|
+
|
21
|
+
assert_nothing_raised { s.validate!('abc') }
|
22
|
+
assert_nothing_raised { s.validate!('abcd') }
|
23
|
+
assert_verr { s.validate!('ab') }
|
24
|
+
assert_verr { s.validate!('') }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_option_max
|
28
|
+
s = Schema.new do
|
29
|
+
type :string, max: 5
|
30
|
+
end
|
31
|
+
|
32
|
+
assert_nothing_raised { s.validate!('') }
|
33
|
+
assert_nothing_raised { s.validate!('abcd') }
|
34
|
+
assert_nothing_raised { s.validate!('abcde') }
|
35
|
+
assert_verr { s.validate!('abcdef') }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_options_min_max
|
39
|
+
s = Schema.new do
|
40
|
+
type :string, min: 3, max: 5
|
41
|
+
end
|
42
|
+
|
43
|
+
assert_nothing_raised { s.validate!('abc') }
|
44
|
+
assert_nothing_raised { s.validate!('abcd') }
|
45
|
+
assert_nothing_raised { s.validate!('abcde') }
|
46
|
+
assert_verr { s.validate!('ab') }
|
47
|
+
assert_verr { s.validate!('abcdef') }
|
48
|
+
assert_verr { s.validate!('') }
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_float_options
|
52
|
+
assert_raises Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.' do
|
53
|
+
Schema.new do
|
54
|
+
type :string, min: 3.2
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
assert_raises Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.' do
|
59
|
+
Schema.new do
|
60
|
+
type :string, max: 5.2
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
assert_raises Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.' do
|
65
|
+
Schema.new do
|
66
|
+
type :string, min: 3.2, max: 5
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
assert_raises Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.' do
|
71
|
+
Schema.new do
|
72
|
+
type :string, min: 3, max: 5.2
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class ValidatorSymbolTest < V2Test
|
6
|
+
def test_basic
|
7
|
+
s = Schema.new do
|
8
|
+
type :symbol
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_nothing_raised { s.validate!(:good) }
|
12
|
+
assert_nothing_raised { s.validate!('-+/'.to_sym) }
|
13
|
+
assert_verr { s.validate!('bad') }
|
14
|
+
assert_verr { s.validate!(456) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V3
|
5
|
+
class AllOfNodeTest < V3Test
|
6
|
+
def test_optional
|
7
|
+
schema :all_of do
|
8
|
+
str min_length: 2
|
9
|
+
str max_length: 4
|
10
|
+
end
|
11
|
+
|
12
|
+
assert_validation(nil)
|
13
|
+
assert_validation('12')
|
14
|
+
assert_validation('1234')
|
15
|
+
assert_validation('1') do
|
16
|
+
error '/', 'Does not match all allOf conditions.'
|
17
|
+
end
|
18
|
+
assert_validation('12345') do
|
19
|
+
error '/', 'Does not match all allOf conditions.'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_required
|
24
|
+
schema :all_of, required: true do
|
25
|
+
int minimum: 2
|
26
|
+
int maximum: 4
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_validation(2)
|
30
|
+
assert_validation(3)
|
31
|
+
assert_validation(4)
|
32
|
+
|
33
|
+
assert_validation(5) do
|
34
|
+
error '/', 'Does not match all allOf conditions.'
|
35
|
+
end
|
36
|
+
assert_validation(1) do
|
37
|
+
error '/', 'Does not match all allOf conditions.'
|
38
|
+
end
|
39
|
+
assert_validation({}) do
|
40
|
+
error '/', 'Does not match all allOf conditions.'
|
41
|
+
end
|
42
|
+
assert_validation(42) do
|
43
|
+
error '/', 'Does not match all allOf conditions.'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_nested
|
48
|
+
schema :all_of do
|
49
|
+
hsh additional_properties: true do
|
50
|
+
all_of! :foo do
|
51
|
+
int minimum: 2
|
52
|
+
int maximum: 4
|
53
|
+
end
|
54
|
+
end
|
55
|
+
hsh additional_properties: true do
|
56
|
+
any_of! :bar do
|
57
|
+
int minimum: 6
|
58
|
+
int maximum: 8
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
assert_validation(foo: 2, bar: 7)
|
64
|
+
assert_validation(foo: 5, bar: 7) do
|
65
|
+
error '/', 'Does not match all allOf conditions.'
|
66
|
+
end
|
67
|
+
assert_validation(foo: 5) do
|
68
|
+
error '/', 'Does not match all allOf conditions.'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_simple_casting
|
73
|
+
schema :all_of do
|
74
|
+
str format: :integer
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_validation(nil)
|
78
|
+
assert_validation('1')
|
79
|
+
assert_validation('Foo') do
|
80
|
+
error '/', 'Does not match all allOf conditions.'
|
81
|
+
end
|
82
|
+
|
83
|
+
assert_cast('1', 1)
|
84
|
+
assert_cast('1337', 1337)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_casting_with_conditions
|
88
|
+
schema :all_of do
|
89
|
+
str format: :integer
|
90
|
+
str min_length: 2
|
91
|
+
str max_length: 3
|
92
|
+
end
|
93
|
+
|
94
|
+
assert_validation(nil)
|
95
|
+
assert_validation('12')
|
96
|
+
assert_validation('123')
|
97
|
+
|
98
|
+
assert_validation('1') do
|
99
|
+
error '/', 'Does not match all allOf conditions.'
|
100
|
+
end
|
101
|
+
|
102
|
+
assert_validation('1234') do
|
103
|
+
error '/', 'Does not match all allOf conditions.'
|
104
|
+
end
|
105
|
+
assert_cast('42', 42)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_casting_with_conditions_changed_order
|
109
|
+
schema :all_of do
|
110
|
+
str min_length: 2
|
111
|
+
str max_length: 3
|
112
|
+
str format: :integer
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_validation(nil)
|
116
|
+
assert_validation('12')
|
117
|
+
assert_validation('123')
|
118
|
+
|
119
|
+
assert_validation('1') do
|
120
|
+
error '/', 'Does not match all allOf conditions.'
|
121
|
+
end
|
122
|
+
|
123
|
+
assert_validation('1234') do
|
124
|
+
error '/', 'Does not match all allOf conditions.'
|
125
|
+
end
|
126
|
+
|
127
|
+
assert_cast('42', 42)
|
128
|
+
assert_cast('123', 123)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_hash_casting
|
132
|
+
schema :all_of do
|
133
|
+
hsh additional_properties: true do
|
134
|
+
str! :foo, format: :integer
|
135
|
+
end
|
136
|
+
hsh additional_properties: true do
|
137
|
+
str! :bar, format: :date
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
assert_validation(foo: '42', bar: '2020-01-15')
|
142
|
+
|
143
|
+
assert_cast(
|
144
|
+
{ foo: '42', bar: '2020-01-15' },
|
145
|
+
{ foo: 42, bar: Date.new(2020, 1, 15) }.with_indifferent_access
|
146
|
+
)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_nonsensical_casting
|
150
|
+
schema :all_of do
|
151
|
+
str format: :integer
|
152
|
+
str format: :boolean
|
153
|
+
end
|
154
|
+
|
155
|
+
assert_validation(nil)
|
156
|
+
assert_validation('42') do
|
157
|
+
error '/', 'Does not match all allOf conditions.'
|
158
|
+
end
|
159
|
+
assert_validation('true') do
|
160
|
+
error '/', 'Does not match all allOf conditions.'
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_defaults
|
165
|
+
schema :all_of do
|
166
|
+
str default: 'foobar'
|
167
|
+
end
|
168
|
+
|
169
|
+
assert_validation(nil)
|
170
|
+
assert_validation('Hello World')
|
171
|
+
|
172
|
+
assert_json({
|
173
|
+
allOf: [
|
174
|
+
type: :string,
|
175
|
+
default: 'foobar'
|
176
|
+
]
|
177
|
+
})
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_with_generic_keywords
|
181
|
+
schema :all_of, title: 'allOf schema',
|
182
|
+
description: 'allOf schema holding generic keywords',
|
183
|
+
examples: [
|
184
|
+
'foo'
|
185
|
+
]
|
186
|
+
|
187
|
+
assert_json({
|
188
|
+
allOf: [],
|
189
|
+
title: 'allOf schema',
|
190
|
+
description: 'allOf schema holding generic keywords',
|
191
|
+
examples: [
|
192
|
+
'foo'
|
193
|
+
]
|
194
|
+
})
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V3
|
5
|
+
class AnyOfNodeTest < V3Test
|
6
|
+
def test_optional
|
7
|
+
schema :any_of do
|
8
|
+
str
|
9
|
+
int
|
10
|
+
end
|
11
|
+
|
12
|
+
assert_validation(nil)
|
13
|
+
assert_validation('string')
|
14
|
+
assert_validation(42)
|
15
|
+
assert_validation(42.1) do
|
16
|
+
error '/', 'Does not match any anyOf condition.'
|
17
|
+
end
|
18
|
+
assert_validation({}) do
|
19
|
+
error '/', 'Does not match any anyOf condition.'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_required
|
24
|
+
schema :any_of, required: true do
|
25
|
+
str
|
26
|
+
int
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_validation('string')
|
30
|
+
assert_validation(42)
|
31
|
+
|
32
|
+
assert_validation(nil) do
|
33
|
+
error '/', 'Value must be given.'
|
34
|
+
end
|
35
|
+
|
36
|
+
assert_validation(42.1) do
|
37
|
+
error '/', 'Does not match any anyOf condition.'
|
38
|
+
end
|
39
|
+
assert_validation({}) do
|
40
|
+
error '/', 'Does not match any anyOf condition.'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_nested
|
45
|
+
schema :any_of do
|
46
|
+
hsh do
|
47
|
+
any_of! :foo do
|
48
|
+
int
|
49
|
+
str
|
50
|
+
end
|
51
|
+
end
|
52
|
+
int
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_validation(nil)
|
56
|
+
assert_validation(42)
|
57
|
+
assert_validation(foo: 42)
|
58
|
+
assert_validation(foo: 'str')
|
59
|
+
assert_validation('string') do
|
60
|
+
error '/', 'Does not match any anyOf condition.'
|
61
|
+
end
|
62
|
+
assert_validation(bar: :baz) do
|
63
|
+
error '/', 'Does not match any anyOf condition.'
|
64
|
+
end
|
65
|
+
assert_validation(foo: :bar) do
|
66
|
+
error '/', 'Does not match any anyOf condition.'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_all_types
|
71
|
+
schema :any_of do
|
72
|
+
all_of do
|
73
|
+
hsh { str! :foo }
|
74
|
+
end
|
75
|
+
any_of do
|
76
|
+
obj(Date)
|
77
|
+
obj(Time)
|
78
|
+
end
|
79
|
+
ary
|
80
|
+
boo
|
81
|
+
int
|
82
|
+
num
|
83
|
+
hsh { int! :bar }
|
84
|
+
one_of do
|
85
|
+
hsh { str! :a, pattern: '^a' }
|
86
|
+
hsh { str! :a, pattern: 'z$' }
|
87
|
+
end
|
88
|
+
ref :MyDate
|
89
|
+
str max_length: 2
|
90
|
+
end
|
91
|
+
|
92
|
+
context = Context.new
|
93
|
+
context.schema :MyDate, :string, format: :date
|
94
|
+
|
95
|
+
Schemacop.with_context context do
|
96
|
+
assert_validation(nil)
|
97
|
+
assert_validation(foo: 'str')
|
98
|
+
assert_validation([1, 2, 3])
|
99
|
+
assert_validation(true)
|
100
|
+
assert_validation(false)
|
101
|
+
assert_validation(42)
|
102
|
+
assert_validation(42.2)
|
103
|
+
assert_validation(bar: 42)
|
104
|
+
assert_validation(a: 'a hello')
|
105
|
+
assert_validation(a: 'hello z')
|
106
|
+
assert_validation('1990-01-13')
|
107
|
+
assert_validation('12')
|
108
|
+
assert_validation(Date.new(1990, 1, 13))
|
109
|
+
assert_validation(Time.now)
|
110
|
+
|
111
|
+
assert_validation('1990-01-13 12') do
|
112
|
+
error '/', 'Does not match any anyOf condition.'
|
113
|
+
end
|
114
|
+
assert_validation(a: 'a hello z') do
|
115
|
+
error '/', 'Does not match any anyOf condition.'
|
116
|
+
end
|
117
|
+
assert_validation(Object.new) do
|
118
|
+
error '/', 'Does not match any anyOf condition.'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_casting
|
124
|
+
schema do
|
125
|
+
any_of! :created_at do
|
126
|
+
str format: :date
|
127
|
+
str format: :date_time
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
assert_validation(created_at: '2020-01-01')
|
132
|
+
assert_validation(created_at: '2020-01-01T17:38:20')
|
133
|
+
|
134
|
+
assert_cast(
|
135
|
+
{ created_at: '2020-01-01' },
|
136
|
+
{ created_at: Date.new(2020, 1, 1) }.with_indifferent_access
|
137
|
+
)
|
138
|
+
assert_cast(
|
139
|
+
{ created_at: '2020-01-01T17:38:20' },
|
140
|
+
{ created_at: DateTime.new(2020, 1, 1, 17, 38, 20) }.with_indifferent_access
|
141
|
+
)
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_defaults
|
145
|
+
schema do
|
146
|
+
any_of! :foo do
|
147
|
+
hsh { str? :bar }
|
148
|
+
hsh { str? :baz, default: 'Baz' }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
assert_validation(foo: { bar: 'Bar' })
|
153
|
+
assert_validation(foo: { baz: 'Baz' })
|
154
|
+
|
155
|
+
assert_validation(foo: { xyz: 'Baz' }) do
|
156
|
+
error '/foo', 'Does not match any anyOf condition.'
|
157
|
+
end
|
158
|
+
|
159
|
+
assert_cast(
|
160
|
+
{ foo: { bar: nil } },
|
161
|
+
{ foo: { bar: nil } }.with_indifferent_access
|
162
|
+
)
|
163
|
+
|
164
|
+
assert_cast(
|
165
|
+
{ foo: { baz: nil } },
|
166
|
+
{ foo: { baz: 'Baz' } }.with_indifferent_access
|
167
|
+
)
|
168
|
+
|
169
|
+
schema do
|
170
|
+
any_of! :foo do
|
171
|
+
hsh { str? :bar, format: :date }
|
172
|
+
hsh { str? :bar, default: 'Baz', format: :date_time }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
assert_cast(
|
177
|
+
{ foo: { bar: '1990-01-13' } },
|
178
|
+
{ foo: { bar: Date.new(1990, 1, 13) } }.with_indifferent_access
|
179
|
+
)
|
180
|
+
|
181
|
+
assert_cast(
|
182
|
+
{ foo: { bar: '1990-01-13T10:00:00Z' } },
|
183
|
+
{ foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) } }.with_indifferent_access
|
184
|
+
)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_with_generic_keywords
|
188
|
+
schema :any_of, title: 'anyOf schema',
|
189
|
+
description: 'anyOf schema holding generic keywords',
|
190
|
+
examples: [
|
191
|
+
'foo'
|
192
|
+
] do
|
193
|
+
str
|
194
|
+
int
|
195
|
+
end
|
196
|
+
|
197
|
+
assert_json({
|
198
|
+
anyOf: [
|
199
|
+
{ type: :string },
|
200
|
+
{ type: :integer }
|
201
|
+
],
|
202
|
+
title: 'anyOf schema',
|
203
|
+
description: 'anyOf schema holding generic keywords',
|
204
|
+
examples: [
|
205
|
+
'foo'
|
206
|
+
]
|
207
|
+
})
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_invalid_schema
|
211
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
212
|
+
'Node "any_of" makes only sense with at least 1 item.' do
|
213
|
+
schema :any_of
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|