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,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class NodeResolver
3
3
  class_attribute :node_classes
4
4
  self.node_classes = [].freeze
@@ -0,0 +1,70 @@
1
+ module Schemacop::V2
2
+ class NodeSupportingField < NodeWithBlock
3
+ block_method :req?
4
+ block_method :req!
5
+ block_method :req
6
+ block_method :opt?
7
+ block_method :opt!
8
+ block_method :opt
9
+
10
+ attr_reader :fields
11
+
12
+ def initialize(options = {}, &block)
13
+ @fields = {}
14
+ super
15
+ exec_block(&block)
16
+ end
17
+
18
+ def req?(*args, **kwargs, &block)
19
+ kwargs ||= {}
20
+ kwargs[:required] = true
21
+ kwargs[:allow_nil] = true
22
+ field(*args, **kwargs, &block)
23
+ end
24
+
25
+ def req!(*args, **kwargs, &block)
26
+ kwargs ||= {}
27
+ kwargs[:required] = true
28
+ kwargs[:allow_nil] = false
29
+ field(*args, **kwargs, &block)
30
+ end
31
+
32
+ alias req req!
33
+
34
+ def opt?(*args, **kwargs, &block)
35
+ kwargs ||= {}
36
+ kwargs[:required] = false
37
+ kwargs[:allow_nil] = true
38
+ field(*args, **kwargs, &block)
39
+ end
40
+
41
+ def opt!(*args, **kwargs, &block)
42
+ kwargs ||= {}
43
+ kwargs[:required] = false
44
+ kwargs[:allow_nil] = false
45
+ field(*args, **kwargs, &block)
46
+ end
47
+
48
+ alias opt opt?
49
+
50
+ protected
51
+
52
+ def field(*args, **kwargs, &block)
53
+ name = args.shift
54
+ required = kwargs.delete(:required)
55
+ allow_nil = kwargs.delete(:allow_nil)
56
+
57
+ if @fields[name]
58
+ @fields[name].type(*args, **kwargs, &block)
59
+ elsif args.any?
60
+ @fields[name] = FieldNode.new(name, required) do
61
+ type(*args, **kwargs, &block)
62
+ end
63
+ else
64
+ @fields[name] = FieldNode.new(name, required, &block)
65
+ end
66
+
67
+ @fields[name].type(:nil) if allow_nil
68
+ end
69
+ end
70
+ end
@@ -1,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class NodeSupportingType < NodeWithBlock
3
3
  block_method :type
4
4
 
@@ -14,6 +14,7 @@ module Schemacop
14
14
 
15
15
  if @types.none?
16
16
  fail Exceptions::InvalidSchemaError, 'Block must contain a type definition or not be given at all.' if block_given?
17
+
17
18
  type :object
18
19
  end
19
20
 
@@ -32,7 +33,7 @@ module Schemacop
32
33
  super
33
34
  rescue NoMethodError
34
35
  @types = []
35
- type :hash, {}, &block
36
+ type :hash, &block
36
37
  end
37
38
 
38
39
  # required signature:
@@ -46,8 +47,8 @@ module Schemacop
46
47
  # happens in here directly and not in the constructor. This way we can
47
48
  # always call 'type', even if we don't have one and the type is auto-guessed
48
49
  # as it formerly was the case in the constructor.
49
- def type(*args, &block)
50
- options = args.last.is_a?(Hash) ? args.pop : {}
50
+ def type(*args, **kwargs, &block)
51
+ options = kwargs
51
52
  types = [*args.shift]
52
53
  subtypes = args
53
54
 
@@ -72,7 +73,7 @@ module Schemacop
72
73
  end
73
74
 
74
75
  child = klass.new do
75
- self.type(*subtypes, options, &block)
76
+ self.type(*subtypes, **options, &block)
76
77
  end
77
78
 
78
79
  # child = klass.build(options)
@@ -101,9 +102,11 @@ module Schemacop
101
102
  def cast!(data, collector)
102
103
  @types.each do |type|
103
104
  next unless type.option?(:cast) && !type.type_matches?(data) && type.type_filter_matches?(data)
105
+
104
106
  caster = Caster.new(type.option(:cast), data, type.class.klasses.first)
105
107
 
106
108
  next unless caster.castable?
109
+
107
110
  begin
108
111
  data = caster.cast
109
112
  collector.override_value(data)
@@ -1,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class NodeWithBlock < Node
3
3
  class_attribute :block_methods
4
4
  self.block_methods = [].freeze
@@ -9,7 +9,8 @@ module Schemacop
9
9
 
10
10
  def exec_block(&block)
11
11
  return unless block_given?
12
- se = ScopedEnv.new(self, self.class.block_methods)
12
+
13
+ se = Schemacop::ScopedEnv.new(self, self.class.block_methods)
13
14
  se.instance_exec(&block)
