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