schemacop 2.4.5 → 3.0.0.rc2
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/.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,120 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class CastingTest < V2Test
|
6
|
+
def test_basic
|
7
|
+
s = Schema.new :integer, cast: [String]
|
8
|
+
|
9
|
+
input = '42'
|
10
|
+
output = s.validate!(input)
|
11
|
+
|
12
|
+
assert_equal(42, output)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_field
|
16
|
+
s = Schema.new do
|
17
|
+
req :foo, :integer, cast: [String]
|
18
|
+
end
|
19
|
+
|
20
|
+
input = { foo: '42' }
|
21
|
+
output = s.validate!(input)
|
22
|
+
|
23
|
+
assert_equal({ foo: '42' }, input)
|
24
|
+
assert_equal({ foo: 42 }, output)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_first_type_matches
|
28
|
+
s = Schema.new do
|
29
|
+
type TrueClass
|
30
|
+
type :integer, cast: [String]
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal(42, s.validate!('42'))
|
34
|
+
assert_equal(42, s.validate!(42))
|
35
|
+
assert_equal(true, s.validate!(true))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_if
|
39
|
+
s = Schema.new do
|
40
|
+
type Float, if: proc { |data| !data.is_a?(String) || data.match(/\d+\.\d+/) }, cast: [String]
|
41
|
+
type Integer, cast: [String]
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal 42.2, s.validate!('42.2')
|
45
|
+
assert_equal 42, s.validate!('42')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_arrays
|
49
|
+
s = Schema.new do
|
50
|
+
req :foo, :array, :integer, cast: [String]
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_equal(
|
54
|
+
{ foo: [1, 2, 3] },
|
55
|
+
s.validate!(foo: %w[1 2 3])
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_check_after_cast
|
60
|
+
s = Schema.new do
|
61
|
+
type Integer, cast: [String], check: proc { |v| v > 41 }
|
62
|
+
end
|
63
|
+
|
64
|
+
assert_equal 42, s.validate!('42')
|
65
|
+
assert_equal 43, s.validate!('43')
|
66
|
+
assert_equal 42, s.validate!(42)
|
67
|
+
assert_verr { s.validate!('41') }
|
68
|
+
assert_verr { s.validate!(42.2) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_multilple_types
|
72
|
+
e = assert_raises Exceptions::InvalidSchemaError do
|
73
|
+
Schema.new do
|
74
|
+
type :number, cast: [String]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
assert_equal 'Casting is only allowed for single-value datatypes, '\
|
79
|
+
'but type Schemacop::V2::NumberValidator has classes ["Integer", "Float"].',
|
80
|
+
e.message
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_custom_castings
|
84
|
+
s = Schema.new do
|
85
|
+
type :integer, cast: { String => proc { |v| Integer(v) } }
|
86
|
+
end
|
87
|
+
|
88
|
+
assert_equal 42, s.validate!('42')
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_decimal_basis_castings
|
92
|
+
s = Schema.new do
|
93
|
+
type :integer, cast: [String]
|
94
|
+
end
|
95
|
+
|
96
|
+
assert_equal 1, s.validate!('01')
|
97
|
+
assert_equal 8, s.validate!('08')
|
98
|
+
assert_equal 11, s.validate!('011')
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_string_to_nil_castings
|
102
|
+
s = Schema.new do
|
103
|
+
opt :int_field, :integer, cast: [String]
|
104
|
+
opt :float_field, :float, cast: [String]
|
105
|
+
end
|
106
|
+
|
107
|
+
expected_int = { int_field: nil }
|
108
|
+
expected_float = { float_field: nil }
|
109
|
+
|
110
|
+
assert_equal expected_int, s.validate!(int_field: nil)
|
111
|
+
assert_equal expected_int, s.validate!(int_field: '')
|
112
|
+
assert_equal expected_int, s.validate!(int_field: ' ')
|
113
|
+
|
114
|
+
assert_equal expected_float, s.validate!(float_field: nil)
|
115
|
+
assert_equal expected_float, s.validate!(float_field: '')
|
116
|
+
assert_equal expected_float, s.validate!(float_field: ' ')
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class CollectorTest < V2Test
|
6
|
+
def test_no_root_node
|
7
|
+
s = Schema.new do
|
8
|
+
req :a, :string
|
9
|
+
end
|
10
|
+
|
11
|
+
col = s.validate(a: 0)
|
12
|
+
assert col.exceptions.first[:path].first !~ %r{^/root}, 'Root node is present in the path.'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_correct_path
|
16
|
+
s = Schema.new do
|
17
|
+
req :long_symbol, :string
|
18
|
+
req 'long_string', :string
|
19
|
+
req 123, :string
|
20
|
+
end
|
21
|
+
|
22
|
+
col = s.validate('long_string' => 0, long_symbol: 0, 123 => 0)
|
23
|
+
|
24
|
+
symbol = col.exceptions[0]
|
25
|
+
string = col.exceptions[1]
|
26
|
+
number = col.exceptions[2]
|
27
|
+
|
28
|
+
assert symbol[:path].first =~ %r{^/long_symbol}
|
29
|
+
assert string[:path].first =~ %r{^/long_string}
|
30
|
+
assert number[:path].first =~ %r{^/123}
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_nested_paths
|
34
|
+
s = Schema.new do
|
35
|
+
req :one do
|
36
|
+
req :two, :string
|
37
|
+
end
|
38
|
+
req :three, :string
|
39
|
+
end
|
40
|
+
|
41
|
+
col = s.validate(one: { two: 0 }, three: 0)
|
42
|
+
assert_equal 2, col.exceptions[0][:path].length
|
43
|
+
assert_equal 1, col.exceptions[1][:path].length
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class CustomCheckTest < V2Test
|
6
|
+
def test_integer_check_short_form
|
7
|
+
s = Schema.new :integer, check: proc { |i| i.even? }
|
8
|
+
assert_nothing_raised { s.validate!(2) }
|
9
|
+
assert_nothing_raised { s.validate!(-8) }
|
10
|
+
assert_nothing_raised { s.validate!(0) }
|
11
|
+
assert_verr { s.validate!(1) }
|
12
|
+
assert_verr { s.validate!(-7) }
|
13
|
+
assert_verr { s.validate!(2.1) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_custom_error_message
|
17
|
+
s = Schema.new :integer, check: proc { |i| i.even? ? true : 'Custom error' }
|
18
|
+
assert_nothing_raised { s.validate!(2) }
|
19
|
+
exception = assert_verr { s.validate!(3) }
|
20
|
+
assert_match(/Custom :check failed: Custom error\./, exception.message)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_integer_check_with_lambda
|
24
|
+
s = Schema.new do
|
25
|
+
type :integer, check: ->(i) { i.even? }
|
26
|
+
end
|
27
|
+
|
28
|
+
assert_nothing_raised { s.validate!(2) }
|
29
|
+
assert_nothing_raised { s.validate!(-8) }
|
30
|
+
assert_nothing_raised { s.validate!(0) }
|
31
|
+
assert_verr { s.validate!(1) }
|
32
|
+
assert_verr { s.validate!(-7) }
|
33
|
+
assert_verr { s.validate!(2.1) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_in_type_dsl
|
37
|
+
s = Schema.new do
|
38
|
+
type :number, check: proc { |x| x == 42 }
|
39
|
+
end
|
40
|
+
assert_nothing_raised { s.validate!(42) }
|
41
|
+
assert_verr { s.validate!(42.1) }
|
42
|
+
assert_verr { s.validate!(0) }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_with_array
|
46
|
+
s = Schema.new do
|
47
|
+
type :array, check: proc { |a| a.first == 1 } do
|
48
|
+
type :integer
|
49
|
+
end
|
50
|
+
end
|
51
|
+
assert_nothing_raised { s.validate!([1, 2, 3]) }
|
52
|
+
assert_verr { s.validate!([2, 3, 4]) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_with_array_nested
|
56
|
+
s = Schema.new do
|
57
|
+
type :array, check: proc { |a| a.first == 4 } do
|
58
|
+
type :integer, check: proc { |i| i >= 2 }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_nothing_raised { s.validate!([4, 3, 2]) }
|
63
|
+
assert_verr { s.validate!([3, 2]) }
|
64
|
+
assert_verr { s.validate!([4, 1]) }
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_with_hash
|
68
|
+
s = Schema.new :hash, check: proc { |h| h.all? { |k, v| k == v } } do
|
69
|
+
opt 1, :integer
|
70
|
+
opt 'two', :string
|
71
|
+
end
|
72
|
+
assert_nothing_raised { s.validate!(1 => 1, 'two' => 'two') }
|
73
|
+
assert_verr { s.validate!(1 => 2, 'two' => 'two') }
|
74
|
+
assert_verr { s.validate!(1 => 1, 'two' => 'one') }
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_mixed_if_check
|
78
|
+
s = Schema.new do
|
79
|
+
req :first_name,
|
80
|
+
:string,
|
81
|
+
if: proc { |str| str.start_with?('Sand') },
|
82
|
+
check: proc { |str| str == 'Sandy' }
|
83
|
+
req :first_name, :string, min: 3
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_nothing_raised { s.validate!(first_name: 'Bob') }
|
87
|
+
assert_nothing_raised { s.validate!(first_name: 'Sandy') }
|
88
|
+
assert_nothing_raised { s.validate!(first_name: 'Sansibar') }
|
89
|
+
|
90
|
+
assert_verr { s.validate!(first_name: 'Sandkasten') }
|
91
|
+
assert_verr { s.validate!(first_name: 'a') }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class CustomIfTest < V2Test
|
6
|
+
def test_allowed_subset_only
|
7
|
+
s = Schema.new do
|
8
|
+
type :integer, if: proc { |data| data.odd? }
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_nothing_raised { s.validate! 5 }
|
12
|
+
assert_verr { s.validate! nil }
|
13
|
+
assert_verr { s.validate! 4 }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_if_with_multiple_types
|
17
|
+
s = Schema.new do
|
18
|
+
type :integer, if: proc { |data| data.odd? }
|
19
|
+
type :string
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_nothing_raised { s.validate!(5) }
|
23
|
+
assert_nothing_raised { s.validate!('abc') }
|
24
|
+
assert_verr { s.validate!(4) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_if_with_string
|
28
|
+
s = Schema.new do
|
29
|
+
req :foo, :string, if: proc { |data| data.start_with?('a') }, min: 3
|
30
|
+
req :foo, :string, min: 5
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_nothing_raised { s.validate!(foo: 'abc') }
|
34
|
+
assert_nothing_raised { s.validate!(foo: 'bcdef') }
|
35
|
+
assert_verr { s.validate!(foo: 'a') }
|
36
|
+
assert_verr { s.validate!(foo: 'bcde') }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_if_with_multiple_types_in_field
|
40
|
+
s = Schema.new do
|
41
|
+
req :foo, :string, if: proc { |data| data.start_with?('a') }
|
42
|
+
req :foo, :integer
|
43
|
+
end
|
44
|
+
|
45
|
+
assert_nothing_raised { s.validate!(foo: 3) }
|
46
|
+
assert_nothing_raised { s.validate!(foo: 'abc') }
|
47
|
+
assert_verr { s.validate!(foo: 'bcd') }
|
48
|
+
assert_verr { s.validate!(foo: true) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_if_with_hash_in_array
|
52
|
+
s = Schema.new do
|
53
|
+
req :user, :array do
|
54
|
+
type :hash, if: proc { |data| data[:admin] } do
|
55
|
+
req :admin, :boolean
|
56
|
+
req :admin_only, :string
|
57
|
+
end
|
58
|
+
type :hash do
|
59
|
+
opt :admin
|
60
|
+
req :non_admin_only, :string
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_nothing_raised { s.validate!(user: [{ admin: true, admin_only: 'foo' }, { admin: false, non_admin_only: 'foo' }]) }
|
66
|
+
assert_nothing_raised { s.validate!(user: [{ admin: true, admin_only: 'foo' }, { non_admin_only: 'foo' }]) }
|
67
|
+
assert_verr { s.validate!(user: [{ admin: false, admin_only: 'foo' }]) }
|
68
|
+
assert_verr { s.validate!(user: [{ admin: true, non_admin_only: 'foo' }]) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_if_true_or_false
|
72
|
+
s = Schema.new do
|
73
|
+
req :foo, :integer, if: proc { true }, min: 5
|
74
|
+
req :bar, :integer, if: proc { false }, min: 5
|
75
|
+
# TODO: It should work without the following line
|
76
|
+
req :bar, :integer
|
77
|
+
end
|
78
|
+
|
79
|
+
assert_nothing_raised { s.validate!(foo: 5, bar: 5) }
|
80
|
+
assert_nothing_raised { s.validate!(foo: 5, bar: 4) }
|
81
|
+
assert_verr { s.validate!(foo: 4, bar: 5) }
|
82
|
+
assert_verr { s.validate!(foo: 4, bar: 4) }
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_mixed_req_opt
|
86
|
+
s = Schema.new do
|
87
|
+
req :foo, :integer, if: proc { |data| !data.nil? }
|
88
|
+
opt :foo, :integer
|
89
|
+
end
|
90
|
+
|
91
|
+
assert_nothing_raised { s.validate!(foo: 4) }
|
92
|
+
assert_verr { s.validate!({}) }
|
93
|
+
assert_verr { s.validate!('something') }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Schemacop
|
4
|
+
module V2
|
5
|
+
class DefaultsTest < V2Test
|
6
|
+
def test_basic
|
7
|
+
s = Schema.new :integer, default: 42
|
8
|
+
|
9
|
+
input = nil
|
10
|
+
output = s.validate!(input)
|
11
|
+
|
12
|
+
assert_equal(42, output)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_hash
|
16
|
+
s = Schema.new do
|
17
|
+
opt :foo, :string, default: 'bar'
|
18
|
+
end
|
19
|
+
|
20
|
+
input = { foo: nil }
|
21
|
+
output = s.validate!(input)
|
22
|
+
assert_equal({ foo: 'bar' }, output)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_missing_hash_key
|
26
|
+
s = Schema.new do
|
27
|
+
opt :foo, :string, default: 'bar'
|
28
|
+
end
|
29
|
+
|
30
|
+
input = {}
|
31
|
+
output = s.validate!(input)
|
32
|
+
assert_equal({ foo: 'bar' }, output)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_entire_hash
|
36
|
+
s = Schema.new do
|
37
|
+
opt :foo, :hash, default: { name: { first: 'Foo', last: 'Bar' } } do
|
38
|
+
req :name do
|
39
|
+
req :first
|
40
|
+
req :last
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
input = {}
|
46
|
+
output = s.validate!(input)
|
47
|
+
assert_equal({ foo: { name: { first: 'Foo', last: 'Bar' } } }, output)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_entire_array
|
51
|
+
s = Schema.new do
|
52
|
+
opt :foo, :array, default: [{ bar: 42 }] do
|
53
|
+
req :bar
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
input = {}
|
58
|
+
output = s.validate!(input)
|
59
|
+
assert_equal({ foo: [{ bar: 42 }] }, output)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_proc
|
63
|
+
s = Schema.new do
|
64
|
+
opt :year, :integer, default: -> { Time.now.year }
|
65
|
+
end
|
66
|
+
|
67
|
+
input = {}
|
68
|
+
output = s.validate!(input)
|
69
|
+
assert_equal({ year: Time.now.year }, output)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_nested_proc
|
73
|
+
myproc = proc { 42 }
|
74
|
+
|
75
|
+
s = Schema.new do
|
76
|
+
opt :myproc, Proc, default: -> { myproc }
|
77
|
+
end
|
78
|
+
|
79
|
+
input = {}
|
80
|
+
output = s.validate!(input)
|
81
|
+
assert_equal({ myproc: myproc }, output)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_invalid_default
|
85
|
+
s = Schema.new :integer, default: '42'
|
86
|
+
|
87
|
+
input = nil
|
88
|
+
|
89
|
+
assert_verr do
|
90
|
+
s.validate!(input)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|