Class: JSONSchemer::OpenAPI31::Vocab::Base::Discriminator
- Defined in:
- lib/json_schemer/openapi31/vocab/base.rb
Constant Summary collapse
- FIXED_FIELD_REGEX =
/\A[a-zA-Z0-9\.\-_]+$\z/
Constants included from JSONSchemer::Output
JSONSchemer::Output::FRAGMENT_ENCODE_REGEX
Instance Attribute Summary collapse
-
#skip_ref_once ⇒ Object
Returns the value of attribute skip_ref_once.
Attributes inherited from Keyword
#parent, #parsed, #root, #value
Attributes included from JSONSchemer::Output
Instance Method Summary collapse
- #error(formatted_instance_location:) ⇒ Object
- #mapping ⇒ Object
- #subschemas_by_property_value ⇒ Object
- #validate(instance, instance_location, keyword_location, context) ⇒ Object
Methods inherited from Keyword
#absolute_keyword_location, #error_key, #fetch, #initialize, #parsed_schema, #schema_pointer
Methods included from JSONSchemer::Output
Constructor Details
This class inherits a constructor from JSONSchemer::Keyword
Instance Attribute Details
#skip_ref_once ⇒ Object
Returns the value of attribute skip_ref_once.
40 41 42 |
# File 'lib/json_schemer/openapi31/vocab/base.rb', line 40 def skip_ref_once @skip_ref_once end |
Instance Method Details
#error(formatted_instance_location:) ⇒ Object
42 43 44 |
# File 'lib/json_schemer/openapi31/vocab/base.rb', line 42 def error(formatted_instance_location:, **) "value at #{formatted_instance_location} does not match `discriminator` schema" end |
#mapping ⇒ Object
46 47 48 |
# File 'lib/json_schemer/openapi31/vocab/base.rb', line 46 def mapping @mapping ||= value['mapping'] || {} end |
#subschemas_by_property_value ⇒ Object
50 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 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/json_schemer/openapi31/vocab/base.rb', line 50 def subschemas_by_property_value @subschemas_by_property_value ||= if schema.parsed.key?('anyOf') || schema.parsed.key?('oneOf') subschemas = schema.parsed['anyOf']&.parsed || [] subschemas += schema.parsed['oneOf']&.parsed || [] subschemas_by_ref = {} subschemas_by_schema_name = {} subschemas.each do |subschema| subschema_ref = subschema.parsed.fetch('$ref').parsed subschemas_by_ref[subschema_ref] = subschema if subschema_ref.start_with?('#/components/schemas/') schema_name = subschema_ref.delete_prefix('#/components/schemas/') subschemas_by_schema_name[schema_name] = subschema if FIXED_FIELD_REGEX.match?(schema_name) end end explicit_mapping = mapping.transform_values do |schema_name_or_ref| subschemas_by_schema_name.fetch(schema_name_or_ref) { subschemas_by_ref.fetch(schema_name_or_ref) } end implicit_mapping = subschemas_by_schema_name.reject do |_schema_name, subschema| explicit_mapping.value?(subschema) end implicit_mapping.merge(explicit_mapping) else Hash.new do |hash, property_value| schema_name_or_ref = mapping.fetch(property_value, property_value) subschema = nil if FIXED_FIELD_REGEX.match?(schema_name_or_ref) subschema = begin schema.ref("#/components/schemas/#{schema_name_or_ref}") rescue InvalidRefPointer nil end end subschema ||= begin schema.ref(schema_name_or_ref) rescue InvalidRefResolution, UnknownRef nil end hash[property_value] = subschema end end end |
#validate(instance, instance_location, keyword_location, context) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/json_schemer/openapi31/vocab/base.rb', line 102 def validate(instance, instance_location, keyword_location, context) return result(instance, instance_location, keyword_location, true) unless instance.is_a?(Hash) property_name = value.fetch('propertyName') return result(instance, instance_location, keyword_location, false) unless instance.key?(property_name) property_value = instance.fetch(property_name) subschema = subschemas_by_property_value[property_value] return result(instance, instance_location, keyword_location, false) unless subschema return if skip_ref_once == subschema.absolute_keyword_location subschema.parsed['allOf']&.skip_ref_once = schema.absolute_keyword_location subschema_result = subschema.validate_instance(instance, instance_location, keyword_location, context) result(instance, instance_location, keyword_location, subschema_result.valid, subschema_result.nested) ensure self.skip_ref_once = nil end |