schemacop 2.4.7 → 3.0.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +25 -1
  4. data/.travis.yml +2 -1
  5. data/CHANGELOG.md +8 -0
  6. data/README.md +41 -708
  7. data/README_V2.md +775 -0
  8. data/README_V3.md +683 -0
  9. data/Rakefile +8 -12
  10. data/VERSION +1 -1
  11. data/lib/schemacop.rb +35 -37
  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/{node_supporting_field.rb → v2/node_supporting_field.rb} +8 -10
  26. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +6 -3
  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 +219 -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 +217 -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 +214 -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 +199 -0
  86. data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
  87. data/test/unit/schemacop/v3/array_node_test.rb +805 -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 +775 -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/root_node.rb +0 -4
  145. data/lib/schemacop/validator/array_validator.rb +0 -30
  146. data/lib/schemacop/validator/float_validator.rb +0 -5
  147. data/lib/schemacop/validator/hash_validator.rb +0 -35
  148. data/lib/schemacop/validator/integer_validator.rb +0 -5
  149. data/lib/schemacop/validator/number_validator.rb +0 -19
  150. data/lib/schemacop/validator/object_validator.rb +0 -27
  151. data/lib/schemacop/validator/string_validator.rb +0 -37
  152. data/test/casting_test.rb +0 -118
  153. data/test/collector_test.rb +0 -45
  154. data/test/custom_check_test.rb +0 -93
  155. data/test/custom_if_test.rb +0 -95
  156. data/test/defaults_test.rb +0 -93
  157. data/test/empty_test.rb +0 -14
  158. data/test/nil_dis_allow_test.rb +0 -41
  159. data/test/node_resolver_test.rb +0 -26
  160. data/test/short_forms_test.rb +0 -349
  161. data/test/test_helper.rb +0 -13
  162. data/test/types_test.rb +0 -84
  163. data/test/validator_array_test.rb +0 -97
  164. data/test/validator_boolean_test.rb +0 -15
  165. data/test/validator_float_test.rb +0 -57
  166. data/test/validator_hash_test.rb +0 -93
  167. data/test/validator_integer_test.rb +0 -46
  168. data/test/validator_nil_test.rb +0 -13
  169. data/test/validator_number_test.rb +0 -60
  170. data/test/validator_object_test.rb +0 -139
  171. data/test/validator_string_test.rb +0 -76
  172. data/test/validator_symbol_test.rb +0 -16
