Class: JSONSchemer::Draft202012::Vocab::Applicator::Properties

Inherits:
Keyword
  • Object
show all
Defined in:
lib/json_schemer/draft202012/vocab/applicator.rb

Constant Summary

Constants included from Output

Output::FRAGMENT_ENCODE_REGEX

Instance Attribute Summary

Attributes inherited from Keyword

#parent, #parsed, #root, #value

Attributes included from Output

#keyword, #schema

Instance Method Summary collapse

Methods inherited from Keyword

#absolute_keyword_location, #error_key, #fetch, #initialize, #parsed_schema, #schema_pointer

Methods included from Output

#x_error

Constructor Details

This class inherits a constructor from JSONSchemer::Keyword

Instance Method Details

#error(formatted_instance_location:) ⇒ Object



219
220
221
# File 'lib/json_schemer/draft202012/vocab/applicator.rb', line 219

def error(formatted_instance_location:, **)
  "object properties at #{formatted_instance_location} do not match corresponding `properties` schemas"
end

#parseObject



223
224
225
226
227
# File 'lib/json_schemer/draft202012/vocab/applicator.rb', line 223

def parse
  value.each_with_object({}) do |(property, subschema), out|
    out[property] = subschema(subschema, property)
  end
end

#validate(instance, instance_location, keyword_location, context) ⇒ Object



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
# File 'lib/json_schemer/draft202012/vocab/applicator.rb', line 229

def validate(instance, instance_location, keyword_location, context)
  return result(instance, instance_location, keyword_location, true) unless instance.is_a?(Hash)

  if root.before_property_validation.any?
    original_instance = context.original_instance(instance_location)
    root.before_property_validation.each do |hook|
      parsed.each do |property, subschema|
        hook.call(original_instance, property, subschema.value, schema.value)
      end
    end
    instance.replace(deep_stringify_keys(original_instance))
  end

  evaluated_keys = []
  nested = []

  parsed.each do |property, subschema|
    if instance.key?(property)
      evaluated_keys << property
      nested << subschema.validate_instance(instance.fetch(property), join_location(instance_location, property), join_location(keyword_location, property), context)
    end
  end

  if root.after_property_validation.any?
    original_instance = context.original_instance(instance_location)
    root.after_property_validation.each do |hook|
      parsed.each do |property, subschema|
        hook.call(original_instance, property, subschema.value, schema.value)
      end
    end
    instance.replace(deep_stringify_keys(original_instance))
  end

  result(instance, instance_location, keyword_location, nested.all?(&:valid), nested, :annotation => evaluated_keys)
end