schemacop 3.0.8 → 3.0.12
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 +18 -0
- data/README.md +1 -1
- data/README_V3.md +129 -40
- data/VERSION +1 -1
- data/lib/schemacop/v3/combination_node.rb +1 -1
- data/lib/schemacop/v3/node.rb +6 -5
- data/lib/schemacop/v3/one_of_node.rb +23 -1
- data/lib/schemacop/v3/string_node.rb +8 -2
- data/schemacop.gemspec +30 -18
- data/test/unit/schemacop/v3/array_node_test.rb +19 -17
- data/test/unit/schemacop/v3/boolean_node_test.rb +40 -7
- data/test/unit/schemacop/v3/hash_node_test.rb +49 -11
- data/test/unit/schemacop/v3/integer_node_test.rb +38 -14
- data/test/unit/schemacop/v3/node_test.rb +10 -0
- data/test/unit/schemacop/v3/number_node_test.rb +14 -9
- data/test/unit/schemacop/v3/object_node_test.rb +7 -7
- data/test/unit/schemacop/v3/one_of_node_test.rb +12 -0
- data/test/unit/schemacop/v3/reference_node_test.rb +4 -4
- data/test/unit/schemacop/v3/string_node_test.rb +60 -11
- data/test/unit/schemacop/v3/symbol_node_test.rb +9 -6
- metadata +4 -4
- data/.travis.yml +0 -9
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.
|
1
|
+
3.0.12
|
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
|
@@ -206,7 +207,7 @@ module Schemacop
|
|
206
207
|
# Validate type #
|
207
208
|
if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
|
208
209
|
collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
|
209
|
-
result.error
|
210
|
+
result.error "Invalid type, got type \"#{data.class}\", expected #{collection}."
|
210
211
|
return nil
|
211
212
|
end
|
212
213
|
|
@@ -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)
|
@@ -22,7 +22,7 @@ module Schemacop
|
|
22
22
|
# rubocop:enable Layout/LineLength
|
23
23
|
|
24
24
|
def self.allowed_options
|
25
|
-
super + ATTRIBUTES + %i[format_options pattern]
|
25
|
+
super + ATTRIBUTES + %i[format_options pattern allow_blank]
|
26
26
|
end
|
27
27
|
|
28
28
|
def allowed_types
|
@@ -39,6 +39,12 @@ module Schemacop
|
|
39
39
|
|
40
40
|
def _validate(data, result:)
|
41
41
|
super_data = super
|
42
|
+
|
43
|
+
# Validate blank #
|
44
|
+
if options[:allow_blank].is_a?(FalseClass) && super_data.blank?
|
45
|
+
result.error 'String is blank but must not be blank!'
|
46
|
+
end
|
47
|
+
|
42
48
|
return if super_data.nil?
|
43
49
|
|
44
50
|
# Validate length #
|
@@ -76,7 +82,7 @@ module Schemacop
|
|
76
82
|
end
|
77
83
|
|
78
84
|
def cast(value)
|
79
|
-
if value.
|
85
|
+
if !value.nil?
|
80
86
|
to_cast = value
|
81
87
|
elsif default.present?
|
82
88
|
to_cast = default
|
data/schemacop.gemspec
CHANGED
@@ -1,37 +1,49 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: schemacop 3.0.
|
2
|
+
# stub: schemacop 3.0.12 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.12"
|
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-09-14"
|
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"])
|
@@ -3,7 +3,9 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class ArrayNodeTest < V3Test
|
6
|
-
|
6
|
+
def self.invalid_type_error(type)
|
7
|
+
"Invalid type, got type \"#{type}\", expected \"array\"."
|
8
|
+
end
|
7
9
|
|
8
10
|
def test_basic
|
9
11
|
schema :array
|
@@ -54,7 +56,7 @@ module Schemacop
|
|
54
56
|
error '/', 'Array has 2 items but must have exactly 1.'
|
55
57
|
end
|
56
58
|
assert_validation [123] do
|
57
|
-
error '/[0]', 'Invalid type, expected "object".'
|
59
|
+
error '/[0]', 'Invalid type, got type "Integer", expected "object".'
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
@@ -64,7 +66,7 @@ module Schemacop
|
|
64
66
|
assert_json(type: :array)
|
65
67
|
|
66
68
|
assert_validation 42 do
|
67
|
-
error '/',
|
69
|
+
error '/', ArrayNodeTest.invalid_type_error(Integer)
|
68
70
|
end
|
69
71
|
|
70
72
|
schema { ary! :foo }
|
@@ -79,11 +81,11 @@ module Schemacop
|
|
79
81
|
)
|
80
82
|
|
81
83
|
assert_validation foo: 42 do
|
82
|
-
error '/foo',
|
84
|
+
error '/foo', ArrayNodeTest.invalid_type_error(Integer)
|
83
85
|
end
|
84
86
|
|
85
87
|
assert_validation foo: {} do
|
86
|
-
error '/foo',
|
88
|
+
error '/foo', ArrayNodeTest.invalid_type_error(ActiveSupport::HashWithIndifferentAccess)
|
87
89
|
end
|
88
90
|
end
|
89
91
|
|
@@ -196,7 +198,7 @@ module Schemacop
|
|
196
198
|
end
|
197
199
|
|
198
200
|
assert_validation %i[foo] do
|
199
|
-
error '/[0]', 'Invalid type, expected "string".'
|
201
|
+
error '/[0]', 'Invalid type, got type "Symbol", expected "string".'
|
200
202
|
end
|
201
203
|
end
|
202
204
|
|
@@ -230,7 +232,7 @@ module Schemacop
|
|
230
232
|
end
|
231
233
|
|
232
234
|
assert_validation([42, 42, { name: 'Hello' }]) do
|
233
|
-
error '/[0]', 'Invalid type, expected "string".'
|
235
|
+
error '/[0]', 'Invalid type, got type "Integer", expected "string".'
|
234
236
|
end
|
235
237
|
|
236
238
|
assert_validation(['foo', 42, { namex: 'Hello' }]) do
|
@@ -305,7 +307,7 @@ module Schemacop
|
|
305
307
|
assert_validation(['foo', 42])
|
306
308
|
assert_validation(['foo', 42, 42])
|
307
309
|
assert_validation(['foo', :foo]) do
|
308
|
-
error '/[1]', 'Invalid type, expected "integer".'
|
310
|
+
error '/[1]', 'Invalid type, got type "Symbol", expected "integer".'
|
309
311
|
end
|
310
312
|
end
|
311
313
|
|
@@ -328,7 +330,7 @@ module Schemacop
|
|
328
330
|
assert_validation(['foo', 42])
|
329
331
|
assert_validation(['foo', 42, 'additional', 'another'])
|
330
332
|
assert_validation(['foo', 42, 'additional', 42, 'another']) do
|
331
|
-
error '/[3]', 'Invalid type, expected "string".'
|
333
|
+
error '/[3]', 'Invalid type, got type "Integer", expected "string".'
|
332
334
|
end
|
333
335
|
|
334
336
|
assert_cast(['foo', 42], ['foo', 42])
|
@@ -354,7 +356,7 @@ module Schemacop
|
|
354
356
|
assert_validation(['foo', 42])
|
355
357
|
assert_validation(['foo', 42, '1990-01-01'])
|
356
358
|
assert_validation(['foo', 42, '1990-01-01', 42]) do
|
357
|
-
error '/[3]', 'Invalid type, expected "string".'
|
359
|
+
error '/[3]', 'Invalid type, got type "Integer", expected "string".'
|
358
360
|
end
|
359
361
|
assert_validation(['foo', 42, '1990-01-01', 'foo']) do
|
360
362
|
error '/[3]', 'String does not match format "date".'
|
@@ -439,7 +441,7 @@ module Schemacop
|
|
439
441
|
assert_validation(['foo', 42, { foo: '1990-01-01', bar: :baz }])
|
440
442
|
|
441
443
|
assert_validation(['foo', 42, { foo: 1234 }]) do
|
442
|
-
error '/[2]/foo', 'Invalid type, expected "string".'
|
444
|
+
error '/[2]/foo', 'Invalid type, got type "Integer", expected "string".'
|
443
445
|
end
|
444
446
|
assert_validation(['foo', 42, { foo: 'String' }]) do
|
445
447
|
error '/[2]/foo', 'String does not match format "date".'
|
@@ -555,16 +557,16 @@ module Schemacop
|
|
555
557
|
# Even we put those types in the enum, they need to fail the validations,
|
556
558
|
# as they are not arrays
|
557
559
|
assert_validation('foo') do
|
558
|
-
error '/',
|
560
|
+
error '/', ArrayNodeTest.invalid_type_error(String)
|
559
561
|
end
|
560
562
|
assert_validation(1) do
|
561
|
-
error '/',
|
563
|
+
error '/', ArrayNodeTest.invalid_type_error(Integer)
|
562
564
|
end
|
563
565
|
assert_validation(:bar) do
|
564
|
-
error '/',
|
566
|
+
error '/', ArrayNodeTest.invalid_type_error(Symbol)
|
565
567
|
end
|
566
568
|
assert_validation({ qux: 42 }) do
|
567
|
-
error '/',
|
569
|
+
error '/', ArrayNodeTest.invalid_type_error(Hash)
|
568
570
|
end
|
569
571
|
|
570
572
|
# These need to fail validation, as they are not in the enum list
|
@@ -669,8 +671,8 @@ module Schemacop
|
|
669
671
|
assert_validation([1, 2, 3, 4, 5, 6])
|
670
672
|
|
671
673
|
assert_validation([1, :foo, 'bar']) do
|
672
|
-
error '/[1]', 'Invalid type, expected "integer".'
|
673
|
-
error '/[2]', 'Invalid type, expected "integer".'
|
674
|
+
error '/[1]', 'Invalid type, got type "Symbol", expected "integer".'
|
675
|
+
error '/[2]', 'Invalid type, got type "String", expected "integer".'
|
674
676
|
end
|
675
677
|
end
|
676
678
|
|
@@ -4,7 +4,10 @@ require 'test_helper'
|
|
4
4
|
module Schemacop
|
5
5
|
module V3
|
6
6
|
class BooleanNodeTest < V3Test
|
7
|
-
|
7
|
+
def self.invalid_type_error(type)
|
8
|
+
type = type.class unless type.class == Class
|
9
|
+
"Invalid type, got type \"#{type}\", expected \"boolean\"."
|
10
|
+
end
|
8
11
|
|
9
12
|
def test_basic
|
10
13
|
schema :boolean
|
@@ -47,12 +50,12 @@ module Schemacop
|
|
47
50
|
assert_json(type: :boolean)
|
48
51
|
|
49
52
|
assert_validation 42 do
|
50
|
-
error '/',
|
53
|
+
error '/', BooleanNodeTest.invalid_type_error(Integer)
|
51
54
|
end
|
52
55
|
|
53
56
|
[:true, 'true', :false, 'false', 0, 1].each do |value|
|
54
57
|
assert_validation value do
|
55
|
-
error '/',
|
58
|
+
error '/', BooleanNodeTest.invalid_type_error(value)
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
@@ -68,7 +71,7 @@ module Schemacop
|
|
68
71
|
|
69
72
|
[:true, 'true', :false, 'false', 0, 1].each do |value|
|
70
73
|
assert_validation name: value do
|
71
|
-
error '/name',
|
74
|
+
error '/name', BooleanNodeTest.invalid_type_error(value)
|
72
75
|
end
|
73
76
|
end
|
74
77
|
end
|
@@ -87,13 +90,13 @@ module Schemacop
|
|
87
90
|
# Even we put those types in the enum, they need to fail the validations,
|
88
91
|
# as they are not booleans
|
89
92
|
assert_validation('foo') do
|
90
|
-
error '/',
|
93
|
+
error '/', BooleanNodeTest.invalid_type_error(String)
|
91
94
|
end
|
92
95
|
assert_validation(:bar) do
|
93
|
-
error '/',
|
96
|
+
error '/', BooleanNodeTest.invalid_type_error(Symbol)
|
94
97
|
end
|
95
98
|
assert_validation({ qux: 42 }) do
|
96
|
-
error '/',
|
99
|
+
error '/', BooleanNodeTest.invalid_type_error(Hash)
|
97
100
|
end
|
98
101
|
|
99
102
|
# These need to fail validation, as they are not in the enum list
|
@@ -149,6 +152,36 @@ module Schemacop
|
|
149
152
|
assert_validation('1') do
|
150
153
|
error '/', 'Matches 0 definitions but should match exactly 1.'
|
151
154
|
end
|
155
|
+
|
156
|
+
# Nil can be validated, as it's not required
|
157
|
+
assert_validation(nil)
|
158
|
+
|
159
|
+
assert_validation('')
|
160
|
+
|
161
|
+
assert_cast('', nil)
|
162
|
+
assert_cast(nil, nil)
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_cast_str_required
|
166
|
+
schema :boolean, cast_str: true, required: true
|
167
|
+
|
168
|
+
assert_cast('true', true)
|
169
|
+
assert_cast('false', false)
|
170
|
+
|
171
|
+
assert_cast(true, true)
|
172
|
+
assert_cast(false, false)
|
173
|
+
|
174
|
+
assert_validation('1') do
|
175
|
+
error '/', 'Matches 0 definitions but should match exactly 1.'
|
176
|
+
end
|
177
|
+
|
178
|
+
assert_validation(nil) do
|
179
|
+
error '/', 'Value must be given.'
|
180
|
+
end
|
181
|
+
|
182
|
+
assert_validation('') do
|
183
|
+
error '/', 'Value must be given.'
|
184
|
+
end
|
152
185
|
end
|
153
186
|
end
|
154
187
|
end
|
@@ -3,8 +3,6 @@ require 'test_helper'
|
|
3
3
|
module Schemacop
|
4
4
|
module V3
|
5
5
|
class HashNodeTest < V3Test
|
6
|
-
EXP_INVALID_TYPE = 'Invalid type, expected "hash".'.freeze
|
7
|
-
|
8
6
|
def test_basic
|
9
7
|
schema
|
10
8
|
assert_validation({})
|
@@ -54,7 +52,7 @@ module Schemacop
|
|
54
52
|
|
55
53
|
assert_validation(foo: 'bar', baz: 'foo', answer: '42')
|
56
54
|
assert_validation(foo: 'bar', baz: 'foo', answer: 42) do
|
57
|
-
error '/answer', 'Invalid type, expected "string".'
|
55
|
+
error '/answer', 'Invalid type, got type "Integer", expected "string".'
|
58
56
|
end
|
59
57
|
|
60
58
|
assert_json(
|
@@ -70,7 +68,7 @@ module Schemacop
|
|
70
68
|
@schema.validate!({ foo: 'foo' })
|
71
69
|
end
|
72
70
|
|
73
|
-
assert_raises_with_message Exceptions::ValidationError, '/bar: Invalid type, expected "string".' do
|
71
|
+
assert_raises_with_message Exceptions::ValidationError, '/bar: Invalid type, got type "Symbol", expected "string".' do
|
74
72
|
@schema.validate!({ foo: 'foo', bar: :baz })
|
75
73
|
end
|
76
74
|
end
|
@@ -247,7 +245,7 @@ module Schemacop
|
|
247
245
|
|
248
246
|
assert_validation(name: 'John', xy: 'John', bar_baz: 'Doe') do
|
249
247
|
error '/', 'Obsolete property "xy".'
|
250
|
-
error '/bar_baz', 'Invalid type, expected "integer".'
|
248
|
+
error '/bar_baz', 'Invalid type, got type "String", expected "integer".'
|
251
249
|
end
|
252
250
|
|
253
251
|
assert_json(
|
@@ -276,7 +274,7 @@ module Schemacop
|
|
276
274
|
assert_validation(keyword: 'value')
|
277
275
|
|
278
276
|
assert_validation(keyword: 42) do
|
279
|
-
error '/keyword', 'Invalid type, expected "string".'
|
277
|
+
error '/keyword', 'Invalid type, got type "Integer", expected "string".'
|
280
278
|
end
|
281
279
|
|
282
280
|
assert_json(
|
@@ -759,10 +757,10 @@ module Schemacop
|
|
759
757
|
# Even we put those types in the enum, they need to fail the validations,
|
760
758
|
# as they are not strings
|
761
759
|
assert_validation({ foo: 123 }) do
|
762
|
-
error '/foo', 'Invalid type, expected "string".'
|
760
|
+
error '/foo', 'Invalid type, got type "Integer", expected "string".'
|
763
761
|
end
|
764
762
|
assert_validation({ foo: :faz }) do
|
765
|
-
error '/foo', 'Invalid type, expected "string".'
|
763
|
+
error '/foo', 'Invalid type, got type "Symbol", expected "string".'
|
766
764
|
end
|
767
765
|
|
768
766
|
# These need to fail validation, as they are not in the enum list
|
@@ -918,7 +916,7 @@ module Schemacop
|
|
918
916
|
end
|
919
917
|
|
920
918
|
assert_validation({ foo: '13' }) do
|
921
|
-
error '/foo', 'Invalid type, expected "integer".'
|
919
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
922
920
|
end
|
923
921
|
|
924
922
|
assert_cast(nil, nil)
|
@@ -937,7 +935,7 @@ module Schemacop
|
|
937
935
|
assert_validation({ foo: 42, bar: 13 })
|
938
936
|
|
939
937
|
assert_validation({ foo: '13' }) do
|
940
|
-
error '/foo', 'Invalid type, expected "integer".'
|
938
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
941
939
|
end
|
942
940
|
|
943
941
|
# assert_cast(nil, nil)
|
@@ -959,7 +957,7 @@ module Schemacop
|
|
959
957
|
end
|
960
958
|
|
961
959
|
assert_validation({ foo: '13' }) do
|
962
|
-
error '/foo', 'Invalid type, expected "integer".'
|
960
|
+
error '/foo', 'Invalid type, got type "String", expected "integer".'
|
963
961
|
end
|
964
962
|
|
965
963
|
assert_cast(nil, nil)
|
@@ -967,6 +965,46 @@ module Schemacop
|
|
967
965
|
assert_cast({ foo: 42, bar: 13 }, { bar: 42 }.with_indifferent_access)
|
968
966
|
assert_cast({ bar: 13, foo: 42 }, { bar: 42 }.with_indifferent_access)
|
969
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
|
970
1008
|
end
|
971
1009
|
end
|
972
1010
|
end
|