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.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +25 -1
  4. data/.travis.yml +3 -1
  5. data/CHANGELOG.md +32 -1
  6. data/README.md +53 -710
  7. data/README_V2.md +775 -0
  8. data/README_V3.md +1195 -0
  9. data/Rakefile +8 -12
  10. data/VERSION +1 -1
  11. data/lib/schemacop.rb +35 -36
  12. data/lib/schemacop/base_schema.rb +37 -0
  13. data/lib/schemacop/railtie.rb +10 -0
  14. data/lib/schemacop/schema.rb +1 -60
  15. data/lib/schemacop/schema2.rb +22 -0
  16. data/lib/schemacop/schema3.rb +21 -0
  17. data/lib/schemacop/scoped_env.rb +25 -13
  18. data/lib/schemacop/v2.rb +26 -0
  19. data/lib/schemacop/{caster.rb → v2/caster.rb} +16 -2
  20. data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
  21. data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
  22. data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
  23. data/lib/schemacop/v2/node.rb +142 -0
  24. data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
  25. data/lib/schemacop/v2/node_supporting_field.rb +70 -0
  26. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +14 -11
  27. data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
  28. data/lib/schemacop/v2/root_node.rb +6 -0
  29. data/lib/schemacop/v2/validator/array_validator.rb +32 -0
  30. data/lib/schemacop/{validator → v2/validator}/boolean_validator.rb +1 -1
  31. data/lib/schemacop/v2/validator/float_validator.rb +7 -0
  32. data/lib/schemacop/v2/validator/hash_validator.rb +37 -0
  33. data/lib/schemacop/v2/validator/integer_validator.rb +7 -0
  34. data/lib/schemacop/{validator → v2/validator}/nil_validator.rb +1 -1
  35. data/lib/schemacop/v2/validator/number_validator.rb +21 -0
  36. data/lib/schemacop/v2/validator/object_validator.rb +29 -0
  37. data/lib/schemacop/v2/validator/string_validator.rb +39 -0
  38. data/lib/schemacop/{validator → v2/validator}/symbol_validator.rb +1 -1
  39. data/lib/schemacop/v3.rb +45 -0
  40. data/lib/schemacop/v3/all_of_node.rb +27 -0
  41. data/lib/schemacop/v3/any_of_node.rb +28 -0
  42. data/lib/schemacop/v3/array_node.rb +218 -0
  43. data/lib/schemacop/v3/boolean_node.rb +16 -0
  44. data/lib/schemacop/v3/combination_node.rb +45 -0
  45. data/lib/schemacop/v3/context.rb +17 -0
  46. data/lib/schemacop/v3/dsl_scope.rb +46 -0
  47. data/lib/schemacop/v3/global_context.rb +114 -0
  48. data/lib/schemacop/v3/hash_node.rb +256 -0
  49. data/lib/schemacop/v3/integer_node.rb +13 -0
  50. data/lib/schemacop/v3/is_not_node.rb +32 -0
  51. data/lib/schemacop/v3/node.rb +215 -0
  52. data/lib/schemacop/v3/node_registry.rb +49 -0
  53. data/lib/schemacop/v3/number_node.rb +18 -0
  54. data/lib/schemacop/v3/numeric_node.rb +76 -0
  55. data/lib/schemacop/v3/object_node.rb +40 -0
  56. data/lib/schemacop/v3/one_of_node.rb +28 -0
  57. data/lib/schemacop/v3/reference_node.rb +49 -0
  58. data/lib/schemacop/v3/result.rb +58 -0
  59. data/lib/schemacop/v3/string_node.rb +124 -0
  60. data/lib/schemacop/v3/symbol_node.rb +13 -0
  61. data/schemacop.gemspec +24 -27
  62. data/test/lib/test_helper.rb +152 -0
  63. data/test/schemas/nested/group.rb +6 -0
  64. data/test/schemas/user.rb +7 -0
  65. data/test/unit/schemacop/v2/casting_test.rb +120 -0
  66. data/test/unit/schemacop/v2/collector_test.rb +47 -0
  67. data/test/unit/schemacop/v2/custom_check_test.rb +95 -0
  68. data/test/unit/schemacop/v2/custom_if_test.rb +97 -0
  69. data/test/unit/schemacop/v2/defaults_test.rb +95 -0
  70. data/test/unit/schemacop/v2/empty_test.rb +16 -0
  71. data/test/unit/schemacop/v2/nil_dis_allow_test.rb +43 -0
  72. data/test/unit/schemacop/v2/node_resolver_test.rb +28 -0
  73. data/test/unit/schemacop/v2/short_forms_test.rb +351 -0
  74. data/test/unit/schemacop/v2/types_test.rb +88 -0
  75. data/test/unit/schemacop/v2/validator_array_test.rb +99 -0
  76. data/test/unit/schemacop/v2/validator_boolean_test.rb +17 -0
  77. data/test/unit/schemacop/v2/validator_float_test.rb +59 -0
  78. data/test/unit/schemacop/v2/validator_hash_test.rb +95 -0
  79. data/test/unit/schemacop/v2/validator_integer_test.rb +48 -0
  80. data/test/unit/schemacop/v2/validator_nil_test.rb +15 -0
  81. data/test/unit/schemacop/v2/validator_number_test.rb +62 -0
  82. data/test/unit/schemacop/v2/validator_object_test.rb +141 -0
  83. data/test/unit/schemacop/v2/validator_string_test.rb +78 -0
  84. data/test/unit/schemacop/v2/validator_symbol_test.rb +18 -0
  85. data/test/unit/schemacop/v3/all_of_node_test.rb +198 -0
  86. data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
  87. data/test/unit/schemacop/v3/array_node_test.rb +815 -0
  88. data/test/unit/schemacop/v3/boolean_node_test.rb +126 -0
  89. data/test/unit/schemacop/v3/global_context_test.rb +164 -0
  90. data/test/unit/schemacop/v3/hash_node_test.rb +884 -0
  91. data/test/unit/schemacop/v3/integer_node_test.rb +323 -0
  92. data/test/unit/schemacop/v3/is_not_node_test.rb +173 -0
  93. data/test/unit/schemacop/v3/node_test.rb +148 -0
  94. data/test/unit/schemacop/v3/number_node_test.rb +292 -0
  95. data/test/unit/schemacop/v3/object_node_test.rb +170 -0
  96. data/test/unit/schemacop/v3/one_of_node_test.rb +187 -0
  97. data/test/unit/schemacop/v3/reference_node_test.rb +351 -0
  98. data/test/unit/schemacop/v3/string_node_test.rb +334 -0
  99. data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
  100. metadata +152 -145
  101. data/doc/Schemacop.html +0 -146
  102. data/doc/Schemacop/ArrayValidator.html +0 -329
  103. data/doc/Schemacop/BooleanValidator.html +0 -145
  104. data/doc/Schemacop/Caster.html +0 -379
  105. data/doc/Schemacop/Collector.html +0 -787
  106. data/doc/Schemacop/Dupper.html +0 -214
  107. data/doc/Schemacop/Exceptions.html +0 -115
  108. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +0 -124
  109. data/doc/Schemacop/Exceptions/ValidationError.html +0 -124
  110. data/doc/Schemacop/FieldNode.html +0 -421
  111. data/doc/Schemacop/FloatValidator.html +0 -158
  112. data/doc/Schemacop/HashValidator.html +0 -293
  113. data/doc/Schemacop/IntegerValidator.html +0 -158
  114. data/doc/Schemacop/NilValidator.html +0 -145
  115. data/doc/Schemacop/Node.html +0 -1438
  116. data/doc/Schemacop/NodeResolver.html +0 -258
  117. data/doc/Schemacop/NodeSupportingField.html +0 -590
  118. data/doc/Schemacop/NodeSupportingType.html +0 -612
  119. data/doc/Schemacop/NodeWithBlock.html +0 -289
  120. data/doc/Schemacop/NumberValidator.html +0 -232
  121. data/doc/Schemacop/ObjectValidator.html +0 -298
  122. data/doc/Schemacop/RootNode.html +0 -171
  123. data/doc/Schemacop/Schema.html +0 -699
  124. data/doc/Schemacop/StringValidator.html +0 -295
  125. data/doc/Schemacop/SymbolValidator.html +0 -145
  126. data/doc/ScopedEnv.html +0 -351
  127. data/doc/_index.html +0 -379
  128. data/doc/class_list.html +0 -51
  129. data/doc/css/common.css +0 -1
  130. data/doc/css/full_list.css +0 -58
  131. data/doc/css/style.css +0 -496
  132. data/doc/file.README.html +0 -833
  133. data/doc/file_list.html +0 -56
  134. data/doc/frames.html +0 -17
  135. data/doc/index.html +0 -833
  136. data/doc/inheritance.graphml +0 -524
  137. data/doc/inheritance.pdf +0 -825
  138. data/doc/js/app.js +0 -303
  139. data/doc/js/full_list.js +0 -216
  140. data/doc/js/jquery.js +0 -4
  141. data/doc/method_list.html +0 -587
  142. data/doc/top-level-namespace.html +0 -112
  143. data/lib/schemacop/node.rb +0 -139
  144. data/lib/schemacop/node_supporting_field.rb +0 -58
  145. data/lib/schemacop/root_node.rb +0 -4
  146. data/lib/schemacop/validator/array_validator.rb +0 -30
  147. data/lib/schemacop/validator/float_validator.rb +0 -5
  148. data/lib/schemacop/validator/hash_validator.rb +0 -35
  149. data/lib/schemacop/validator/integer_validator.rb +0 -5
  150. data/lib/schemacop/validator/number_validator.rb +0 -19
  151. data/lib/schemacop/validator/object_validator.rb +0 -27
  152. data/lib/schemacop/validator/string_validator.rb +0 -37
  153. data/test/casting_test.rb +0 -90
  154. data/test/collector_test.rb +0 -45
  155. data/test/custom_check_test.rb +0 -93
  156. data/test/custom_if_test.rb +0 -95
  157. data/test/defaults_test.rb +0 -93
  158. data/test/empty_test.rb +0 -14
  159. data/test/nil_dis_allow_test.rb +0 -41
  160. data/test/node_resolver_test.rb +0 -26
  161. data/test/short_forms_test.rb +0 -349
  162. data/test/test_helper.rb +0 -13
  163. data/test/types_test.rb +0 -84
  164. data/test/validator_array_test.rb +0 -97
  165. data/test/validator_boolean_test.rb +0 -15
  166. data/test/validator_float_test.rb +0 -57
  167. data/test/validator_hash_test.rb +0 -93
  168. data/test/validator_integer_test.rb +0 -46
  169. data/test/validator_nil_test.rb +0 -13
  170. data/test/validator_number_test.rb +0 -60
  171. data/test/validator_object_test.rb +0 -139
  172. data/test/validator_string_test.rb +0 -76
  173. data/test/validator_symbol_test.rb +0 -16