@@ -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
@@ -1,14 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Schemacop
4
- class EmptyTest < Minitest::Test
5
- def test_empty_hash
6
- schema = Schema.new do
7
- type Hash
8
- end
9
-
10
- assert_nothing_raised { schema.validate!({}) }
11
- assert_verr { schema.validate!(foo: :bar) }
12
- end
13
- end
14
- end
@@ -1,41 +0,0 @@
1
- require 'test_helper'
2
-
3
- # Note: A test for req and opt is part of validator_hash_test.rb
4
-
5
- module Schemacop
6
- class NilDisAllowTest < Minitest::Test
7
- def test_req
8
- s = Schema.new do
9
- req? :o do
10
- type :boolean
11
- end
12
-
13
- req :r do
14
- type :boolean
15
- end
16
- end
17
- assert_nothing_raised { s.validate!(o: nil, r: false) }
18
- assert_nothing_raised { s.validate!(o: false, r: false) }
19
- assert_verr { s.validate!(o: true, r: nil) }
20
- assert_verr { s.validate!(o: nil, r: nil) }
21
- assert_verr { s.validate!(r: true) }
22
- end
23
-
24
- def test_opt
25
- s = Schema.new do
26
- opt :o do
27
- type :boolean
28
- end
29
- opt! :r do
30
- type :boolean
31
- end
32
- end
33
- assert_nothing_raised { s.validate!(o: nil, r: false) }
34
- assert_nothing_raised { s.validate!(o: false, r: false) }
35
- assert_nothing_raised { s.validate!(r: true) }
36
- assert_nothing_raised { s.validate!({}) }
37
- assert_verr { s.validate!(o: true, r: nil) }
38
- assert_verr { s.validate!(o: nil, r: nil) }
39
- end
40
- end
41
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Schemacop
4
- class NodeResolverTest < Minitest::Test
5
- class ClassA; end
6
- class ClassB; end
7
- class ClassC; end
8
- class ClassD; end
9
-
10
- def test_insert_before
11
- prev_node_classes = NodeResolver.node_classes
12
-
13
- NodeResolver.node_classes = []
14
-
15
- NodeResolver.register(ClassA)
16
- NodeResolver.register(ClassB)
17
- NodeResolver.register(ClassC)
18
- NodeResolver.register(ClassD, before: ClassB)
19
-
20
- assert_equal [ClassA, ClassD, ClassB, ClassC],
21
- NodeResolver.node_classes
22
- ensure
23
- NodeResolver.node_classes = prev_node_classes
24
- end
25
- end
26
- end
@@ -1,349 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Schemacop
4
- class ShortFormsTest < Minitest::Test
5
- class User; end
6
- class Group; end
7
-
8
- def test_untyped
9
- s = Schema.new do
10
- req :foo
11
- end
12
-
13
- assert_nothing_raised { s.validate!(foo: 3) }
14
- assert_nothing_raised { s.validate!(foo: '42') }
15
- assert_nothing_raised { s.validate!(foo: Object.new) }
16
- assert_verr { s.validate!(foo: nil) }
17
- end
18
-
19
- def test_constructor_defaults_to_hash
20
- s = Schema.new do
21
- req! :r do
22
- type :integer
23
- end
24
- opt? :o do
25
- type :integer
26
- end
27
- end
28
-
29
- assert_nothing_raised { s.validate!(r: 3) }
30
- assert_nothing_raised { s.validate!(r: 3, o: 1) }
31
- assert_verr { s.validate!(o: 1) }
32
- assert_verr { s.validate!(1) }
33
- assert_verr { s.validate!(r: 3, n: 5) }
34
- end
35
-
36
- def test_field_defaults_to_any
37
- s = Schema.new do
38
- type :hash do
39
- req :r
40
- opt :o
41
- end
42
- end
43
-
44
- assert_nothing_raised { s.validate!(r: 3) }
45
- assert_nothing_raised { s.validate!(r: 'asd', o: self) }
46
- assert_verr { s.validate!(o: -5.3) }
47
- assert_verr { s.validate!(Class) }
48
- assert_verr { s.validate!(r: s, n: true) }
49
- end
50
-
51
- def test_inline_type_with_attrs_in_hash
52
- s = Schema.new do
53
- type :hash do
54
- req :i, :integer, min: 5
55
- opt :f, :float, min: 2.3, max: 3
56
- end
57
- end
58
-
59
- assert_nothing_raised { s.validate!(i: 5) }
60
- assert_nothing_raised { s.validate!(i: 5, f: 2.3) }
61
- assert_verr { s.validate!(i: 4, f: 2.3) }
62
- assert_verr { s.validate!(i: 5, f: 2.2) }
63
- assert_verr { s.validate!(i: 5, f: 3.4) }
64
- assert_verr { s.validate!(i: 5.3) }
65
- assert_verr { s.validate!(j: 2.3) }
66
- assert_verr { s.validate!({}) }
67
- end
68
-
69
- def test_inline_type_in_constructor
70
- s = Schema.new :integer, min: 2, max: 4
71
- assert_nothing_raised { s.validate!(3) }
72
- assert_verr { s.validate!(5) }
73
- assert_verr { s.validate!(1) }
74
- end
75
-
76
- def test_mixed_field_and_type_attrs
77
- s = Schema.new do
78
- req? :nilbool, :boolean
79
- req? :nilint, :integer, min: 2, max: 3
80
- opt! :optint, :integer, min: 2, max: 3
81
- end
82
-
83
- assert_nothing_raised { s.validate!(nilbool: nil, nilint: nil) }
84
- assert_nothing_raised { s.validate!(nilbool: false, nilint: 2) }
85
- assert_nothing_raised { s.validate!(nilbool: false, nilint: 3, optint: 2) }
86
- assert_verr { s.validate!(nilbool: false, nilint: 2, optint: nil) }
87
- assert_verr { s.validate!(nilbool: false, nilint: 2, optint: 4) }
88
- assert_verr { s.validate!(nilbool: false, nilint: -5, optint: 2) }
89
- end
90
-
91
- def test_array_shortform_simple
92
- s = Schema.new do
93
- type(:array, :string)
94
- end
95
- assert_nothing_raised { s.validate!(%w(a b)) }
96
- end
97
-
98
- # TODO: Get the exception message into the assertion
99
- def test_array_shortform_invalid
100
- assert_raises Exceptions::InvalidSchemaError do
101
- Schema.new do
102
- type(:array, [:array, :integer], min: 2)
103
- end
104
- end
105
- end
106
-
107
- def test_array_shortform_advanced1
108
- s = Schema.new do
109
- type(:array, [:array, :integer])
110
- end
111
- assert_nothing_raised { s.validate! [[], 3] }
112
- assert_nothing_raised { s.validate! [[:a, 9], 3] }
113
- assert_nothing_raised { s.validate! [[]] }
114
- assert_nothing_raised { s.validate! [3] }
115
- assert_verr { s.validate! [[], 'string'] }
116
- assert_verr { s.validate! [3, 'string'] }
117
- assert_verr { s.validate! ['string'] }
118
- end
119
-
120
- def test_array_shortform_advanced2
121
- assert_raises Exceptions::InvalidSchemaError, 'No validation class found for type [:array, :integer].' do
122
- Schema.new do
123
- type([:array, [:array, :integer], :boolean])
124
- end
125
- end
126
- end
127
-
128
- # For explicit form test see types_test.rb
129
- def test_array_subtype_shortform
130
- s = Schema.new do
131
- type :array, :integer
132
- end
133
- assert_nothing_raised { s.validate! [5] }
134
- assert_verr { s.validate! [nil] }
135
- assert_verr { s.validate! ['a'] }
136
- assert_verr { s.validate! [5, 'a'] }
137
- assert_verr { s.validate! [5, nil] }
138
- end
139
-
140
- def test_array_subsubtype_shortform
141
- s = Schema.new do
142
- type :array, :array, :integer
143
- end
144
- assert_nothing_raised { s.validate! [[5]] }
145
- assert_verr { s.validate! [5] }
146
- assert_verr { s.validate! [[nil]] }
147
- assert_verr { s.validate! [['a']] }
148
- assert_verr { s.validate! [[5, 'a']] }
149
- assert_verr { s.validate! [[5, nil]] }
150
- end
151
-
152
- def test_wild_mix_should_pass
153
- s = Schema.new do
154
- req :foo do
155
- req? :bar, :object, classes: NilClass
156
- end
157
- req :name, :integer, min: 5, max: 7
158
- req :id, [:integer, :string]
159
- req :callback, :symbol
160
- req :attrs do
161
- req :color do
162
- type :integer
163
- end
164
- end
165
- req :colors, :array, [:string, :integer]
166
- req :cars, :array, :hash do
167
- req? :years, :array, :integer
168
- req! :make, :string
169
- req! :ps, :integer
170
- req? :electric, :boolean
171
- end
172
- end
173
-
174
- assert_nothing_raised do
175
- s.validate!(
176
- name: 6,
177
- foo: { bar: nil },
178
- attrs: { color: 5 },
179
- id: 'hallo',
180
- callback: :funky_function,
181
- colors: [5, 'sdf'],
182
- cars: [
183
- {
184
- make: 'Tesla',
185
- ps: 5,
186
- electric: nil,
187
- years: [1993, 1990]
188
- }
189
- ]
190
- )
191
- end
192
- end
193
-
194
- def test_super_deep_wild_should_pass
195
- s = Schema.new do
196
- type :hash do
197
- opt? :bla do
198
- type :string
199
- end
200
- req :id do
201
- type :string
202
- end
203
- req? :friends do
204
- type :array do
205
- type :string, min: 3, max: 6
206
- type :boolean
207
- type :hash do
208
- req :rod do
209
- type :hash do
210
- req :fritz do
211
- type :array, min: 2 do
212
- type :array do
213
- type :integer, min: 1
214
- end
215
- end
216
- end
217
- end
218
- end
219
- end
220
- end
221
- end
222
- end
223
- type :integer, min: 3
224
- end
225
-
226
- assert_nothing_raised do
227
- s.validate!(
228
- id: 'meine ID',
229
- friends: [
230
- 'Rodney',
231
- true,
232
- false,
233
- {
234
- rod: {
235
- fritz:
236
- [
237
- [1],
238
- [3]
239
- ]
240
- }
241
- }
242
- ]
243
- )
244
- end
245
- assert_nothing_raised { s.validate!(id: 'my ID', friends: nil) }
246
- assert_nothing_raised { s.validate!(3) }
247
- end
248
-
249
- def test_example_from_readme
250
- schema = Schema.new do
251
- req :naming, :hash do
252
- opt :first_name, :string
253
- req :last_name, :string
254
- end
255
- opt! :age, :integer, min: 18
256
- req? :password do
257
- type :string, check: proc { |pw| pw.include?('*') }
258
- type :integer
259
- end
260
- end
261
-
262
- assert_nothing_raised do
263
- schema.validate!(
264
- naming: { first_name: 'John',
265
- last_name: 'Doe' },
266
- age: 34,
267
- password: 'my*pass'
268
- )
269
- end
270
-
271
- assert_verr do
272
- schema.validate!(
273
- naming: { first_name: 'John',
274
- last_name: 'Doe' },
275
- age: 12,
276
- password: 'my*pass'
277
- )
278
- end
279
-
280
- assert_verr do
281
- schema.validate!(
282
- naming: { first_name: 'John',
283
- last_name: 'Doe' },
284
- age: 12,
285
- password: 'mypass'
286
- )
287
- end
288
-
289
- schema2 = Schema.new do
290
- req :description,
291
- :string,
292
- if: proc { |str| str.start_with?('Abstract: ') },
293
- max: 35,
294
- check: proc { |str| !str.end_with?('.') }
295
- req :description, :string, min: 35
296
- end
297
-
298
- assert_nothing_raised { schema2.validate!(description: 'Abstract: a short description') }
299
- assert_nothing_raised { schema2.validate!(description: 'Since this is no abstract, we expect it to be longer.') }
300
- assert_verr { schema2.validate!(description: 'Abstract: A short description.') }
301
- assert_verr { schema2.validate!(description: 'Abstract: This is gonna be way way too long for an abstract.') }
302
- assert_verr { schema2.validate!(description: 'This is too short.') }
303
- end
304
-
305
- def test_one_line_subtype_with_options
306
- s = Schema.new do
307
- type :array, :integer, min: 3
308
- end
309
- assert_nothing_raised { s.validate!([3]) }
310
- assert_nothing_raised { s.validate!([3, 4, 5]) }
311
- assert_verr { s.validate!([3, 2]) }
312
- assert_verr { s.validate!([5, 'string']) }
313
- end
314
-
315
- def test_one_line_array_schema
316
- s = Schema.new :array, :integer, min: 3
317
- assert_nothing_raised { s.validate!([3]) }
318
- assert_nothing_raised { s.validate!([3, 4, 5]) }
319
- assert_verr { s.validate!([3, 2]) }
320
- assert_verr { s.validate!([5, 'string']) }
321
- end
322
-
323
- def test_implicit_hash
324
- s = Schema.new do
325
- req :bar
326
- end
327
- assert_nothing_raised { s.validate!(bar: 2) }
328
- assert_verr { s.validate!(foo: 2) }
329
- assert_verr { s.validate!([2]) }
330
- end
331
-
332
- def test_one_line_string_schema
333
- s = Schema.new :string, min: 4
334
- assert_nothing_raised { s.validate!('1234') }
335
- assert_verr { s.validate!('123') }
336
- assert_verr { s.validate!(string: '1234') }
337
- end
338
-
339
- def test_inline_objects
340
- s = Schema.new do
341
- req :user, User
342
- req :group, Group
343
- end
344
-
345
- assert_nothing_raised { s.validate!(user: User.new, group: Group.new) }
346
- assert_verr { s.validate!(user: Group.new, group: User.new) }
347
- end
348
- end
349
- end