schemacop 2.4.6 → 3.0.0.rc3
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/.releaser_config +0 -1
- data/.rubocop.yml +25 -1
- data/.travis.yml +3 -1
- data/CHANGELOG.md +33 -0
- data/README.md +53 -710
- data/README_V2.md +775 -0
- data/README_V3.md +1197 -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 +25 -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} +8 -5
- data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
- data/lib/schemacop/v2/root_node.rb +0 -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 +258 -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 +219 -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 +157 -150
- 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 -100
- 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,323 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V3
|
5
|
+
class IntegerNodeTest < V3Test
|
6
|
+
EXP_INVALID_TYPE = 'Invalid type, expected "integer".'.freeze
|
7
|
+
|
8
|
+
def test_basic
|
9
|
+
schema :integer
|
10
|
+
|
11
|
+
assert_json(type: :integer)
|
12
|
+
|
13
|
+
assert_validation 25
|
14
|
+
assert_validation(-14)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_hash
|
18
|
+
schema { int! :age }
|
19
|
+
|
20
|
+
assert_json(
|
21
|
+
type: :object,
|
22
|
+
properties: {
|
23
|
+
age: { type: :integer }
|
24
|
+
},
|
25
|
+
required: %i[age],
|
26
|
+
additionalProperties: false
|
27
|
+
)
|
28
|
+
|
29
|
+
assert_validation age: 30
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_array
|
33
|
+
schema(:array) { int }
|
34
|
+
|
35
|
+
assert_json(
|
36
|
+
type: :array,
|
37
|
+
items: [
|
38
|
+
{
|
39
|
+
type: :integer
|
40
|
+
}
|
41
|
+
],
|
42
|
+
additionalItems: false
|
43
|
+
)
|
44
|
+
|
45
|
+
assert_validation [30]
|
46
|
+
assert_validation [30, 42] do
|
47
|
+
error '/', 'Array has 2 items but must have exactly 1.'
|
48
|
+
end
|
49
|
+
assert_validation [30, '30'] do
|
50
|
+
error '/', 'Array has 2 items but must have exactly 1.'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_type
|
55
|
+
schema :integer
|
56
|
+
|
57
|
+
assert_json(
|
58
|
+
type: :integer
|
59
|
+
)
|
60
|
+
|
61
|
+
assert_validation 42.5 do
|
62
|
+
error '/', EXP_INVALID_TYPE
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_validation '42.5' do
|
66
|
+
error '/', EXP_INVALID_TYPE
|
67
|
+
end
|
68
|
+
|
69
|
+
schema { int! :age }
|
70
|
+
|
71
|
+
assert_json(
|
72
|
+
type: :object,
|
73
|
+
properties: {
|
74
|
+
age: { type: :integer }
|
75
|
+
},
|
76
|
+
required: %i[age],
|
77
|
+
additionalProperties: false
|
78
|
+
)
|
79
|
+
|
80
|
+
assert_validation age: :foo do
|
81
|
+
error '/age', EXP_INVALID_TYPE
|
82
|
+
end
|
83
|
+
|
84
|
+
assert_validation age: '234' do
|
85
|
+
error '/age', EXP_INVALID_TYPE
|
86
|
+
end
|
87
|
+
|
88
|
+
assert_validation age: 10.0 do
|
89
|
+
error '/age', EXP_INVALID_TYPE
|
90
|
+
end
|
91
|
+
|
92
|
+
assert_validation age: 4r do
|
93
|
+
error '/age', EXP_INVALID_TYPE
|
94
|
+
end
|
95
|
+
|
96
|
+
assert_validation age: (4 + 0i) do
|
97
|
+
error '/age', EXP_INVALID_TYPE
|
98
|
+
end
|
99
|
+
|
100
|
+
assert_validation age: BigDecimal(5) do
|
101
|
+
error '/age', EXP_INVALID_TYPE
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_minimum
|
106
|
+
schema :integer, minimum: 0
|
107
|
+
|
108
|
+
assert_json(
|
109
|
+
type: :integer,
|
110
|
+
minimum: 0
|
111
|
+
)
|
112
|
+
|
113
|
+
assert_validation 5
|
114
|
+
assert_validation 0
|
115
|
+
assert_validation(-1) do
|
116
|
+
error '/', 'Value must have a minimum of 0.'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_exclusive_minimum
|
121
|
+
schema :integer, exclusive_minimum: 0
|
122
|
+
|
123
|
+
assert_json(
|
124
|
+
type: :integer,
|
125
|
+
exclusiveMinimum: 0
|
126
|
+
)
|
127
|
+
|
128
|
+
assert_validation 5
|
129
|
+
assert_validation 1
|
130
|
+
assert_validation(0) do
|
131
|
+
error '/', 'Value must have an exclusive minimum of 0.'
|
132
|
+
end
|
133
|
+
assert_validation(-5) do
|
134
|
+
error '/', 'Value must have an exclusive minimum of 0.'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_maximum
|
139
|
+
schema :integer, maximum: 5
|
140
|
+
|
141
|
+
assert_json(
|
142
|
+
type: :integer,
|
143
|
+
maximum: 5
|
144
|
+
)
|
145
|
+
|
146
|
+
assert_validation 5
|
147
|
+
assert_validation 0
|
148
|
+
assert_validation(6) do
|
149
|
+
error '/', 'Value must have a maximum of 5.'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_exclusive_maximum
|
154
|
+
schema :integer, exclusive_maximum: 5
|
155
|
+
|
156
|
+
assert_json(
|
157
|
+
type: :integer,
|
158
|
+
exclusiveMaximum: 5
|
159
|
+
)
|
160
|
+
|
161
|
+
assert_validation 4
|
162
|
+
assert_validation 1
|
163
|
+
assert_validation(5) do
|
164
|
+
error '/', 'Value must have an exclusive maximum of 5.'
|
165
|
+
end
|
166
|
+
assert_validation(9) do
|
167
|
+
error '/', 'Value must have an exclusive maximum of 5.'
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_multiple_of
|
172
|
+
schema :integer, multiple_of: 2
|
173
|
+
|
174
|
+
assert_json(
|
175
|
+
type: :integer,
|
176
|
+
multipleOf: 2
|
177
|
+
)
|
178
|
+
|
179
|
+
assert_validation(-4)
|
180
|
+
assert_validation(-2)
|
181
|
+
assert_validation(0)
|
182
|
+
assert_validation(2)
|
183
|
+
assert_validation(300)
|
184
|
+
|
185
|
+
assert_validation(5) do
|
186
|
+
error '/', 'Value must be a multiple of 2.'
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_default
|
191
|
+
schema :integer, default: 5
|
192
|
+
|
193
|
+
assert_json(
|
194
|
+
type: :integer,
|
195
|
+
default: 5
|
196
|
+
)
|
197
|
+
|
198
|
+
assert_validation(nil)
|
199
|
+
assert_validation(50)
|
200
|
+
assert_validation(5.2) do
|
201
|
+
error '/', 'Invalid type, expected "integer".'
|
202
|
+
end
|
203
|
+
|
204
|
+
assert_cast(5, 5)
|
205
|
+
assert_cast(6, 6)
|
206
|
+
assert_cast(nil, 5)
|
207
|
+
end
|
208
|
+
|
209
|
+
# Helper function that checks for all the options if the option is
|
210
|
+
# an integer or something else, in which case it needs to raise
|
211
|
+
def validate_self_should_error(value_to_check)
|
212
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
213
|
+
'Option "minimum" must be a "integer"' do
|
214
|
+
schema :integer, minimum: value_to_check
|
215
|
+
end
|
216
|
+
|
217
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
218
|
+
'Option "maximum" must be a "integer"' do
|
219
|
+
schema :integer, maximum: value_to_check
|
220
|
+
end
|
221
|
+
|
222
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
223
|
+
'Option "exclusive_minimum" must be a "integer"' do
|
224
|
+
schema :integer, exclusive_minimum: value_to_check
|
225
|
+
end
|
226
|
+
|
227
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
228
|
+
'Option "exclusive_maximum" must be a "integer"' do
|
229
|
+
schema :integer, exclusive_maximum: value_to_check
|
230
|
+
end
|
231
|
+
|
232
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
233
|
+
'Option "multiple_of" must be a "integer"' do
|
234
|
+
schema :integer, multiple_of: value_to_check
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_validate_self
|
239
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
240
|
+
'Option "minimum" can\'t be greater than "maximum".' do
|
241
|
+
schema :integer, minimum: 5, maximum: 4
|
242
|
+
end
|
243
|
+
|
244
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
245
|
+
'Option "exclusive_minimum" can\'t be '\
|
246
|
+
'greater than "exclusive_maximum".' do
|
247
|
+
schema :integer, exclusive_minimum: 5, exclusive_maximum: 4
|
248
|
+
end
|
249
|
+
|
250
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
251
|
+
'Option "multiple_of" can\'t be 0.' do
|
252
|
+
schema :integer, multiple_of: 0
|
253
|
+
end
|
254
|
+
|
255
|
+
validate_self_should_error(1.0) # Float
|
256
|
+
validate_self_should_error(1r) # Rational
|
257
|
+
validate_self_should_error(1 + 0i) # Complex
|
258
|
+
validate_self_should_error(BigDecimal(5)) # BigDecimal
|
259
|
+
validate_self_should_error(Object.new)
|
260
|
+
validate_self_should_error(true)
|
261
|
+
validate_self_should_error(false)
|
262
|
+
validate_self_should_error('1')
|
263
|
+
validate_self_should_error('String')
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_enum_schema
|
267
|
+
schema :integer, enum: [1, 2, 'foo', :bar, { qux: 42 }]
|
268
|
+
|
269
|
+
assert_json({
|
270
|
+
type: :integer,
|
271
|
+
enum: [1, 2, 'foo', :bar, { qux: 42 }]
|
272
|
+
})
|
273
|
+
|
274
|
+
assert_validation(nil)
|
275
|
+
assert_validation(1)
|
276
|
+
|
277
|
+
# Even we put those types in the enum, they need to fail the validations,
|
278
|
+
# as they are not integers
|
279
|
+
assert_validation('foo') do
|
280
|
+
error '/', 'Invalid type, expected "integer".'
|
281
|
+
end
|
282
|
+
assert_validation(:bar) do
|
283
|
+
error '/', 'Invalid type, expected "integer".'
|
284
|
+
end
|
285
|
+
assert_validation({ qux: 42 }) do
|
286
|
+
error '/', 'Invalid type, expected "integer".'
|
287
|
+
end
|
288
|
+
|
289
|
+
# This needs to fail as it is a number (float) and not an integer
|
290
|
+
assert_validation(4.2) do
|
291
|
+
error '/', 'Invalid type, expected "integer".'
|
292
|
+
end
|
293
|
+
|
294
|
+
# These need to fail validation, as they are not in the enum list
|
295
|
+
assert_validation(13) do
|
296
|
+
error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
|
297
|
+
end
|
298
|
+
assert_validation(4) do
|
299
|
+
error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_with_generic_keywords
|
304
|
+
schema :integer, enum: [1, 'foo'],
|
305
|
+
title: 'Integer schema',
|
306
|
+
description: 'Integer schema holding generic keywords',
|
307
|
+
examples: [
|
308
|
+
1
|
309
|
+
]
|
310
|
+
|
311
|
+
assert_json({
|
312
|
+
type: :integer,
|
313
|
+
enum: [1, 'foo'],
|
314
|
+
title: 'Integer schema',
|
315
|
+
description: 'Integer schema holding generic keywords',
|
316
|
+
examples: [
|
317
|
+
1
|
318
|
+
]
|
319
|
+
})
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V3
|
5
|
+
class IsNotNodeTest < V3Test
|
6
|
+
def test_optional
|
7
|
+
schema :is_not do
|
8
|
+
int minimum: 5
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_json(
|
12
|
+
not: { type: :integer, minimum: 5 }
|
13
|
+
)
|
14
|
+
|
15
|
+
assert_validation(nil)
|
16
|
+
assert_validation({})
|
17
|
+
assert_validation(:foo)
|
18
|
+
assert_validation(4)
|
19
|
+
assert_validation(8) do
|
20
|
+
error '/', 'Must not match schema: {"type"=>"integer", "minimum"=>5}.'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_required
|
25
|
+
schema :is_not, required: true do
|
26
|
+
int minimum: 5
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_json(
|
30
|
+
not: { type: :integer, minimum: 5 }
|
31
|
+
)
|
32
|
+
|
33
|
+
assert_validation(:foo)
|
34
|
+
assert_validation(4)
|
35
|
+
assert_validation(nil) do
|
36
|
+
error '/', 'Value must be given.'
|
37
|
+
end
|
38
|
+
assert_validation({})
|
39
|
+
assert_validation(8) do
|
40
|
+
error '/', 'Must not match schema: {"type"=>"integer", "minimum"=>5}.'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_enum_schema
|
45
|
+
schema :is_not do
|
46
|
+
str enum: ['bar', 'qux', 123, :faz]
|
47
|
+
end
|
48
|
+
|
49
|
+
assert_json({
|
50
|
+
not: {
|
51
|
+
type: :string,
|
52
|
+
enum: ['bar', 'qux', 123, :faz]
|
53
|
+
}
|
54
|
+
})
|
55
|
+
assert_validation(nil)
|
56
|
+
assert_validation({ foo: 'bar' })
|
57
|
+
assert_validation('baz')
|
58
|
+
assert_validation(123)
|
59
|
+
assert_validation(:faz)
|
60
|
+
|
61
|
+
# Needs to fail the validation, as the value is included in the enum
|
62
|
+
# and a string (which the not turns into "failing the validation")
|
63
|
+
assert_validation('bar') do
|
64
|
+
error '/', 'Must not match schema: {"type"=>"string", "enum"=>["bar", "qux", 123, "faz"]}.'
|
65
|
+
end
|
66
|
+
|
67
|
+
assert_validation('qux') do
|
68
|
+
error '/', 'Must not match schema: {"type"=>"string", "enum"=>["bar", "qux", 123, "faz"]}.'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_with_generic_keywords
|
73
|
+
schema :is_not do
|
74
|
+
str enum: ['bar', 'qux', 123, :faz], title: 'Short title', description: 'Longer description', examples: ['foo']
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_json({
|
78
|
+
not: {
|
79
|
+
type: :string,
|
80
|
+
enum: ['bar', 'qux', 123, :faz],
|
81
|
+
title: 'Short title',
|
82
|
+
description: 'Longer description',
|
83
|
+
examples: ['foo']
|
84
|
+
}
|
85
|
+
})
|
86
|
+
|
87
|
+
# rubocop:disable Layout/LineLength
|
88
|
+
|
89
|
+
# Needs to fail the validation, as the value is included in the enum
|
90
|
+
# and a string (which the not turns into "failing the validation")
|
91
|
+
assert_validation('bar') do
|
92
|
+
error '/', 'Must not match schema: {"type"=>"string", "title"=>"Short title", "examples"=>["foo"], "description"=>"Longer description", "enum"=>["bar", "qux", 123, "faz"]}.'
|
93
|
+
end
|
94
|
+
|
95
|
+
assert_validation('qux') do
|
96
|
+
error '/', 'Must not match schema: {"type"=>"string", "title"=>"Short title", "examples"=>["foo"], "description"=>"Longer description", "enum"=>["bar", "qux", 123, "faz"]}.'
|
97
|
+
end
|
98
|
+
|
99
|
+
# rubocop:enable Layout/LineLength
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_not_object_json
|
103
|
+
schema :is_not do
|
104
|
+
hsh do
|
105
|
+
int! :foo
|
106
|
+
str! :bar
|
107
|
+
add :integer
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
assert_json(
|
112
|
+
not: {
|
113
|
+
properties: {
|
114
|
+
foo: {
|
115
|
+
type: :integer
|
116
|
+
},
|
117
|
+
bar: {
|
118
|
+
type: :string
|
119
|
+
}
|
120
|
+
},
|
121
|
+
additionalProperties: {
|
122
|
+
type: :integer
|
123
|
+
},
|
124
|
+
required: %i[foo bar],
|
125
|
+
type: :object
|
126
|
+
}
|
127
|
+
)
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_not_array
|
131
|
+
schema :is_not do
|
132
|
+
ary do
|
133
|
+
list :integer
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_validation(nil)
|
138
|
+
assert_validation([]) do
|
139
|
+
error '/', 'Must not match schema: {"type"=>"array", "items"=>{"type"=>"integer"}}.'
|
140
|
+
end
|
141
|
+
assert_validation('foo')
|
142
|
+
assert_validation(['foo'])
|
143
|
+
assert_validation([1]) do
|
144
|
+
error '/', 'Must not match schema: {"type"=>"array", "items"=>{"type"=>"integer"}}.'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_invalid_schema
|
149
|
+
assert_raises_with_message Exceptions::InvalidSchemaError,
|
150
|
+
'Node "is_not" only allows exactly one item.' do
|
151
|
+
schema :is_not do
|
152
|
+
int
|
153
|
+
str
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_casting
|
159
|
+
schema :is_not do
|
160
|
+
str format: :date
|
161
|
+
end
|
162
|
+
|
163
|
+
assert_validation(nil)
|
164
|
+
assert_validation([])
|
165
|
+
assert_validation(:bar)
|
166
|
+
|
167
|
+
assert_cast([], [])
|
168
|
+
assert_cast(1, 1)
|
169
|
+
assert_cast({ foo: :bar, baz: 'Str' }, { foo: :bar, baz: 'Str' })
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|