schemacop 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.rubocop.yml +59 -1
  4. data/CHANGELOG.md +10 -0
  5. data/README.md +389 -199
  6. data/Rakefile +2 -0
  7. data/VERSION +1 -1
  8. data/doc/Schemacop.html +41 -130
  9. data/doc/Schemacop/ArrayValidator.html +329 -0
  10. data/doc/Schemacop/BooleanValidator.html +145 -0
  11. data/doc/Schemacop/Collector.html +535 -0
  12. data/doc/Schemacop/Exceptions.html +39 -39
  13. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +124 -0
  14. data/doc/Schemacop/Exceptions/ValidationError.html +124 -0
  15. data/doc/Schemacop/FieldNode.html +409 -0
  16. data/doc/Schemacop/FloatValidator.html +158 -0
  17. data/doc/Schemacop/HashValidator.html +263 -0
  18. data/doc/Schemacop/IntegerValidator.html +158 -0
  19. data/doc/Schemacop/NilValidator.html +145 -0
  20. data/doc/Schemacop/Node.html +1426 -0
  21. data/doc/Schemacop/NodeResolver.html +242 -0
  22. data/doc/Schemacop/NodeSupportingField.html +590 -0
  23. data/doc/Schemacop/NodeSupportingType.html +614 -0
  24. data/doc/Schemacop/NodeWithBlock.html +289 -0
  25. data/doc/Schemacop/NumberValidator.html +232 -0
  26. data/doc/Schemacop/ObjectValidator.html +288 -0
  27. data/doc/Schemacop/RootNode.html +171 -0
  28. data/doc/Schemacop/Schema.html +697 -0
  29. data/doc/Schemacop/StringValidator.html +295 -0
  30. data/doc/ScopedEnv.html +351 -0
  31. data/doc/_index.html +190 -47
  32. data/doc/class_list.html +24 -31
  33. data/doc/css/full_list.css +32 -31
  34. data/doc/css/style.css +244 -91
  35. data/doc/file.README.html +428 -232
  36. data/doc/file_list.html +26 -30
  37. data/doc/frames.html +7 -16
  38. data/doc/index.html +428 -232
  39. data/doc/inheritance.graphml +524 -0
  40. data/doc/inheritance.pdf +825 -0
  41. data/doc/js/app.js +106 -77
  42. data/doc/js/full_list.js +170 -135
  43. data/doc/method_list.html +494 -38
  44. data/doc/top-level-namespace.html +36 -36
  45. data/lib/schemacop.rb +22 -7
  46. data/lib/schemacop/collector.rb +34 -0
  47. data/lib/schemacop/exceptions.rb +2 -8
  48. data/lib/schemacop/field_node.rb +26 -0
  49. data/lib/schemacop/node.rb +127 -0
  50. data/lib/schemacop/node_resolver.rb +14 -0
  51. data/lib/schemacop/node_supporting_field.rb +62 -0
  52. data/lib/schemacop/node_supporting_type.rb +112 -0
  53. data/lib/schemacop/node_with_block.rb +16 -0
  54. data/lib/schemacop/root_node.rb +4 -0
  55. data/lib/schemacop/schema.rb +61 -0
  56. data/lib/schemacop/scoped_env.rb +18 -0
  57. data/lib/schemacop/validator/array_validator.rb +30 -0
  58. data/lib/schemacop/validator/boolean_validator.rb +5 -0
  59. data/lib/schemacop/validator/float_validator.rb +5 -0
  60. data/lib/schemacop/validator/hash_validator.rb +18 -0
  61. data/lib/schemacop/validator/integer_validator.rb +5 -0
  62. data/lib/schemacop/validator/nil_validator.rb +5 -0
  63. data/lib/schemacop/validator/number_validator.rb +19 -0
  64. data/lib/schemacop/validator/object_validator.rb +21 -0
  65. data/lib/schemacop/validator/string_validator.rb +37 -0
  66. data/schemacop.gemspec +7 -5
  67. data/test/custom_check_test.rb +86 -0
  68. data/test/custom_if_test.rb +95 -0
  69. data/test/nil_dis_allow_test.rb +41 -0
  70. data/test/short_forms_test.rb +316 -0
  71. data/test/test_helper.rb +6 -0
  72. data/test/types_test.rb +83 -0
  73. data/test/validator_array_test.rb +97 -0
  74. data/test/validator_boolean_test.rb +15 -0
  75. data/test/validator_float_test.rb +57 -0
  76. data/test/validator_hash_test.rb +71 -0
  77. data/test/validator_integer_test.rb +46 -0
  78. data/test/validator_nil_test.rb +13 -0
  79. data/test/validator_number_test.rb +60 -0
  80. data/test/validator_object_test.rb +87 -0
  81. data/test/validator_string_test.rb +76 -0
  82. metadata +78 -14
  83. data/doc/Schemacop/Exceptions/Base.html +0 -127
  84. data/doc/Schemacop/Exceptions/InvalidSchema.html +0 -141
  85. data/doc/Schemacop/Exceptions/Validation.html +0 -142
  86. data/doc/Schemacop/MethodValidation.html +0 -120
  87. data/doc/Schemacop/MethodValidation/ClassMethods.html +0 -196
  88. data/doc/Schemacop/Validator.html +0 -254
  89. data/lib/schemacop/validator.rb +0 -145
  90. data/test/schemacop_validator_test.rb +0 -257
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'schemacop'
3
+
4
+ def assert_verr(&_block)
5
+ assert_raises(Schemacop::Exceptions::ValidationError) { yield }
6
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class TypesTest < Minitest::Test
5
+ def setup
6
+ @datatype_samples = { array: [],
7
+ boolean: true,
8
+ float: 2.3,
9
+ hash: {},
10
+ integer: -5,
11
+ number: -3.2,
12
+ object: self,
13
+ string: 'miau' }.freeze
14
+ end
15
+
16
+ def test_multitype
17
+ assert_only_types_allowed(
18
+ Schema.new do
19
+ type :integer
20
+ type :boolean
21
+ end, [:integer, :boolean]
22
+ )
23
+
24
+ assert_only_types_allowed(
25
+ Schema.new([:integer, :boolean]),
26
+ [:integer, :boolean]
27
+ )
28
+ end
29
+
30
+ def test_all_data_types
31
+ @datatype_samples.keys.each do |type|
32
+ assert_only_types_allowed(Schema.new(type), type)
33
+ end
34
+ end
35
+
36
+ def test_conditional_types
37
+ s = Schema.new do
38
+ type :boolean
39
+ type :integer, min: 5
40
+ end
41
+
42
+ assert_nil s.validate! true
43
+ assert_nil s.validate! false
44
+ assert_nil s.validate! 5
45
+
46
+ assert_verr { s.validate! 'sali' }
47
+ assert_verr { s.validate! 4 }
48
+ end
49
+
50
+ # For short form test see short_forms_test.rb
51
+ def test_array_subtype_explicit
52
+ s = Schema.new do
53
+ type :array do
54
+ type :integer
55
+ end
56
+ end
57
+ assert_nil s.validate! [5]
58
+ assert_verr { s.validate! [nil] }
59
+ assert_verr { s.validate! ['a'] }
60
+ assert_verr { s.validate! [5, 'a'] }
61
+ assert_verr { s.validate! [5, nil] }
62
+ end
63
+
64
+ private
65
+
66
+ def assert_only_types_allowed(schema, allowed_types)
67
+ allowed_types = [*allowed_types]
68
+ @datatype_samples.each do |type, data|
69
+ if allowed_types.include?(type) ||
70
+ # All the weird cases need to be differentiated
71
+ (type == :float && allowed_types.include?(:number)) ||
72
+ (type == :integer && allowed_types.include?(:number)) ||
73
+ (type == :number && allowed_types.include?(:float)) ||
74
+ allowed_types.include?(:object)
75
+
76
+ assert_nil schema.validate! data
77
+ else
78
+ assert_verr { schema.validate! data }
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,97 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorArrayTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :array do
8
+ type :integer
9
+ end
10
+ end
11
+ assert_nil s.validate!([])
12
+ assert_nil s.validate!([0])
13
+ assert_nil s.validate!([0, 1])
14
+ assert_verr { s.validate!(['string']) }
15
+ end
16
+
17
+ def test_option_min
18
+ s = Schema.new do
19
+ type :array, min: 2
20
+ end
21
+
22
+ assert_nil s.validate!([0, 1])
23
+ assert_nil s.validate!([0, 1, 2])
24
+ assert_verr { s.validate!([]) }
25
+ assert_verr { s.validate!([0]) }
26
+ end
27
+
28
+ def test_option_max
29
+ s = Schema.new do
30
+ type :array, max: 2
31
+ end
32
+
33
+ assert_nil s.validate!([])
34
+ assert_nil s.validate!([0])
35
+ assert_nil s.validate!([0, 1])
36
+ assert_verr { s.validate!([0, 1, 2]) }
37
+ end
38
+
39
+ def test_options_min_max
40
+ s = Schema.new do
41
+ type :array, min: 2, max: 3 do
42
+ type :integer
43
+ end
44
+ end
45
+
46
+ assert_nil s.validate!([1, 2])
47
+ assert_nil s.validate!([1, 2, 3])
48
+ assert_verr { s.validate!([]) }
49
+ assert_verr { s.validate!([1]) }
50
+ assert_verr { s.validate!([1, 2, 3, 4]) }
51
+ end
52
+
53
+ def test_nil
54
+ s = Schema.new do
55
+ type :array, nil: true do
56
+ type :integer
57
+ end
58
+ end
59
+
60
+ assert_nil s.validate!([1, nil, 2])
61
+ assert_verr { s.validate!([1, nil, 'nope']) }
62
+ end
63
+
64
+ def test_multiple_arrays
65
+ s = Schema.new do
66
+ type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
67
+ type :integer
68
+ end
69
+ type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
70
+ type :string
71
+ end
72
+ end
73
+
74
+ assert_nil s.validate!([1, 2, 3])
75
+ assert_nil s.validate!(%w(one two three))
76
+ assert_verr { s.validate!([1, 'mix']) }
77
+ assert_verr { s.validate!([]) }
78
+ end
79
+
80
+ def test_multiple_arrays_with_empty
81
+ s = Schema.new do
82
+ type :array, if: proc { |arr| arr&.first&.is_a?(Integer) } do
83
+ type :integer
84
+ end
85
+ type :array, if: proc { |arr| arr&.first&.is_a?(String) } do
86
+ type :string
87
+ end
88
+ type :array
89
+ end
90
+
91
+ assert_nil s.validate!([])
92
+ assert_nil s.validate!([1, 2, 3])
93
+ assert_nil s.validate!(%w(one two three))
94
+ assert_verr { s.validate!([1, 'mix']) }
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorBooleanTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :boolean
8
+ end
9
+ assert_nil s.validate! true
10
+ assert_nil s.validate! false
11
+ assert_verr { s.validate! nil }
12
+ assert_verr { s.validate! 1 }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorFloatTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :float
8
+ end
9
+ assert_nil s.validate!(-3.0)
10
+ assert_nil s.validate!(-3.123)
11
+ assert_nil s.validate!(0.0)
12
+ assert_nil s.validate!(15.0)
13
+ assert_nil s.validate!(15.13)
14
+ assert_verr { s.validate!(-3) }
15
+ assert_verr { s.validate!(0) }
16
+ assert_verr { s.validate!(15) }
17
+ end
18
+
19
+ def test_option_min
20
+ s = Schema.new do
21
+ type :float, min: -2
22
+ end
23
+
24
+ assert_nil s.validate!(-2.0)
25
+ assert_nil s.validate!(-1.99999)
26
+ assert_nil s.validate!(1.2)
27
+ assert_verr { s.validate!(-5.2) }
28
+ assert_verr { s.validate!(-2.00001) }
29
+ end
30
+
31
+ def test_option_max
32
+ s = Schema.new do
33
+ type :float, max: 5.2
34
+ end
35
+
36
+ assert_nil s.validate!(-2.0)
37
+ assert_nil s.validate!(5.2)
38
+ assert_verr { s.validate!(5.200001) }
39
+ end
40
+
41
+ def test_options_min_max
42
+ s = Schema.new do
43
+ type :float, min: -2, max: 5.2
44
+ end
45
+
46
+ assert_nil s.validate!(-2.0)
47
+ assert_nil s.validate!(-1.99999)
48
+ assert_nil s.validate!(0.0)
49
+ assert_nil s.validate!(1.2)
50
+ assert_nil s.validate!(5.2)
51
+ assert_verr { s.validate!(-2.00001) }
52
+ assert_verr { s.validate!(5.200001) }
53
+ assert_verr { s.validate!(6) }
54
+ assert_verr { s.validate!(0) }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorHashTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :hash do
8
+ req :one do
9
+ type :integer
10
+ end
11
+ req :two do
12
+ type :boolean
13
+ end
14
+ end
15
+ end
16
+ assert_nil s.validate!(one: 3, two: true)
17
+ assert_verr { s.validate!(one: 3) }
18
+ assert_verr { s.validate!(two: true) }
19
+ assert_verr { s.validate!({}) }
20
+ assert_verr { s.validate!(one: 3, two: true, three: 3) }
21
+ assert_verr { s.validate!(one: 3, three: true) }
22
+ end
23
+
24
+ def test_nested
25
+ s = Schema.new do
26
+ type :hash do
27
+ req :h do
28
+ type :hash do
29
+ req :i do
30
+ type :integer
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ s.validate(h: { i: 3 })
37
+ assert_verr { s.validate!(h: { i: true }) }
38
+ assert_verr { s.validate!(h: {}) }
39
+ assert_verr { s.validate!({}) }
40
+ assert_verr { s.validate!(g: { i: 3 }) }
41
+ assert_verr { s.validate!(h: { j: 3 }) }
42
+ end
43
+
44
+ def test_req_opt
45
+ s = Schema.new do
46
+ type :hash do
47
+ req :r do
48
+ type :boolean
49
+ end
50
+ req? :r_nil do
51
+ type :boolean
52
+ end
53
+ opt :o do
54
+ type :boolean
55
+ end
56
+ opt! :o_nonnil do
57
+ type :boolean
58
+ end
59
+ end
60
+ end
61
+
62
+ assert_nil s.validate!(r: true, r_nil: false)
63
+ assert_nil s.validate!(r: true, r_nil: nil)
64
+ assert_nil s.validate!(r: true, r_nil: false, o: false)
65
+ assert_nil s.validate!(r: true, r_nil: false, o: nil)
66
+ assert_verr { s.validate!(r: true, r_nil: false, o_nonnil: nil) }
67
+ assert_verr { s.validate!(o: true) }
68
+ assert_verr { s.validate!(r: nil, r_nil: false, o: true) }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorIntegerTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :integer
8
+ end
9
+ assert_nil s.validate!(-3)
10
+ assert_nil s.validate!(0)
11
+ assert_nil s.validate!(15)
12
+ assert_verr { s.validate!(0.0) }
13
+ end
14
+
15
+ def test_option_min
16
+ s = Schema.new do
17
+ type :integer, min: 6
18
+ end
19
+
20
+ assert_nil s.validate!(6)
21
+ assert_nil s.validate!(7)
22
+ assert_verr { s.validate!(5) }
23
+ end
24
+
25
+ def test_option_max
26
+ s = Schema.new do
27
+ type :integer, max: 7
28
+ end
29
+
30
+ assert_nil s.validate!(6)
31
+ assert_nil s.validate!(7)
32
+ assert_verr { s.validate!(8) }
33
+ end
34
+
35
+ def test_options_min_max
36
+ s = Schema.new do
37
+ type :integer, min: 6, max: 7
38
+ end
39
+
40
+ assert_nil s.validate!(6)
41
+ assert_nil s.validate!(7)
42
+ assert_verr { s.validate!(5) }
43
+ assert_verr { s.validate!(8) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorNilTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :nil
8
+ end
9
+ assert_nil s.validate! nil
10
+ assert_verr { s.validate! false }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class ValidatorNumberTest < Minitest::Test
5
+ def test_basic
6
+ s = Schema.new do
7
+ type :number
8
+ end
9
+ assert_nil s.validate!(-3)
10
+ assert_nil s.validate!(-3.123)
11
+ assert_nil s.validate!(0)
12
+ assert_nil s.validate!(15)
13
+ assert_nil s.validate!(15.13)
14
+ assert_verr { s.validate!('0.12') }
15
+ end
16
+
17
+ def test_option_min
18
+ s = Schema.new do
19
+ type :number, min: -2
20
+ end
21
+
22
+ assert_nil s.validate!(-2)
23
+ assert_nil s.validate!(-1.99999)
24
+ assert_nil s.validate!(0)
25
+ assert_nil s.validate!(1.2)
26
+ assert_verr { s.validate!(-3) }
27
+ assert_verr { s.validate!(-2.00001) }
28
+ end
29
+
30
+ def test_option_max
31
+ s = Schema.new do
32
+ type :number, max: 5.2
33
+ end
34
+
35
+ assert_nil s.validate!(-2)
36
+ assert_nil s.validate!(-1.9)
37
+ assert_nil s.validate!(0)
38
+ assert_nil s.validate!(5.19999)
39
+ assert_nil s.validate!(5.2)
40
+ assert_verr { s.validate!(5.200001) }
41
+ assert_verr { s.validate!(6) }
42
+ end
43
+
44
+ def test_options_min_max
45
+ s = Schema.new do
46
+ type :number, min: -2, max: 5.2
47
+ end
48
+
49
+ assert_nil s.validate!(-2)
50
+ assert_nil s.validate!(-1.99999)
51
+ assert_nil s.validate!(0)
52
+ assert_nil s.validate!(1.2)
53
+ assert_nil s.validate!(5.2)
54
+ assert_verr { s.validate!(-3) }
55
+ assert_verr { s.validate!(-2.00001) }
56
+ assert_verr { s.validate!(5.200001) }
57
+ assert_verr { s.validate!(6) }
58
+ end
59
+ end
60
+ end