Class: OpenAPIParser::SchemaValidator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_parser/schema_validator/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validatable, coerce_value) ⇒ Base

Returns a new instance of Base.



3
4
5
6
# File 'lib/openapi_parser/schema_validator/base.rb', line 3

def initialize(validatable, coerce_value)
  @validatable = validatable
  @coerce_value = coerce_value
end

Instance Attribute Details

#validatableObject (readonly)

Returns the value of attribute validatable.



8
9
10
# File 'lib/openapi_parser/schema_validator/base.rb', line 8

def validatable
  @validatable
end

Instance Method Details

#coerce_and_validate(_value, _schema, **_keyword_args) ⇒ Object

need override



11
12
13
# File 'lib/openapi_parser/schema_validator/base.rb', line 11

def coerce_and_validate(_value, _schema, **_keyword_args)
  raise 'need implement'
end

#validate_discriminator_schema(discriminator, value, parent_discriminator_schemas: []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openapi_parser/schema_validator/base.rb', line 15

def validate_discriminator_schema(discriminator, value, parent_discriminator_schemas: [])
  property_name = discriminator.property_name
  if property_name.nil? || !value.key?(property_name)
    return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
  end
  mapping_key = value[property_name]

  # it's allowed to have discriminator without mapping, then we need to lookup discriminator.property_name
  # but the format is not the full path, just model name in the components
  mapping_target = discriminator.mapping&.[](mapping_key) || "#/components/schemas/#{mapping_key}"

  # Find object does O(n) search at worst, then caches the result, so this is ok for repeated search
  resolved_schema = discriminator.root.find_object(mapping_target)

  unless resolved_schema
    return [nil, OpenAPIParser::NotExistDiscriminatorMappedSchema.new(mapping_target, discriminator.object_reference)]
  end
  validatable.validate_schema(
    value,
    resolved_schema,
    **{discriminator_property_name: discriminator.property_name, parent_discriminator_schemas: parent_discriminator_schemas}
  )
end