Class: OpenAPIParser::SchemaValidator::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validatable, coerce_value) ⇒ Base

Returns a new instance of Base.



4
5
6
7
# File 'lib/openapi_parser/schema_validators/base.rb', line 4

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

Instance Attribute Details

#validatableOpenAPIParser::SchemaValidator::Validatable (readonly)



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

def validatable
  @validatable
end

Instance Method Details

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

need override

Parameters:



17
18
19
# File 'lib/openapi_parser/schema_validators/base.rb', line 17

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

#validate_discriminator_schema(discriminator, value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openapi_parser/schema_validators/base.rb', line 21

def validate_discriminator_schema(discriminator, value)
  unless value.key?(discriminator.property_name)
    return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
  end
  mapping_key = value[discriminator.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})
end