Class: JSONSchemer::Draft202012::Vocab::Validation::Required

Inherits:
Keyword
  • Object
show all
Defined in:
lib/json_schemer/draft202012/vocab/validation.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:, details:) ⇒ Object



240
241
242
# File 'lib/json_schemer/draft202012/vocab/validation.rb', line 240

def error(formatted_instance_location:, details:, **)
  "object at #{formatted_instance_location} is missing required properties: #{details.fetch('missing_keys').join(', ')}"
end

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/json_schemer/draft202012/vocab/validation.rb', line 244

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

  required_keys = value

  if context.access_mode && schema.parsed.key?('properties')
    inapplicable_access_mode_keys = []
    schema.parsed.fetch('properties').parsed.each do |property, subschema|
      read_only, write_only = subschema.parsed.values_at('readOnly', 'writeOnly')
      inapplicable_access_mode_keys << property if context.access_mode == 'write' && read_only&.parsed == true
      inapplicable_access_mode_keys << property if context.access_mode == 'read' && write_only&.parsed == true
    end
    required_keys -= inapplicable_access_mode_keys
  end

  missing_keys = required_keys - instance.keys
  result(instance, instance_location, keyword_location, missing_keys.none?, :details => { 'missing_keys' => missing_keys })
end