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.
Files changed (174) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.releaser_config +0 -1
  4. data/.rubocop.yml +25 -1
  5. data/.travis.yml +3 -1
  6. data/CHANGELOG.md +33 -0
  7. data/README.md +53 -710
  8. data/README_V2.md +775 -0
  9. data/README_V3.md +1197 -0
  10. data/Rakefile +8 -12
  11. data/VERSION +1 -1
  12. data/lib/schemacop.rb +35 -36
  13. data/lib/schemacop/base_schema.rb +37 -0
  14. data/lib/schemacop/railtie.rb +10 -0
  15. data/lib/schemacop/schema.rb +1 -60
  16. data/lib/schemacop/schema2.rb +22 -0
  17. data/lib/schemacop/schema3.rb +21 -0
  18. data/lib/schemacop/scoped_env.rb +25 -13
  19. data/lib/schemacop/v2.rb +25 -0
  20. data/lib/schemacop/{caster.rb → v2/caster.rb} +16 -2
  21. data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
  22. data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
  23. data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
  24. data/lib/schemacop/v2/node.rb +142 -0
  25. data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
  26. data/lib/schemacop/v2/node_supporting_field.rb +70 -0
  27. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +8 -5
  28. data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
  29. data/lib/schemacop/v2/root_node.rb +0 -0
  30. data/lib/schemacop/v2/validator/array_validator.rb +32 -0
  31. data/lib/schemacop/{validator → v2/validator}/boolean_validator.rb +1 -1
  32. data/lib/schemacop/v2/validator/float_validator.rb +7 -0
  33. data/lib/schemacop/v2/validator/hash_validator.rb +37 -0
  34. data/lib/schemacop/v2/validator/integer_validator.rb +7 -0
  35. data/lib/schemacop/{validator → v2/validator}/nil_validator.rb +1 -1
  36. data/lib/schemacop/v2/validator/number_validator.rb +21 -0
  37. data/lib/schemacop/v2/validator/object_validator.rb +29 -0
  38. data/lib/schemacop/v2/validator/string_validator.rb +39 -0
  39. data/lib/schemacop/{validator → v2/validator}/symbol_validator.rb +1 -1
  40. data/lib/schemacop/v3.rb +45 -0
  41. data/lib/schemacop/v3/all_of_node.rb +27 -0
  42. data/lib/schemacop/v3/any_of_node.rb +28 -0
  43. data/lib/schemacop/v3/array_node.rb +218 -0
  44. data/lib/schemacop/v3/boolean_node.rb +16 -0
  45. data/lib/schemacop/v3/combination_node.rb +45 -0
  46. data/lib/schemacop/v3/context.rb +17 -0
  47. data/lib/schemacop/v3/dsl_scope.rb +46 -0
  48. data/lib/schemacop/v3/global_context.rb +114 -0
  49. data/lib/schemacop/v3/hash_node.rb +258 -0
  50. data/lib/schemacop/v3/integer_node.rb +13 -0
  51. data/lib/schemacop/v3/is_not_node.rb +32 -0
  52. data/lib/schemacop/v3/node.rb +219 -0
  53. data/lib/schemacop/v3/node_registry.rb +49 -0
  54. data/lib/schemacop/v3/number_node.rb +18 -0
  55. data/lib/schemacop/v3/numeric_node.rb +76 -0
  56. data/lib/schemacop/v3/object_node.rb +40 -0
  57. data/lib/schemacop/v3/one_of_node.rb +28 -0
  58. data/lib/schemacop/v3/reference_node.rb +49 -0
  59. data/lib/schemacop/v3/result.rb +58 -0
  60. data/lib/schemacop/v3/string_node.rb +124 -0
  61. data/lib/schemacop/v3/symbol_node.rb +13 -0
  62. data/schemacop.gemspec +24 -27
  63. data/test/lib/test_helper.rb +152 -0
  64. data/test/schemas/nested/group.rb +6 -0
  65. data/test/schemas/user.rb +7 -0
  66. data/test/unit/schemacop/v2/casting_test.rb +120 -0
  67. data/test/unit/schemacop/v2/collector_test.rb +47 -0
  68. data/test/unit/schemacop/v2/custom_check_test.rb +95 -0
  69. data/test/unit/schemacop/v2/custom_if_test.rb +97 -0
  70. data/test/unit/schemacop/v2/defaults_test.rb +95 -0
  71. data/test/unit/schemacop/v2/empty_test.rb +16 -0
  72. data/test/unit/schemacop/v2/nil_dis_allow_test.rb +43 -0
  73. data/test/unit/schemacop/v2/node_resolver_test.rb +28 -0
  74. data/test/unit/schemacop/v2/short_forms_test.rb +351 -0
  75. data/test/unit/schemacop/v2/types_test.rb +88 -0
  76. data/test/unit/schemacop/v2/validator_array_test.rb +99 -0
  77. data/test/unit/schemacop/v2/validator_boolean_test.rb +17 -0
  78. data/test/unit/schemacop/v2/validator_float_test.rb +59 -0
  79. data/test/unit/schemacop/v2/validator_hash_test.rb +95 -0
  80. data/test/unit/schemacop/v2/validator_integer_test.rb +48 -0
  81. data/test/unit/schemacop/v2/validator_nil_test.rb +15 -0
  82. data/test/unit/schemacop/v2/validator_number_test.rb +62 -0
  83. data/test/unit/schemacop/v2/validator_object_test.rb +141 -0
  84. data/test/unit/schemacop/v2/validator_string_test.rb +78 -0
  85. data/test/unit/schemacop/v2/validator_symbol_test.rb +18 -0
  86. data/test/unit/schemacop/v3/all_of_node_test.rb +198 -0
  87. data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
  88. data/test/unit/schemacop/v3/array_node_test.rb +815 -0
  89. data/test/unit/schemacop/v3/boolean_node_test.rb +126 -0
  90. data/test/unit/schemacop/v3/global_context_test.rb +164 -0
  91. data/test/unit/schemacop/v3/hash_node_test.rb +884 -0
  92. data/test/unit/schemacop/v3/integer_node_test.rb +323 -0
  93. data/test/unit/schemacop/v3/is_not_node_test.rb +173 -0
  94. data/test/unit/schemacop/v3/node_test.rb +148 -0
  95. data/test/unit/schemacop/v3/number_node_test.rb +292 -0
  96. data/test/unit/schemacop/v3/object_node_test.rb +170 -0
  97. data/test/unit/schemacop/v3/one_of_node_test.rb +187 -0
  98. data/test/unit/schemacop/v3/reference_node_test.rb +351 -0
  99. data/test/unit/schemacop/v3/string_node_test.rb +334 -0
  100. data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
  101. metadata +157 -150
  102. data/doc/Schemacop.html +0 -146
  103. data/doc/Schemacop/ArrayValidator.html +0 -329
  104. data/doc/Schemacop/BooleanValidator.html +0 -145
  105. data/doc/Schemacop/Caster.html +0 -379
  106. data/doc/Schemacop/Collector.html +0 -787
  107. data/doc/Schemacop/Dupper.html +0 -214
  108. data/doc/Schemacop/Exceptions.html +0 -115
  109. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +0 -124
  110. data/doc/Schemacop/Exceptions/ValidationError.html +0 -124
  111. data/doc/Schemacop/FieldNode.html +0 -421
  112. data/doc/Schemacop/FloatValidator.html +0 -158
  113. data/doc/Schemacop/HashValidator.html +0 -293
  114. data/doc/Schemacop/IntegerValidator.html +0 -158
  115. data/doc/Schemacop/NilValidator.html +0 -145
  116. data/doc/Schemacop/Node.html +0 -1438
  117. data/doc/Schemacop/NodeResolver.html +0 -258
  118. data/doc/Schemacop/NodeSupportingField.html +0 -590
  119. data/doc/Schemacop/NodeSupportingType.html +0 -612
  120. data/doc/Schemacop/NodeWithBlock.html +0 -289
  121. data/doc/Schemacop/NumberValidator.html +0 -232
  122. data/doc/Schemacop/ObjectValidator.html +0 -298
  123. data/doc/Schemacop/RootNode.html +0 -171
  124. data/doc/Schemacop/Schema.html +0 -699
  125. data/doc/Schemacop/StringValidator.html +0 -295
  126. data/doc/Schemacop/SymbolValidator.html +0 -145
  127. data/doc/ScopedEnv.html +0 -351
  128. data/doc/_index.html +0 -379
  129. data/doc/class_list.html +0 -51
  130. data/doc/css/common.css +0 -1
  131. data/doc/css/full_list.css +0 -58
  132. data/doc/css/style.css +0 -496
  133. data/doc/file.README.html +0 -833
  134. data/doc/file_list.html +0 -56
  135. data/doc/frames.html +0 -17
  136. data/doc/index.html +0 -833
  137. data/doc/inheritance.graphml +0 -524
  138. data/doc/inheritance.pdf +0 -825
  139. data/doc/js/app.js +0 -303
  140. data/doc/js/full_list.js +0 -216
  141. data/doc/js/jquery.js +0 -4
  142. data/doc/method_list.html +0 -587
  143. data/doc/top-level-namespace.html +0 -112
  144. data/lib/schemacop/node.rb +0 -139
  145. data/lib/schemacop/node_supporting_field.rb +0 -58
  146. data/lib/schemacop/root_node.rb +0 -4
  147. data/lib/schemacop/validator/array_validator.rb +0 -30
  148. data/lib/schemacop/validator/float_validator.rb +0 -5
  149. data/lib/schemacop/validator/hash_validator.rb +0 -35
  150. data/lib/schemacop/validator/integer_validator.rb +0 -5
  151. data/lib/schemacop/validator/number_validator.rb +0 -19
  152. data/lib/schemacop/validator/object_validator.rb +0 -27
  153. data/lib/schemacop/validator/string_validator.rb +0 -37
  154. data/test/casting_test.rb +0 -100
  155. data/test/collector_test.rb +0 -45
  156. data/test/custom_check_test.rb +0 -93
  157. data/test/custom_if_test.rb +0 -95
  158. data/test/defaults_test.rb +0 -93
  159. data/test/empty_test.rb +0 -14
  160. data/test/nil_dis_allow_test.rb +0 -41
  161. data/test/node_resolver_test.rb +0 -26
  162. data/test/short_forms_test.rb +0 -349
  163. data/test/test_helper.rb +0 -13
  164. data/test/types_test.rb +0 -84
  165. data/test/validator_array_test.rb +0 -97
  166. data/test/validator_boolean_test.rb +0 -15
  167. data/test/validator_float_test.rb +0 -57
  168. data/test/validator_hash_test.rb +0 -93
  169. data/test/validator_integer_test.rb +0 -46
  170. data/test/validator_nil_test.rb +0 -13
  171. data/test/validator_number_test.rb +0 -60
  172. data/test/validator_object_test.rb +0 -139
  173. data/test/validator_string_test.rb +0 -76
  174. 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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