@@ -0,0 +1,99 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorArrayTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :array do
9
+ type :integer
10
+ end
11
+ end
12
+ assert_nothing_raised { s.validate!([]) }
13
+ assert_nothing_raised { s.validate!([0]) }
14
+ assert_nothing_raised { s.validate!([0, 1]) }
15
+ assert_verr { s.validate!(['string']) }
16
+ end
17
+
18
+ def test_option_min
19
+ s = Schema.new do
20
+ type :array, min: 2
21
+ end
22
+
23
+ assert_nothing_raised { s.validate!([0, 1]) }
24
+ assert_nothing_raised { s.validate!([0, 1, 2]) }
25
+ assert_verr { s.validate!([]) }
26
+ assert_verr { s.validate!([0]) }
27
+ end
28
+
29
+ def test_option_max
30
+ s = Schema.new do
31
+ type :array, max: 2
32
+ end
33
+
34
+ assert_nothing_raised { s.validate!([]) }
35
+ assert_nothing_raised { s.validate!([0]) }
36
+ assert_nothing_raised { s.validate!([0, 1]) }
37
+ assert_verr { s.validate!([0, 1, 2]) }
38
+ end
39
+
40
+ def test_options_min_max
41
+ s = Schema.new do
42
+ type :array, min: 2, max: 3 do
43
+ type :integer
44
+ end
45
+ end
46
+
47
+ assert_nothing_raised { s.validate!([1, 2]) }
48
+ assert_nothing_raised { s.validate!([1, 2, 3]) }
49
+ assert_verr { s.validate!([]) }
50
+ assert_verr { s.validate!([1]) }
51
+ assert_verr { s.validate!([1, 2, 3, 4]) }
52
+ end
53
+
54
+ def test_nil
55
+ s = Schema.new do
56
+ type :array, nil: true do
57
+ type :integer
58
+ end
59
+ end
60
+
61
+ assert_nothing_raised { s.validate!([1, nil, 2]) }
62
+ assert_verr { s.validate!([1, nil, 'nope']) }
63
+ end
64
+
65
+ def test_multiple_arrays
66
+ s = Schema.new do
67
+ type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
68
+ type :integer
69
+ end
70
+ type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
71
+ type :string
72
+ end
73
+ end
74
+
75
+ assert_nothing_raised { s.validate!([1, 2, 3]) }
76
+ assert_nothing_raised { s.validate!(%w[one two three]) }
77
+ assert_verr { s.validate!([1, 'mix']) }
78
+ assert_verr { s.validate!([]) }
79
+ end
80
+
81
+ def test_multiple_arrays_with_empty
82
+ s = Schema.new do
83
+ type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
84
+ type :integer
85
+ end
86
+ type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
87
+ type :string
88
+ end
89
+ type :array
90
+ end
91
+
92
+ assert_nothing_raised { s.validate!([]) }
93
+ assert_nothing_raised { s.validate!([1, 2, 3]) }
94
+ assert_nothing_raised { s.validate!(%w[one two three]) }
95
+ assert_verr { s.validate!([1, 'mix']) }
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorBooleanTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :boolean
9
+ end
10
+ assert_nothing_raised { s.validate! true }
11
+ assert_nothing_raised { s.validate! false }
12
+ assert_verr { s.validate! nil }
13
+ assert_verr { s.validate! 1 }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorFloatTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :float
9
+ end
10
+ assert_nothing_raised { s.validate!(-3.0) }
11
+ assert_nothing_raised { s.validate!(-3.123) }
12
+ assert_nothing_raised { s.validate!(0.0) }
13
+ assert_nothing_raised { s.validate!(15.0) }
14
+ assert_nothing_raised { s.validate!(15.13) }
15
+ assert_verr { s.validate!(-3) }
16
+ assert_verr { s.validate!(0) }
17
+ assert_verr { s.validate!(15) }
18
+ end
19
+
20
+ def test_option_min
21
+ s = Schema.new do
22
+ type :float, min: -2
23
+ end
24
+
25
+ assert_nothing_raised { s.validate!(-2.0) }
26
+ assert_nothing_raised { s.validate!(-1.99999) }
27
+ assert_nothing_raised { s.validate!(1.2) }
28
+ assert_verr { s.validate!(-5.2) }
29
+ assert_verr { s.validate!(-2.00001) }
30
+ end
31
+
32
+ def test_option_max
33
+ s = Schema.new do
34
+ type :float, max: 5.2
35
+ end
36
+
37
+ assert_nothing_raised { s.validate!(-2.0) }
38
+ assert_nothing_raised { s.validate!(5.2) }
39
+ assert_verr { s.validate!(5.200001) }
40
+ end
41
+
42
+ def test_options_min_max
43
+ s = Schema.new do
44
+ type :float, min: -2, max: 5.2
45
+ end
46
+
47
+ assert_nothing_raised { s.validate!(-2.0) }
48
+ assert_nothing_raised { s.validate!(-1.99999) }
49
+ assert_nothing_raised { s.validate!(0.0) }
50
+ assert_nothing_raised { s.validate!(1.2) }
51
+ assert_nothing_raised { s.validate!(5.2) }
52
+ assert_verr { s.validate!(-2.00001) }
53
+ assert_verr { s.validate!(5.200001) }
54
+ assert_verr { s.validate!(6) }
55
+ assert_verr { s.validate!(0) }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorHashTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :hash do
9
+ req :one do
10
+ type :integer
11
+ end
12
+ req :two do
13
+ type :boolean
14
+ end
15
+ end
16
+ end
17
+ assert_nothing_raised { s.validate!(one: 3, two: true) }
18
+ assert_verr { s.validate!(one: 3) }
19
+ assert_verr { s.validate!(two: true) }
20
+ assert_verr { s.validate!({}) }
21
+ assert_verr { s.validate!(one: 3, two: true, three: 3) }
22
+ assert_verr { s.validate!(one: 3, three: true) }
23
+ end
24
+
25
+ def test_allow_obsolete_keys
26
+ s = Schema.new do
27
+ type :hash, allow_obsolete_keys: true
28
+ end
29
+
30
+ assert_nothing_raised { s.validate!(foo: :bar) }
31
+ assert_equal({ foo: { bar: :baz } }, s.validate!(foo: { bar: :baz }))
32
+
33
+ assert_verr { s.validate!(3) }
34
+ end
35
+
36
+ def test_allow_obsolete_keys_with_substructore
37
+ s = Schema.new do
38
+ type :hash, allow_obsolete_keys: true do
39
+ req :foo
40
+ end
41
+ end
42
+
43
+ assert_nothing_raised { s.validate!(foo: :bar, bar: :baz) }
44
+ assert_verr { s.validate!(bar: :baz) }
45
+ end
46
+
47
+ def test_nested
48
+ s = Schema.new do
49
+ type :hash do
50
+ req :h do
51
+ type :hash do
52
+ req :i do
53
+ type :integer
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ s.validate(h: { i: 3 })
60
+ assert_verr { s.validate!(h: { i: true }) }
61
+ assert_verr { s.validate!(h: {}) }
62
+ assert_verr { s.validate!({}) }
63
+ assert_verr { s.validate!(g: { i: 3 }) }
64
+ assert_verr { s.validate!(h: { j: 3 }) }
65
+ end
66
+
67
+ def test_req_opt
68
+ s = Schema.new do
69
+ type :hash do
70
+ req :r do
71
+ type :boolean
72
+ end
73
+ req? :r_nil do
74
+ type :boolean
75
+ end
76
+ opt :o do
77
+ type :boolean
78
+ end
79
+ opt! :o_nonnil do
80
+ type :boolean
81
+ end
82
+ end
83
+ end
84
+
85
+ assert_nothing_raised { s.validate!(r: true, r_nil: false) }
86
+ assert_nothing_raised { s.validate!(r: true, r_nil: nil) }
87
+ assert_nothing_raised { s.validate!(r: true, r_nil: false, o: false) }
88
+ assert_nothing_raised { s.validate!(r: true, r_nil: false, o: nil) }
89
+ assert_verr { s.validate!(r: true, r_nil: false, o_nonnil: nil) }
90
+ assert_verr { s.validate!(o: true) }
91
+ assert_verr { s.validate!(r: nil, r_nil: false, o: true) }
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorIntegerTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :integer
9
+ end
10
+ assert_nothing_raised { s.validate!(-3) }
11
+ assert_nothing_raised { s.validate!(0) }
12
+ assert_nothing_raised { s.validate!(15) }
13
+ assert_verr { s.validate!(0.0) }
14
+ end
15
+
16
+ def test_option_min
17
+ s = Schema.new do
18
+ type :integer, min: 6
19
+ end
20
+
21
+ assert_nothing_raised { s.validate!(6) }
22
+ assert_nothing_raised { s.validate!(7) }
23
+ assert_verr { s.validate!(5) }
24
+ end
25
+
26
+ def test_option_max
27
+ s = Schema.new do
28
+ type :integer, max: 7
29
+ end
30
+
31
+ assert_nothing_raised { s.validate!(6) }
32
+ assert_nothing_raised { s.validate!(7) }
33
+ assert_verr { s.validate!(8) }
34
+ end
35
+
36
+ def test_options_min_max
37
+ s = Schema.new do
38
+ type :integer, min: 6, max: 7
39
+ end
40
+
41
+ assert_nothing_raised { s.validate!(6) }
42
+ assert_nothing_raised { s.validate!(7) }
43
+ assert_verr { s.validate!(5) }
44
+ assert_verr { s.validate!(8) }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorNilTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :nil
9
+ end
10
+ assert_nothing_raised { s.validate! nil }
11
+ assert_verr { s.validate! false }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorNumberTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :number
9
+ end
10
+ assert_nothing_raised { s.validate!(-3) }
11
+ assert_nothing_raised { s.validate!(-3.123) }
12
+ assert_nothing_raised { s.validate!(0) }
13
+ assert_nothing_raised { s.validate!(15) }
14
+ assert_nothing_raised { s.validate!(15.13) }
15
+ assert_verr { s.validate!('0.12') }
16
+ end
17
+
18
+ def test_option_min
19
+ s = Schema.new do
20
+ type :number, min: -2
21
+ end
22
+
23
+ assert_nothing_raised { s.validate!(-2) }
24
+ assert_nothing_raised { s.validate!(-1.99999) }
25
+ assert_nothing_raised { s.validate!(0) }
26
+ assert_nothing_raised { s.validate!(1.2) }
27
+ assert_verr { s.validate!(-3) }
28
+ assert_verr { s.validate!(-2.00001) }
29
+ end
30
+
31
+ def test_option_max
32
+ s = Schema.new do
33
+ type :number, max: 5.2
34
+ end
35
+
36
+ assert_nothing_raised { s.validate!(-2) }
37
+ assert_nothing_raised { s.validate!(-1.9) }
38
+ assert_nothing_raised { s.validate!(0) }
39
+ assert_nothing_raised { s.validate!(5.19999) }
40
+ assert_nothing_raised { s.validate!(5.2) }
41
+ assert_verr { s.validate!(5.200001) }
42
+ assert_verr { s.validate!(6) }
43
+ end
44
+
45
+ def test_options_min_max
46
+ s = Schema.new do
47
+ type :number, min: -2, max: 5.2
48
+ end
49
+
50
+ assert_nothing_raised { s.validate!(-2) }
51
+ assert_nothing_raised { s.validate!(-1.99999) }
52
+ assert_nothing_raised { s.validate!(0) }
53
+ assert_nothing_raised { s.validate!(1.2) }
54
+ assert_nothing_raised { s.validate!(5.2) }
55
+ assert_verr { s.validate!(-3) }
56
+ assert_verr { s.validate!(-2.00001) }
57
+ assert_verr { s.validate!(5.200001) }
58
+ assert_verr { s.validate!(6) }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,141 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorObjectTest < V2Test
6
+ # Classes used in the tests below
7
+ class User; end
8
+ class AdminUser; end
9
+ class SubUser < User; end
10
+
11
+ def test_basic
12
+ s = Schema.new do
13
+ type :object, classes: User
14
+ end
15
+
16
+ assert_nothing_raised { s.validate!(User.new) }
17
+ assert_verr { s.validate!(SubUser.new) }
18
+ assert_verr { s.validate!(AdminUser.new) }
19
+ end
20
+
21
+ # In modern versions of ruby, some classes such as Tempfile are not derived
22
+ # from Object but from BasicObject. Before the time of writing this test,
23
+ # the ObjectValidator only accepted subclasses of Object and classes like
24
+ # Tempfile did not work at all. This test ensures that this is working now.
25
+ def test_basic_object
26
+ refute Tempfile <= Object
27
+ assert Tempfile <= BasicObject
28
+
29
+ s = Schema.new do
30
+ type :object
31
+ end
32
+
33
+ assert_nothing_raised { s.validate!(Tempfile.new) }
34
+
35
+ s = Schema.new do
36
+ req :foo
37
+ end
38
+
39
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
40
+
41
+ s = Schema.new do
42
+ req :foo, Tempfile
43
+ end
44
+
45
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
46
+ assert_verr { s.validate!(foo: Time.new) }
47
+
48
+ s = Schema.new do
49
+ req :foo, :object, classes: Tempfile
50
+ end
51
+
52
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
53
+ end
54
+
55
+ def test_multiple_classes_long
56
+ s = Schema.new do
57
+ type :object, classes: User
58
+ type :object, classes: AdminUser
59
+ end
60
+
61
+ assert_nothing_raised { s.validate!(User.new) }
62
+ assert_nothing_raised { s.validate!(AdminUser.new) }
63
+ assert_verr { s.validate!(SubUser.new) }
64
+ assert_verr { s.validate!([User.new, AdminUser.new]) }
65
+ end
66
+
67
+ def test_multiple_classes_short
68
+ s = Schema.new do
69
+ type :object, classes: [User, AdminUser]
70
+ end
71
+
72
+ assert_nothing_raised { s.validate!(User.new) }
73
+ assert_nothing_raised { s.validate!(AdminUser.new) }
74
+ assert_verr { s.validate!(SubUser.new) }
75
+ assert_verr { s.validate!([User.new, AdminUser.new]) }
76
+ end
77
+
78
+ def test_hash_of_objects
79
+ s = Schema.new do
80
+ req :user do
81
+ type :object, classes: User
82
+ end
83
+ opt :multitype do
84
+ type :object, classes: [User, AdminUser]
85
+ end
86
+ end
87
+
88
+ assert_nothing_raised { s.validate!(user: User.new) }
89
+ assert_nothing_raised { s.validate!(user: User.new, multitype: AdminUser.new) }
90
+ assert_nothing_raised { s.validate!(user: User.new, multitype: User.new) }
91
+ assert_verr { s.validate!(user: AdminUser.new) }
92
+ assert_verr { s.validate!(user: User.new, multitype: 12) }
93
+ assert_verr { s.validate!(user: User.new, multitype: self) }
94
+ end
95
+
96
+ def test_any
97
+ s = Schema.new do
98
+ type :object
99
+ end
100
+
101
+ assert_nothing_raised { s.validate!(User.new) }
102
+ assert_nothing_raised { s.validate!(123) }
103
+ assert_nothing_raised { s.validate!('sali') }
104
+ assert_nothing_raised { s.validate!(self) }
105
+ assert_nothing_raised { s.validate!(self.class) }
106
+ assert_verr { s.validate!(nil) }
107
+ end
108
+
109
+ def test_any_in_hash
110
+ s = Schema.new do
111
+ req :fld do
112
+ type :object
113
+ end
114
+ end
115
+
116
+ assert_nothing_raised { s.validate!(fld: User.new) }
117
+ assert_nothing_raised { s.validate!(fld: self) }
118
+ assert_verr { s.validate!(fld: nil) }
119
+ assert_verr { s.validate!({}) }
120
+ end
121
+
122
+ def test_strict_option
123
+ s = Schema.new do
124
+ req :o_strict do
125
+ type :object, classes: User, strict: true
126
+ end
127
+ opt :o_ns do
128
+ type :object, classes: User, strict: false
129
+ end
130
+ end
131
+
132
+ assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: User.new) }
133
+ assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: SubUser.new) }
134
+ assert_nothing_raised { s.validate!(o_strict: User.new) }
135
+ assert_verr { s.validate!(o_strict: SubUser.new) }
136
+ assert_verr { s.validate!(o_strict: User.new, o_ns: AdminUser.new) }
137
+ assert_verr { s.validate!(o_strict: SubUser.new, o_ns: User.new) }
138
+ end
139
+ end
140
+ end
141
+ end