schemacop 2.4.7 → 3.0.0.rc4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) 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 +37 -0
  7. data/README.md +53 -710
  8. data/README_V2.md +775 -0
  9. data/README_V3.md +1253 -0
  10. data/Rakefile +8 -12
  11. data/VERSION +1 -1
  12. data/lib/schemacop.rb +35 -37
  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} +17 -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/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 +267 -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 +219 -0
  52. data/lib/schemacop/v3/node_registry.rb +45 -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 +55 -0
  58. data/lib/schemacop/v3/result.rb +58 -0
  59. data/lib/schemacop/v3/string_node.rb +132 -0
  60. data/lib/schemacop/v3/symbol_node.rb +13 -0
  61. data/schemacop.gemspec +24 -27
  62. data/test/lib/test_helper.rb +167 -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 +157 -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 +106 -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 +166 -0
  90. data/test/unit/schemacop/v3/hash_node_test.rb +972 -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 +162 -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 +367 -0
  98. data/test/unit/schemacop/v3/string_node_test.rb +372 -0
  99. data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
  100. metadata +151 -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 -118
  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,219 @@
1
+ module Schemacop
2
+ module V3
3
+ class Node
4
+ attr_reader :name
5
+ attr_reader :as
6
+ attr_reader :default
7
+ attr_reader :title
8
+ attr_reader :description
9
+ attr_reader :options
10
+ attr_reader :parent
11
+
12
+ class_attribute :_supports_children
13
+ self._supports_children = nil
14
+
15
+ def self.supports_children(name: false)
16
+ self._supports_children = { name: name }
17
+ end
18
+
19
+ def self.supports_children_options
20
+ _supports_children
21
+ end
22
+
23
+ def self.resolve_class(type)
24
+ NodeRegistry.find(type)
25
+ end
26
+
27
+ def self.create(type = self, **options, &block)
28
+ klass = resolve_class(type)
29
+ fail "Could not find node for type #{type.inspect}." unless klass
30
+
31
+ node = klass.new(**options, &block)
32
+
33
+ if options.delete(:cast_str)
34
+ format = NodeRegistry.name(klass)
35
+ one_of_options = {
36
+ required: options.delete(:required),
37
+ name: options.delete(:name),
38
+ as: options.delete(:as),
39
+ description: options.delete(:description)
40
+ }
41
+ node = create(:one_of, **one_of_options) do
42
+ self.node node
43
+ str format: format, format_options: options
44
+ end
45
+ end
46
+
47
+ return node
48
+ end
49
+
50
+ def self.allowed_options
51
+ %i[name required default description examples enum parent options cast_str title as]
52
+ end
53
+
54
+ def self.dsl_methods
55
+ %i[dsl_scm dsl_node]
56
+ end
57
+
58
+ def allowed_types
59
+ {}
60
+ end
61
+
62
+ def used_external_schemas
63
+ children.map(&:used_external_schemas).flatten.uniq
64
+ end
65
+
66
+ def children
67
+ []
68
+ end
69
+
70
+ def initialize(**options, &block)
71
+ # Check options #
72
+ disallowed_options = options.keys - self.class.allowed_options
73
+
74
+ if disallowed_options.any?
75
+ fail Schemacop::Exceptions::InvalidSchemaError, "Options #{disallowed_options.inspect} are not allowed for this node."
76
+ end
77
+
78
+ # Assign attributes #
79
+ @name = options.delete(:name)
80
+ @name = @name.to_s unless @name.nil? || @name.is_a?(Regexp)
81
+ @as = options.delete(:as)
82
+ @required = !!options.delete(:required)
83
+ @default = options.delete(:default)
84
+ @title = options.delete(:title)
85
+ @description = options.delete(:description)
86
+ @examples = options.delete(:examples)
87
+ @enum = options.delete(:enum)&.to_set
88
+ @parent = options.delete(:parent)
89
+ @options = options
90
+ @schemas = {}
91
+
92
+ # Run subclass init #
93
+ init
94
+
95
+ # Run DSL block #
96
+ if block_given?
97
+ unless self.class.supports_children_options
98
+ fail Schemacop::Exceptions::InvalidSchemaError, "Node #{self.class} does not support blocks."
99
+ end
100
+
101
+ scope = DslScope.new(self)
102
+ env = ScopedEnv.new(self, self.class.dsl_methods, scope, :dsl_)
103
+ env.instance_exec(&block)
104
+ end
105
+
106
+ # Validate self #
107
+ begin
108
+ validate_self
109
+ rescue StandardError => e
110
+ fail Exceptions::InvalidSchemaError, e.message
111
+ end
112
+ end
113
+
114
+ def create(type, **options, &block)
115
+ options[:parent] = self
116
+ return Node.create(type, **options, &block)
117
+ end
118
+
119
+ def init; end
120
+
121
+ def dsl_scm(name, type = :hash, **options, &block)
122
+ @schemas[name] = create(type, **options, &block)
123
+ end
124
+
125
+ def dsl_node(node, *_args, **_kwargs)
126
+ add_child node
127
+ end
128
+
129
+ def schemas
130
+ (parent&.schemas || {}).merge(@schemas)
131
+ end
132
+
133
+ def required?
134
+ @required
135
+ end
136
+
137
+ def as_json
138
+ process_json([], {})
139
+ end
140
+
141
+ def cast(value)
142
+ value || default
143
+ end
144
+
145
+ def validate(data)
146
+ result = Result.new(self, data)
147
+ _validate(data, result: result)
148
+ return result
149
+ end
150
+
151
+ protected
152
+
153
+ def item_matches?(item, data)
154
+ item_result = Result.new(self)
155
+ item._validate(data, result: item_result)
156
+ return item_result.errors.none?
157
+ end
158
+
159
+ def process_json(attrs, json)
160
+ if @schemas.any?
161
+ json[:definitions] = {}
162
+ @schemas.each do |name, subschema|
163
+ json[:definitions][name] = subschema.as_json
164
+ end
165
+ end
166
+ attrs.each do |attr|
167
+ if options.include?(attr)
168
+ json[attr.to_s.camelize(:lower).to_sym] = options[attr]
169
+ end
170
+ end
171
+
172
+ json[:title] = @title if @title
173
+ json[:examples] = @examples if @examples
174
+ json[:description] = @description if @description
175
+ json[:default] = @default if @default
176
+ json[:enum] = @enum.to_a if @enum
177
+
178
+ return json.as_json
179
+ end
180
+
181
+ def type_assertion_method
182
+ :is_a?
183
+ end
184
+
185
+ def _validate(data, result:)
186
+ # Validate nil #
187
+ if data.nil? && required?
188
+ result.error 'Value must be given.'
189
+ return nil
190
+ end
191
+
192
+ # Apply default #
193
+ if data.nil?
194
+ if default
195
+ data = default
196
+ else
197
+ return nil
198
+ end
199
+ end
200
+
201
+ # Validate type #
202
+ if allowed_types.any? && allowed_types.keys.none? { |c| data.send(type_assertion_method, c) }
203
+ collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
204
+ result.error %(Invalid type, expected #{collection}.)
205
+ return nil
206
+ end
207
+
208
+ # Validate enums #
209
+ if @enum && !@enum.include?(data)
210
+ result.error "Value not included in enum #{@enum.to_a.inspect}."
211
+ end
212
+
213
+ return data
214
+ end
215
+
216
+ def validate_self; end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,45 @@
1
+ module Schemacop
2
+ module V3
3
+ class NodeRegistry
4
+ @by_name = {}
5
+ @by_short_name = {}
6
+ @by_class = {}
7
+
8
+ def self.register(name, short_name, klass)
9
+ @by_name[name.to_sym] = klass
10
+ @by_short_name[short_name.to_sym] = klass
11
+ @by_class[klass] = { name: name.to_sym, short_name: short_name.to_sym }
12
+ end
13
+
14
+ def self.dsl_methods(bang)
15
+ return @by_short_name.keys.map do |short_name|
16
+ if bang
17
+ ["dsl_#{short_name}!", "dsl_#{short_name}?"]
18
+ else
19
+ ["dsl_#{short_name}"]
20
+ end
21
+ end.flatten.map(&:to_sym)
22
+ end
23
+
24
+ def self.find(name_or_klass)
25
+ if name_or_klass.is_a?(Class)
26
+ return name_or_klass
27
+ else
28
+ return by_name(name_or_klass)
29
+ end
30
+ end
31
+
32
+ def self.by_name(name)
33
+ @by_name[name.to_sym]
34
+ end
35
+
36
+ def self.by_short_name(short_name)
37
+ @by_short_name[short_name.to_sym]
38
+ end
39
+
40
+ def self.name(klass)
41
+ @by_class[klass][:name]
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ module Schemacop
2
+ module V3
3
+ class NumberNode < NumericNode
4
+ def as_json
5
+ process_json(ATTRIBUTES, type: :number)
6
+ end
7
+
8
+ def allowed_types
9
+ {
10
+ Integer => :integer,
11
+ Float => :float,
12
+ Rational => :rational,
13
+ BigDecimal => :big_decimal
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,76 @@
1
+ module Schemacop
2
+ module V3
3
+ # @abstract
4
+ class NumericNode < Node
5
+ ATTRIBUTES = %i[
6
+ minimum
7
+ exclusive_minimum
8
+ maximum
9
+ exclusive_maximum
10
+ multiple_of
11
+ ].freeze
12
+
13
+ def self.allowed_options
14
+ super + ATTRIBUTES
15
+ end
16
+
17
+ def _validate(data, result:)
18
+ super_data = super
19
+ return if super_data.nil?
20
+
21
+ # Validate minimum #
22
+ if options[:minimum] && super_data < options[:minimum]
23
+ result.error "Value must have a minimum of #{options[:minimum]}."
24
+ end
25
+
26
+ if options[:exclusive_minimum] && super_data <= options[:exclusive_minimum]
27
+ result.error "Value must have an exclusive minimum of #{options[:exclusive_minimum]}."
28
+ end
29
+
30
+ # Validate maximum #
31
+ if options[:maximum] && super_data > options[:maximum]
32
+ result.error "Value must have a maximum of #{options[:maximum]}."
33
+ end
34
+
35
+ if options[:exclusive_maximum] && super_data >= options[:exclusive_maximum]
36
+ result.error "Value must have an exclusive maximum of #{options[:exclusive_maximum]}."
37
+ end
38
+
39
+ # Validate multiple of #
40
+ if options[:multiple_of] && !compare_float((super_data % options[:multiple_of]), 0.0)
41
+ result.error "Value must be a multiple of #{options[:multiple_of]}."
42
+ end
43
+ end
44
+
45
+ def validate_self
46
+ # Check that the options have the correct type
47
+ ATTRIBUTES.each do |attribute|
48
+ next if options[attribute].nil?
49
+ next unless allowed_types.keys.none? { |c| options[attribute].send(type_assertion_method, c) }
50
+
51
+ collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ')
52
+ fail "Option \"#{attribute}\" must be a #{collection}"
53
+ end
54
+
55
+ if options[:minimum] && options[:maximum] && options[:minimum] > options[:maximum]
56
+ fail 'Option "minimum" can\'t be greater than "maximum".'
57
+ end
58
+
59
+ if options[:exclusive_minimum] && options[:exclusive_maximum]\
60
+ && options[:exclusive_minimum] > options[:exclusive_maximum]
61
+ fail 'Option "exclusive_minimum" can\'t be greater than "exclusive_maximum".'
62
+ end
63
+
64
+ if options[:multiple_of]&.zero?
65
+ fail 'Option "multiple_of" can\'t be 0.'
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def compare_float(first, second)
72
+ (first - second).abs < Float::EPSILON
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,40 @@
1
+ module Schemacop
2
+ module V3
3
+ class ObjectNode < Node
4
+ def self.allowed_options
5
+ super + %i[classes strict]
6
+ end
7
+
8
+ def self.create(classes, **options, &block)
9
+ options[:classes] = classes
10
+ super(**options, &block)
11
+ end
12
+
13
+ def as_json
14
+ {} # Not supported by Json Schema
15
+ end
16
+
17
+ protected
18
+
19
+ def allowed_types
20
+ Hash[@classes.map { |c| [c, c.name] }]
21
+ end
22
+
23
+ def init
24
+ @classes = Array(options.delete(:classes) || [])
25
+ @strict = options.delete(:strict)
26
+ @strict = true if @strict.nil?
27
+ end
28
+
29
+ def type_assertion_method
30
+ @strict ? :instance_of? : :is_a?
31
+ end
32
+
33
+ def validate_self
34
+ unless @strict.is_a?(TrueClass) || @strict.is_a?(FalseClass)
35
+ fail 'Option "strict" must be a "boolean".'
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ module Schemacop
2
+ module V3
3
+ class OneOfNode < CombinationNode
4
+ def type
5
+ :oneOf
6
+ end
7
+
8
+ def _validate(data, result:)
9
+ super_data = super
10
+ return if super_data.nil?
11
+
12
+ matches = matches(super_data)
13
+
14
+ if matches.size == 1
15
+ matches.first._validate(super_data, result: result)
16
+ else
17
+ result.error "Matches #{matches.size} definitions but should match exactly 1."
18
+ end
19
+ end
20
+
21
+ def validate_self
22
+ if @items.size < 2
23
+ fail 'Node "one_of" makes only sense with at least 2 items.'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ module Schemacop
2
+ module V3
3
+ class ReferenceNode < Node
4
+ def self.allowed_options
5
+ super + %i[path]
6
+ end
7
+
8
+ def self.create(path, **options, &block)
9
+ options[:path] = path
10
+ super(**options, &block)
11
+ end
12
+
13
+ def as_json
14
+ process_json([], '$ref': "#/definitions/#{@path}")
15
+ end
16
+
17
+ def _validate(data, result:)
18
+ super_data = super
19
+ return if super_data.nil?
20
+
21
+ # Lookup schema #
22
+ node = target
23
+ fail "Schema #{@path.to_s.inspect} not found." unless node
24
+
25
+ # Validate schema #
26
+ node._validate(super_data, result: result)
27
+ end
28
+
29
+ def target
30
+ schemas[@path] || Schemacop.context.schemas[@path] || GlobalContext.schema_for(@path)
31
+ end
32
+
33
+ def cast(data)
34
+ data = default if data.nil?
35
+ return target.cast(data)
36
+ end
37
+
38
+ def used_external_schemas
39
+ target_children_schema = target.used_external_schemas
40
+
41
+ if schemas.include?(@path)
42
+ return target_children_schema
43
+ else
44
+ return [@path] + target_children_schema
45
+ end
46
+ end
47
+
48
+ protected
49
+
50
+ def init
51
+ @path = options.delete(:path)
52
+ end
53
+ end
54
+ end
55
+ end