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
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorArrayTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :array do
|
8
|
-
type :integer
|
9
|
-
end
|
10
|
-
end
|
11
|
-
assert_nothing_raised { s.validate!([]) }
|
12
|
-
assert_nothing_raised { s.validate!([0]) }
|
13
|
-
assert_nothing_raised { s.validate!([0, 1]) }
|
14
|
-
assert_verr { s.validate!(['string']) }
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_option_min
|
18
|
-
s = Schema.new do
|
19
|
-
type :array, min: 2
|
20
|
-
end
|
21
|
-
|
22
|
-
assert_nothing_raised { s.validate!([0, 1]) }
|
23
|
-
assert_nothing_raised { s.validate!([0, 1, 2]) }
|
24
|
-
assert_verr { s.validate!([]) }
|
25
|
-
assert_verr { s.validate!([0]) }
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_option_max
|
29
|
-
s = Schema.new do
|
30
|
-
type :array, max: 2
|
31
|
-
end
|
32
|
-
|
33
|
-
assert_nothing_raised { s.validate!([]) }
|
34
|
-
assert_nothing_raised { s.validate!([0]) }
|
35
|
-
assert_nothing_raised { s.validate!([0, 1]) }
|
36
|
-
assert_verr { s.validate!([0, 1, 2]) }
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_options_min_max
|
40
|
-
s = Schema.new do
|
41
|
-
type :array, min: 2, max: 3 do
|
42
|
-
type :integer
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
assert_nothing_raised { s.validate!([1, 2]) }
|
47
|
-
assert_nothing_raised { s.validate!([1, 2, 3]) }
|
48
|
-
assert_verr { s.validate!([]) }
|
49
|
-
assert_verr { s.validate!([1]) }
|
50
|
-
assert_verr { s.validate!([1, 2, 3, 4]) }
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_nil
|
54
|
-
s = Schema.new do
|
55
|
-
type :array, nil: true do
|
56
|
-
type :integer
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
assert_nothing_raised { s.validate!([1, nil, 2]) }
|
61
|
-
assert_verr { s.validate!([1, nil, 'nope']) }
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_multiple_arrays
|
65
|
-
s = Schema.new do
|
66
|
-
type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
|
67
|
-
type :integer
|
68
|
-
end
|
69
|
-
type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
|
70
|
-
type :string
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
assert_nothing_raised { s.validate!([1, 2, 3]) }
|
75
|
-
assert_nothing_raised { s.validate!(%w(one two three)) }
|
76
|
-
assert_verr { s.validate!([1, 'mix']) }
|
77
|
-
assert_verr { s.validate!([]) }
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_multiple_arrays_with_empty
|
81
|
-
s = Schema.new do
|
82
|
-
type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
|
83
|
-
type :integer
|
84
|
-
end
|
85
|
-
type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
|
86
|
-
type :string
|
87
|
-
end
|
88
|
-
type :array
|
89
|
-
end
|
90
|
-
|
91
|
-
assert_nothing_raised { s.validate!([]) }
|
92
|
-
assert_nothing_raised { s.validate!([1, 2, 3]) }
|
93
|
-
assert_nothing_raised { s.validate!(%w(one two three)) }
|
94
|
-
assert_verr { s.validate!([1, 'mix']) }
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorBooleanTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :boolean
|
8
|
-
end
|
9
|
-
assert_nothing_raised { s.validate! true }
|
10
|
-
assert_nothing_raised { s.validate! false }
|
11
|
-
assert_verr { s.validate! nil }
|
12
|
-
assert_verr { s.validate! 1 }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorFloatTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :float
|
8
|
-
end
|
9
|
-
assert_nothing_raised { s.validate!(-3.0) }
|
10
|
-
assert_nothing_raised { s.validate!(-3.123) }
|
11
|
-
assert_nothing_raised { s.validate!(0.0) }
|
12
|
-
assert_nothing_raised { s.validate!(15.0) }
|
13
|
-
assert_nothing_raised { s.validate!(15.13) }
|
14
|
-
assert_verr { s.validate!(-3) }
|
15
|
-
assert_verr { s.validate!(0) }
|
16
|
-
assert_verr { s.validate!(15) }
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_option_min
|
20
|
-
s = Schema.new do
|
21
|
-
type :float, min: -2
|
22
|
-
end
|
23
|
-
|
24
|
-
assert_nothing_raised { s.validate!(-2.0) }
|
25
|
-
assert_nothing_raised { s.validate!(-1.99999) }
|
26
|
-
assert_nothing_raised { s.validate!(1.2) }
|
27
|
-
assert_verr { s.validate!(-5.2) }
|
28
|
-
assert_verr { s.validate!(-2.00001) }
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_option_max
|
32
|
-
s = Schema.new do
|
33
|
-
type :float, max: 5.2
|
34
|
-
end
|
35
|
-
|
36
|
-
assert_nothing_raised { s.validate!(-2.0) }
|
37
|
-
assert_nothing_raised { s.validate!(5.2) }
|
38
|
-
assert_verr { s.validate!(5.200001) }
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_options_min_max
|
42
|
-
s = Schema.new do
|
43
|
-
type :float, min: -2, max: 5.2
|
44
|
-
end
|
45
|
-
|
46
|
-
assert_nothing_raised { s.validate!(-2.0) }
|
47
|
-
assert_nothing_raised { s.validate!(-1.99999) }
|
48
|
-
assert_nothing_raised { s.validate!(0.0) }
|
49
|
-
assert_nothing_raised { s.validate!(1.2) }
|
50
|
-
assert_nothing_raised { s.validate!(5.2) }
|
51
|
-
assert_verr { s.validate!(-2.00001) }
|
52
|
-
assert_verr { s.validate!(5.200001) }
|
53
|
-
assert_verr { s.validate!(6) }
|
54
|
-
assert_verr { s.validate!(0) }
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/test/validator_hash_test.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorHashTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :hash do
|
8
|
-
req :one do
|
9
|
-
type :integer
|
10
|
-
end
|
11
|
-
req :two do
|
12
|
-
type :boolean
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
assert_nothing_raised { s.validate!(one: 3, two: true) }
|
17
|
-
assert_verr { s.validate!(one: 3) }
|
18
|
-
assert_verr { s.validate!(two: true) }
|
19
|
-
assert_verr { s.validate!({}) }
|
20
|
-
assert_verr { s.validate!(one: 3, two: true, three: 3) }
|
21
|
-
assert_verr { s.validate!(one: 3, three: true) }
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_allow_obsolete_keys
|
25
|
-
s = Schema.new do
|
26
|
-
type :hash, allow_obsolete_keys: true
|
27
|
-
end
|
28
|
-
|
29
|
-
assert_nothing_raised { s.validate!(foo: :bar) }
|
30
|
-
assert_equal({ foo: { bar: :baz } }, s.validate!(foo: { bar: :baz }))
|
31
|
-
|
32
|
-
assert_verr { s.validate!(3) }
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_allow_obsolete_keys_with_substructore
|
36
|
-
s = Schema.new do
|
37
|
-
type :hash, allow_obsolete_keys: true do
|
38
|
-
req :foo
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
assert_nothing_raised { s.validate!(foo: :bar, bar: :baz) }
|
43
|
-
assert_verr { s.validate!(bar: :baz) }
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_nested
|
47
|
-
s = Schema.new do
|
48
|
-
type :hash do
|
49
|
-
req :h do
|
50
|
-
type :hash do
|
51
|
-
req :i do
|
52
|
-
type :integer
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
s.validate(h: { i: 3 })
|
59
|
-
assert_verr { s.validate!(h: { i: true }) }
|
60
|
-
assert_verr { s.validate!(h: {}) }
|
61
|
-
assert_verr { s.validate!({}) }
|
62
|
-
assert_verr { s.validate!(g: { i: 3 }) }
|
63
|
-
assert_verr { s.validate!(h: { j: 3 }) }
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_req_opt
|
67
|
-
s = Schema.new do
|
68
|
-
type :hash do
|
69
|
-
req :r do
|
70
|
-
type :boolean
|
71
|
-
end
|
72
|
-
req? :r_nil do
|
73
|
-
type :boolean
|
74
|
-
end
|
75
|
-
opt :o do
|
76
|
-
type :boolean
|
77
|
-
end
|
78
|
-
opt! :o_nonnil do
|
79
|
-
type :boolean
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
assert_nothing_raised { s.validate!(r: true, r_nil: false) }
|
85
|
-
assert_nothing_raised { s.validate!(r: true, r_nil: nil) }
|
86
|
-
assert_nothing_raised { s.validate!(r: true, r_nil: false, o: false) }
|
87
|
-
assert_nothing_raised { s.validate!(r: true, r_nil: false, o: nil) }
|
88
|
-
assert_verr { s.validate!(r: true, r_nil: false, o_nonnil: nil) }
|
89
|
-
assert_verr { s.validate!(o: true) }
|
90
|
-
assert_verr { s.validate!(r: nil, r_nil: false, o: true) }
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorIntegerTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :integer
|
8
|
-
end
|
9
|
-
assert_nothing_raised { s.validate!(-3) }
|
10
|
-
assert_nothing_raised { s.validate!(0) }
|
11
|
-
assert_nothing_raised { s.validate!(15) }
|
12
|
-
assert_verr { s.validate!(0.0) }
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_option_min
|
16
|
-
s = Schema.new do
|
17
|
-
type :integer, min: 6
|
18
|
-
end
|
19
|
-
|
20
|
-
assert_nothing_raised { s.validate!(6) }
|
21
|
-
assert_nothing_raised { s.validate!(7) }
|
22
|
-
assert_verr { s.validate!(5) }
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_option_max
|
26
|
-
s = Schema.new do
|
27
|
-
type :integer, max: 7
|
28
|
-
end
|
29
|
-
|
30
|
-
assert_nothing_raised { s.validate!(6) }
|
31
|
-
assert_nothing_raised { s.validate!(7) }
|
32
|
-
assert_verr { s.validate!(8) }
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_options_min_max
|
36
|
-
s = Schema.new do
|
37
|
-
type :integer, min: 6, max: 7
|
38
|
-
end
|
39
|
-
|
40
|
-
assert_nothing_raised { s.validate!(6) }
|
41
|
-
assert_nothing_raised { s.validate!(7) }
|
42
|
-
assert_verr { s.validate!(5) }
|
43
|
-
assert_verr { s.validate!(8) }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/test/validator_nil_test.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorNumberTest < Minitest::Test
|
5
|
-
def test_basic
|
6
|
-
s = Schema.new do
|
7
|
-
type :number
|
8
|
-
end
|
9
|
-
assert_nothing_raised { s.validate!(-3) }
|
10
|
-
assert_nothing_raised { s.validate!(-3.123) }
|
11
|
-
assert_nothing_raised { s.validate!(0) }
|
12
|
-
assert_nothing_raised { s.validate!(15) }
|
13
|
-
assert_nothing_raised { s.validate!(15.13) }
|
14
|
-
assert_verr { s.validate!('0.12') }
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_option_min
|
18
|
-
s = Schema.new do
|
19
|
-
type :number, min: -2
|
20
|
-
end
|
21
|
-
|
22
|
-
assert_nothing_raised { s.validate!(-2) }
|
23
|
-
assert_nothing_raised { s.validate!(-1.99999) }
|
24
|
-
assert_nothing_raised { s.validate!(0) }
|
25
|
-
assert_nothing_raised { s.validate!(1.2) }
|
26
|
-
assert_verr { s.validate!(-3) }
|
27
|
-
assert_verr { s.validate!(-2.00001) }
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_option_max
|
31
|
-
s = Schema.new do
|
32
|
-
type :number, max: 5.2
|
33
|
-
end
|
34
|
-
|
35
|
-
assert_nothing_raised { s.validate!(-2) }
|
36
|
-
assert_nothing_raised { s.validate!(-1.9) }
|
37
|
-
assert_nothing_raised { s.validate!(0) }
|
38
|
-
assert_nothing_raised { s.validate!(5.19999) }
|
39
|
-
assert_nothing_raised { s.validate!(5.2) }
|
40
|
-
assert_verr { s.validate!(5.200001) }
|
41
|
-
assert_verr { s.validate!(6) }
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_options_min_max
|
45
|
-
s = Schema.new do
|
46
|
-
type :number, min: -2, max: 5.2
|
47
|
-
end
|
48
|
-
|
49
|
-
assert_nothing_raised { s.validate!(-2) }
|
50
|
-
assert_nothing_raised { s.validate!(-1.99999) }
|
51
|
-
assert_nothing_raised { s.validate!(0) }
|
52
|
-
assert_nothing_raised { s.validate!(1.2) }
|
53
|
-
assert_nothing_raised { s.validate!(5.2) }
|
54
|
-
assert_verr { s.validate!(-3) }
|
55
|
-
assert_verr { s.validate!(-2.00001) }
|
56
|
-
assert_verr { s.validate!(5.200001) }
|
57
|
-
assert_verr { s.validate!(6) }
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,139 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module Schemacop
|
4
|
-
class ValidatorObjectTest < Minitest::Test
|
5
|
-
# Classes used in the tests below
|
6
|
-
class User; end
|
7
|
-
class AdminUser; end
|
8
|
-
class SubUser < User; end
|
9
|
-
|
10
|
-
def test_basic
|
11
|
-
s = Schema.new do
|
12
|
-
type :object, classes: User
|
13
|
-
end
|
14
|
-
|
15
|
-
assert_nothing_raised { s.validate!(User.new) }
|
16
|
-
assert_verr { s.validate!(SubUser.new) }
|
17
|
-
assert_verr { s.validate!(AdminUser.new) }
|
18
|
-
end
|
19
|
-
|
20
|
-
# In modern versions of ruby, some classes such as Tempfile are not derived
|
21
|
-
# from Object but from BasicObject. Before the time of writing this test,
|
22
|
-
# the ObjectValidator only accepted subclasses of Object and classes like
|
23
|
-
# Tempfile did not work at all. This test ensures that this is working now.
|
24
|
-
def test_basic_object
|
25
|
-
refute Tempfile <= Object
|
26
|
-
assert Tempfile <= BasicObject
|
27
|
-
|
28
|
-
s = Schema.new do
|
29
|
-
type :object
|
30
|
-
end
|
31
|
-
|
32
|
-
assert_nothing_raised { s.validate!(Tempfile.new) }
|
33
|
-
|
34
|
-
s = Schema.new do
|
35
|
-
req :foo
|
36
|
-
end
|
37
|
-
|
38
|
-
assert_nothing_raised { s.validate!(foo: Tempfile.new) }
|
39
|
-
|
40
|
-
s = Schema.new do
|
41
|
-
req :foo, Tempfile
|
42
|
-
end
|
43
|
-
|
44
|
-
assert_nothing_raised { s.validate!(foo: Tempfile.new) }
|
45
|
-
assert_verr { s.validate!(foo: Time.new) }
|
46
|
-
|
47
|
-
s = Schema.new do
|
48
|
-
req :foo, :object, classes: Tempfile
|
49
|
-
end
|
50
|
-
|
51
|
-
assert_nothing_raised { s.validate!(foo: Tempfile.new) }
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_multiple_classes_long
|
55
|
-
s = Schema.new do
|
56
|
-
type :object, classes: User
|
57
|
-
type :object, classes: AdminUser
|
58
|
-
end
|
59
|
-
|
60
|
-
assert_nothing_raised { s.validate!(User.new) }
|
61
|
-
assert_nothing_raised { s.validate!(AdminUser.new) }
|
62
|
-
assert_verr { s.validate!(SubUser.new) }
|
63
|
-
assert_verr { s.validate!([User.new, AdminUser.new]) }
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_multiple_classes_short
|
67
|
-
s = Schema.new do
|
68
|
-
type :object, classes: [User, AdminUser]
|
69
|
-
end
|
70
|
-
|
71
|
-
assert_nothing_raised { s.validate!(User.new) }
|
72
|
-
assert_nothing_raised { s.validate!(AdminUser.new) }
|
73
|
-
assert_verr { s.validate!(SubUser.new) }
|
74
|
-
assert_verr { s.validate!([User.new, AdminUser.new]) }
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_hash_of_objects
|
78
|
-
s = Schema.new do
|
79
|
-
req :user do
|
80
|
-
type :object, classes: User
|
81
|
-
end
|
82
|
-
opt :multitype do
|
83
|
-
type :object, classes: [User, AdminUser]
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
assert_nothing_raised { s.validate!(user: User.new) }
|
88
|
-
assert_nothing_raised { s.validate!(user: User.new, multitype: AdminUser.new) }
|
89
|
-
assert_nothing_raised { s.validate!(user: User.new, multitype: User.new) }
|
90
|
-
assert_verr { s.validate!(user: AdminUser.new) }
|
91
|
-
assert_verr { s.validate!(user: User.new, multitype: 12) }
|
92
|
-
assert_verr { s.validate!(user: User.new, multitype: self) }
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_any
|
96
|
-
s = Schema.new do
|
97
|
-
type :object
|
98
|
-
end
|
99
|
-
|
100
|
-
assert_nothing_raised { s.validate!(User.new) }
|
101
|
-
assert_nothing_raised { s.validate!(123) }
|
102
|
-
assert_nothing_raised { s.validate!('sali') }
|
103
|
-
assert_nothing_raised { s.validate!(self) }
|
104
|
-
assert_nothing_raised { s.validate!(self.class) }
|
105
|
-
assert_verr { s.validate!(nil) }
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_any_in_hash
|
109
|
-
s = Schema.new do
|
110
|
-
req :fld do
|
111
|
-
type :object
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
assert_nothing_raised { s.validate!(fld: User.new) }
|
116
|
-
assert_nothing_raised { s.validate!(fld: self) }
|
117
|
-
assert_verr { s.validate!(fld: nil) }
|
118
|
-
assert_verr { s.validate!({}) }
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_strict_option
|
122
|
-
s = Schema.new do
|
123
|
-
req :o_strict do
|
124
|
-
type :object, classes: User, strict: true
|
125
|
-
end
|
126
|
-
opt :o_ns do
|
127
|
-
type :object, classes: User, strict: false
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: User.new) }
|
132
|
-
assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: SubUser.new) }
|
133
|
-
assert_nothing_raised { s.validate!(o_strict: User.new) }
|
134
|
-
assert_verr { s.validate!(o_strict: SubUser.new) }
|
135
|
-
assert_verr { s.validate!(o_strict: User.new, o_ns: AdminUser.new) }
|
136
|
-
assert_verr { s.validate!(o_strict: SubUser.new, o_ns: User.new) }
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|