Class: Schemacop::V3::HashNode
- Inherits:
-
Node
- Object
- Node
- Schemacop::V3::HashNode
show all
- Defined in:
- lib/schemacop/v3/hash_node.rb
Constant Summary
collapse
- ATTRIBUTES =
%i[
type
min_properties
max_properties
dependencies
property_names
].freeze
Instance Attribute Summary collapse
Attributes inherited from Node
#as, #default, #description, #name, #options, #parent, #require_key, #title
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Node
#cast_str_wrapper?, #create, create, #dsl_node, #dsl_scm, #initialize, #require_key?, #required?, resolve_class, #schemas, supports_children, supports_children_options, #used_external_schemas, #validate
Instance Attribute Details
#inline_refs ⇒ Object
Returns the value of attribute inline_refs.
15
16
17
|
# File 'lib/schemacop/v3/hash_node.rb', line 15
def inline_refs
@inline_refs
end
|
#properties ⇒ Object
Returns the value of attribute properties.
14
15
16
|
# File 'lib/schemacop/v3/hash_node.rb', line 14
def properties
@properties
end
|
Class Method Details
.allowed_options ⇒ Object
17
18
19
|
# File 'lib/schemacop/v3/hash_node.rb', line 17
def self.allowed_options
super + ATTRIBUTES - %i[dependencies] + %i[additional_properties ignore_obsolete_properties parse_json]
end
|
.dsl_methods ⇒ Object
21
22
23
|
# File 'lib/schemacop/v3/hash_node.rb', line 21
def self.dsl_methods
super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add]
end
|
Instance Method Details
#_validate(data, result: Result.new) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/schemacop/v3/hash_node.rb', line 98
def _validate(data, result: Result.new)
super_data = super
return if super_data.nil?
super_data = parse_if_json(super_data, result: result, allowed_types: { Hash => :object })
return if super_data.nil?
original_data_hash = super_data.dup
data_hash = super_data.with_indifferent_access
if original_data_hash.size != data_hash.size
ambiguous_properties = original_data_hash.keys - data_hash.keys
result.error "Has #{ambiguous_properties.size} ambiguous properties: #{ambiguous_properties}."
end
if options[:min_properties] && data_hash.size < options[:min_properties]
result.error "Has #{data_hash.size} properties but needs at least #{options[:min_properties]}."
end
if options[:max_properties] && data_hash.size > options[:max_properties]
result.error "Has #{data_hash.size} properties but needs at most #{options[:max_properties]}."
end
@properties.each_value do |node|
result.in_path(node.name) do
next if node.name.is_a?(Regexp)
result.error "Key #{node.name} must be given." if node.require_key? && !data_hash.include?(node.name)
node._validate(data_hash[node.name], result: result)
end
end
inline_ref_property_names = Set.new
@inline_refs.each do |inline_ref|
target = inline_ref.target
next unless target
target.properties.each_value do |prop|
next if prop.name.is_a?(Regexp)
next if @properties.key?(prop.name)
next if inline_ref_property_names.include?(prop.name)
inline_ref_property_names << prop.name
result.in_path(prop.name) do
result.error "Key #{prop.name} must be given." if prop.require_key? && !data_hash.include?(prop.name)
prop._validate(data_hash[prop.name], result: result)
end
end
end
specified_properties = @properties.keys.to_set + inline_ref_property_names
additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s) }
property_patterns = {}
@properties.each_value do |property|
if property.name.is_a?(Regexp)
property_patterns[property.name] = property
end
end
property_names = options[:property_names]
property_names = Regexp.compile(property_names) if property_names
if options[:ignore_obsolete_properties].is_a?(Enumerable)
obsolete_allow_list = options[:ignore_obsolete_properties].to_set(&:to_s)
end
additional_properties.each do |name, additional_property|
if property_names && !V3.match?(property_names, name.to_s)
result.error "Property name #{name.inspect} does not match #{options[:property_names].inspect}."
end
if options[:additional_properties].is_a?(TrueClass)
next
elsif options[:additional_properties].is_a?(FalseClass) || options[:additional_properties].blank?
match = property_patterns.keys.find { |p| V3.match?(p, name.to_s) }
if match
result.in_path(name) do
property_patterns[match]._validate(additional_property, result: result)
end
elsif obsolete_allow_list&.exclude?(name.to_s) ||
!options[:ignore_obsolete_properties]
result.error "Obsolete property #{name.to_s.inspect}."
end
elsif options[:additional_properties].is_a?(Node)
result.in_path(name) do
options[:additional_properties]._validate(additional_property, result: result)
end
end
end
options[:dependencies]&.each do |source, targets|
targets.each do |target|
if !V3.blank?(data_hash[source]) && V3.blank?(data_hash[target])
result.error "Missing property #{target.to_s.inspect} because #{source.to_s.inspect} is given."
end
end
end
end
|
#add_child(node) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/schemacop/v3/hash_node.rb', line 25
def add_child(node)
if node.name.nil?
if node.is_a?(ReferenceNode)
@inline_refs << node
return
else
fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.'
end
end
@properties[node.name] = node
end
|
#allowed_types ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/schemacop/v3/hash_node.rb', line 90
def allowed_types
if options[:parse_json]
{ Hash => :object, String => :string }
else
{ Hash => :object }
end
end
|
#as_json ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/schemacop/v3/hash_node.rb', line 51
def as_json
properties = {}
pattern_properties = {}
@properties.each do |name, property|
if name.is_a?(Regexp)
pattern_properties[name] = property
else
properties[name] = property
end
end
if @inline_refs.any?
return as_json_with_inline_refs(properties, pattern_properties)
end
json = {}
json[:properties] = properties.values.to_h { |p| [p.name, p.as_json] } if properties.any?
json[:patternProperties] = pattern_properties.values.to_h { |p| [V3.sanitize_exp(p.name), p.as_json] } if pattern_properties.any?
if options[:additional_properties].is_a?(TrueClass)
json[:additionalProperties] = true
elsif options[:additional_properties].is_a?(Node)
json[:additionalProperties] = options[:additional_properties].as_json
else
json[:additionalProperties] = false
end
required_properties = @properties.values.select(&:required?).map(&:name)
if required_properties.any?
json[:required] = required_properties
end
return process_json(ATTRIBUTES, json)
end
|
#cast(data) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/schemacop/v3/hash_node.rb', line 215
def cast(data)
data = parse_if_json(data, allowed_types: { Hash => :object })
result = {}.with_indifferent_access
data ||= default
return nil if data.nil?
data_hash = data.dup.with_indifferent_access
property_patterns = {}
as_names = []
@properties.each_value do |prop|
if prop.name.is_a?(Regexp)
property_patterns[prop.name] = prop
next
end
as_names << prop.as&.to_s if prop.as.present?
prop_name = prop.as&.to_s || prop.name
casted_data = prop.cast(data_hash[prop.name])
if !casted_data.nil? || data_hash.include?(prop.name)
result[prop_name] = casted_data
end
if result[prop_name].nil? && !data_hash.include?(prop.name) && !as_names.include?(prop.name)
result.delete(prop_name)
end
end
inline_ref_property_names = Set.new
@inline_refs.each do |inline_ref|
target = inline_ref.target
next unless target
target.properties.each_value do |prop|
next if prop.name.is_a?(Regexp)
next if @properties.key?(prop.name)
next if inline_ref_property_names.include?(prop.name)
inline_ref_property_names << prop.name
prop_name = prop.as&.to_s || prop.name
casted_data = prop.cast(data_hash[prop.name])
if !casted_data.nil? || data_hash.include?(prop.name)
result[prop_name] = casted_data
end
if result[prop_name].nil? && !data_hash.include?(prop.name)
result.delete(prop_name)
end
end
end
specified_properties = @properties.keys.to_set + inline_ref_property_names
additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s) }
if additional_properties.any? && property_patterns.any?
additional_properties.each do |name, additional_property|
match_key = property_patterns.keys.find { |p| V3.match?(p, name.to_s) }
next unless match_key
result[name] = property_patterns[match_key].cast(additional_property)
end
end
if options[:additional_properties].is_a?(TrueClass)
result = data_hash.reject { |k, _v| specified_properties.include?(k.to_s) }.merge(result)
elsif options[:additional_properties].is_a?(Node)
additional_properties = data_hash.reject { |k, _v| specified_properties.include?(k.to_s) }
if additional_properties.any?
additional_properties_result = {}
additional_properties.each do |key, value|
additional_properties_result[key] = options[:additional_properties].cast(value)
end
result = additional_properties_result.merge(result)
end
end
return result
end
|
#children ⇒ Object
211
212
213
|
# File 'lib/schemacop/v3/hash_node.rb', line 211
def children
@properties.values + @inline_refs
end
|
#dsl_add(type, **options, &block) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/schemacop/v3/hash_node.rb', line 38
def dsl_add(type, **options, &block)
if @options[:additional_properties].is_a?(Node)
fail Exceptions::InvalidSchemaError, 'You can only use "add" once to specify additional properties.'
end
@options[:additional_properties] = create(type, **options, &block)
end
|
#dsl_dep(source, *targets, **_kwargs) ⇒ Object
46
47
48
49
|
# File 'lib/schemacop/v3/hash_node.rb', line 46
def dsl_dep(source, *targets, **_kwargs)
@options[:dependencies] ||= {}
@options[:dependencies][source] = targets
end
|