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
|
# File 'lib/json_schemer/result.rb', line 131
def insert_property_defaults(context)
instance_locations = {}
results = [[self, true]]
while (result, valid = results.pop)
next if result.source.is_a?(Schema::NOT_KEYWORD_CLASS)
valid &&= result.valid
result.nested&.each { |nested_result| results << [nested_result, valid] }
if result.source.is_a?(Schema::PROPERTIES_KEYWORD_CLASS) && result.instance.is_a?(Hash)
result.source.parsed.each do |property, schema|
next if result.instance.key?(property) || !schema.parsed.key?('default')
default = schema.parsed.fetch('default')
instance_location = Location.join(result.instance_location, property)
keyword_location = Location.join(Location.join(result.keyword_location, property), default.keyword)
default_result = default.validate(nil, instance_location, keyword_location, nil)
instance_locations[result.instance_location] ||= {}
instance_locations[result.instance_location][property] ||= []
instance_locations[result.instance_location][property] << [default_result, valid]
end
end
end
inserted = false
instance_locations.each do |instance_location, properties|
original_instance = context.original_instance(instance_location)
properties.each do |property, results_with_tree_validity|
property_inserted = yield(original_instance, property, results_with_tree_validity)
inserted ||= (property_inserted != false)
end
end
inserted
end
|