14
15
  end
15
16
  end
File without changes
@@ -0,0 +1,32 @@
1
+ module Schemacop
2
+ module V2
3
+ class ArrayValidator < NodeSupportingType
4
+ register symbols: :array, klasses: Array
5
+
6
+ option :min # Minimal number of elements
7
+ option :max # Maximal number of elements
8
+ option :nil # Whether to allow nil values
9
+
10
+ def initialize(*args)
11
+ super
12
+ type(:nil) if option(:nil)
13
+ end
14
+
15
+ def validate(data, collector)
16
+ validate_custom_check(data, collector)
17
+
18
+ if option?(:min) && data.size < option(:min)
19
+ collector.error "Array must have more (>=) than #{option(:min)} elements."
20
+ end
21
+ if option?(:max) && data.size > option(:max)
22
+ collector.error "Array must have less (<=) than #{option(:max)} elements."
23
+ end
24
+ data.each_with_index do |entry, index|
25
+ collector.path("[#{index}]", index, :array) do
26
+ validate_types(entry, collector)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class BooleanValidator < Node
3
3
  register symbols: :boolean, klasses: [TrueClass, FalseClass]
4
4
  end
@@ -0,0 +1,7 @@
1
+ module Schemacop
2
+ module V2
3
+ class FloatValidator < NumberValidator
4
+ register symbols: :float, klasses: Float, before: NumberValidator
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ module Schemacop
2
+ module V2
3
+ class HashValidator < NodeSupportingField
4
+ register symbols: :hash, klasses: Hash
5
+
6
+ option :allow_obsolete_keys
7
+
8
+ def validate(data, collector)
9
+ super
10
+
11
+ if data.is_a? ActiveSupport::HashWithIndifferentAccess
12
+ allowed_fields = @fields.keys.map { |k| k.is_a?(String) ? k.to_sym : k }
13
+ data_keys = data.keys.map { |k| k.is_a?(String) ? k.to_sym : k }
14
+
15
+ # If the same key is specified in the schema as string and symbol, we
16
+ # definitely expect a Ruby hash and not one with indifferent access
17
+ if @fields.keys.length != Set.new(allowed_fields).length
18
+ fail Exceptions::ValidationError, 'Hash expected, but got ActiveSupport::HashWithIndifferentAccess.'
19
+ end
20
+ else
21
+ allowed_fields = @fields.keys
22
+ data_keys = data.keys
23
+ end
24
+
25
+ obsolete_keys = data_keys - allowed_fields
26
+
27
+ if !option?(:allow_obsolete_keys) && obsolete_keys.any?
28
+ collector.error "Obsolete keys: #{obsolete_keys.inspect}."
29
+ end
30
+
31
+ @fields.each_value do |field|
32
+ field.validate(data, collector)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module Schemacop
2
+ module V2
3
+ class IntegerValidator < NumberValidator
4
+ register symbols: :integer, klasses: Integer, before: NumberValidator
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class NilValidator < Node
3
3
  register symbols: :nil, klasses: NilClass
4
4
  end
@@ -0,0 +1,21 @@
1
+ module Schemacop
2
+ module V2
3
+ class NumberValidator < Node
4
+ register symbols: :number, klasses: [Integer, Float]
5
+
6
+ option :min
7
+ option :max
8
+
9
+ def validate(data, collector)
10
+ super
11
+
12
+ if option?(:min) && data < option(:min)
13
+ collector.error "Value must be >= #{option(:min)}."
14
+ end
15
+ if option?(:max) && data > option(:max)
16
+ collector.error "Value must be <= #{option(:max)}."
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Schemacop
2
+ module V2
3
+ class ObjectValidator < Node
4
+ register symbols: :object, klasses: BasicObject
5
+
6
+ option :classes
7
+ option :strict
8
+
9
+ def type_label
10
+ "#{super} (#{classes.join(', ')})"
11
+ end
12
+
13
+ def type_matches?(data)
14
+ if option(:strict).is_a?(FalseClass)
15
+ sub_or_class = classes.map { |klass| data.class <= klass }.include?(true)
16
+ super && (classes.empty? || sub_or_class) && !data.nil?
17
+ else
18
+ super && (classes.empty? || classes.include?(data.class)) && !data.nil?
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def classes
25
+ [*option(:classes)]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ module Schemacop
2
+ module V2
3
+ class StringValidator < Node
4
+ register symbols: :string, klasses: String
5
+
6
+ option :min
7
+ option :max
8
+
9
+ def initialize(options = {})
10
+ super(options)
11
+
12
+ validate_options!
13
+ end
14
+
15
+ def validate(data, collector)
16
+ super
17
+
18
+ if option?(:min) && data.size < option(:min)
19
+ collector.error "String must be longer (>=) than #{option(:min)} characters."
20
+ end
21
+ if option?(:max) && data.size > option(:max)
22
+ collector.error "String must be shorter (<=) than #{option(:max)} characters."
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def validate_options!
29
+ option_schema = Schema.new :integer, min: 0
30
+
31
+ if option?(:min) && option_schema.invalid?(option(:min))
32
+ fail Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.'
33
+ elsif option?(:max) && option_schema.invalid?(option(:max))
34
+ fail Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.'
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,4 +1,4 @@
1
- module Schemacop
1
+ module Schemacop::V2
2
2
  class SymbolValidator < Node
3
3
  register symbols: :symbol, klasses: Symbol
4
4
  end
@@ -0,0 +1,45 @@
1
+ module Schemacop
2
+ module V3
3
+ def self.register(*args)
4
+ NodeRegistry.register(*args)
5
+ end
6
+ end
7
+ end
8
+
9
+ # Require V3 files
10
+ require 'schemacop/v3/node_registry'
11
+ require 'schemacop/v3/dsl_scope'
12
+ require 'schemacop/v3/context'
13
+ require 'schemacop/v3/global_context'
14
+ require 'schemacop/v3/result'
15
+ require 'schemacop/v3/node'
16
+ require 'schemacop/v3/combination_node'
17
+ require 'schemacop/v3/numeric_node'
18
+ require 'schemacop/v3/all_of_node'
19
+ require 'schemacop/v3/any_of_node'
20
+ require 'schemacop/v3/array_node'
21
+ require 'schemacop/v3/boolean_node'
22
+ require 'schemacop/v3/hash_node'
23
+ require 'schemacop/v3/integer_node'
24
+ require 'schemacop/v3/is_not_node'
25
+ require 'schemacop/v3/number_node'
26
+ require 'schemacop/v3/object_node'
27
+ require 'schemacop/v3/one_of_node'
28
+ require 'schemacop/v3/reference_node'
29
+ require 'schemacop/v3/string_node'
30
+ require 'schemacop/v3/symbol_node'
31
+
32
+ # Register built-in nodes
33
+ Schemacop::V3.register :all_of, :all_of, Schemacop::V3::AllOfNode
34
+ Schemacop::V3.register :any_of, :any_of, Schemacop::V3::AnyOfNode
35
+ Schemacop::V3.register :array, :ary, Schemacop::V3::ArrayNode
36
+ Schemacop::V3.register :boolean, :boo, Schemacop::V3::BooleanNode
37
+ Schemacop::V3.register :integer, :int, Schemacop::V3::IntegerNode
38
+ Schemacop::V3.register :is_not, :is_not, Schemacop::V3::IsNotNode
39
+ Schemacop::V3.register :number, :num, Schemacop::V3::NumberNode
40
+ Schemacop::V3.register :hash, :hsh, Schemacop::V3::HashNode
41
+ Schemacop::V3.register :one_of, :one_of, Schemacop::V3::OneOfNode
42
+ Schemacop::V3.register :reference, :ref, Schemacop::V3::ReferenceNode
43
+ Schemacop::V3.register :object, :obj, Schemacop::V3::ObjectNode
44
+ Schemacop::V3.register :string, :str, Schemacop::V3::StringNode
45
+ Schemacop::V3.register :symbol, :sym, Schemacop::V3::SymbolNode
@@ -0,0 +1,27 @@
1
+ module Schemacop
2
+ module V3
3
+ class AllOfNode < CombinationNode
4
+ def type
5
+ :allOf
6
+ end
7
+
8
+ def _validate(data, result:)
9
+ super_data = super
10
+ return if super_data.nil?
11
+
12
+ if matches(super_data).size != @items.size
13
+ result.error 'Does not match all allOf conditions.'
14
+ end
15
+ end
16
+
17
+ def cast(value)
18
+ items = matches(value)
19
+ return value unless items
20
+
21
+ casted_value = value.dup
22
+ items.each { |i| casted_value = i.cast(casted_value) }
23
+ return casted_value
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Schemacop
2
+ module V3
3
+ class AnyOfNode < CombinationNode
4
+ def type
5
+ :anyOf
6
+ end
7
+
8
+ def _validate(data, result:)
9
+ super_data = super
10
+ return if super_data.nil?
11
+
12
+ match = match(super_data)
13
+
14
+ if match
15
+ match._validate(super_data, result: result)
16
+ else
17
+ result.error 'Does not match any anyOf condition.'
18
+ end
19
+ end
20
+
21
+ def validate_self
22
+ if @items.empty?
23
+ fail 'Node "any_of" makes only sense with at least 1 item.'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end