Class: OpenAPIParser::SchemaValidator::ObjectValidator

Inherits:
Base
  • Object
show all
Includes:
PropertiesNumber
Defined in:
lib/openapi_parser/schema_validator/object_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#validatable

Instance Method Summary collapse

Methods included from PropertiesNumber

#check_properties_number

Methods inherited from Base

#initialize, #validate_discriminator_schema

Constructor Details

This class inherits a constructor from OpenAPIParser::SchemaValidator::Base

Instance Method Details

#coerce_and_validate(value, schema, parent_all_of: false, parent_discriminator_schemas: [], discriminator_property_name: nil) ⇒ Object

Parameters:

  • value (Hash)
  • schema (OpenAPIParser::Schemas::Schema)
  • parent_all_of (Boolean) (defaults to: false)

    true if component is nested under allOf

  • discriminator_property_name (String, nil) (defaults to: nil)

    discriminator.property_name to ignore checking additional_properties



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/openapi_parser/schema_validator/object_validator.rb', line 9

def coerce_and_validate(value, schema, parent_all_of: false, parent_discriminator_schemas: [], discriminator_property_name: nil)
  return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Hash)

  properties = schema.properties || {}

  required_set = schema.required ? schema.required.to_set : Set.new
  remaining_keys = value.keys

  if schema.discriminator && !parent_discriminator_schemas.include?(schema)
    return validate_discriminator_schema(
      schema.discriminator,
      value,
      parent_discriminator_schemas: parent_discriminator_schemas + [schema]
    )
  else
    remaining_keys.delete('discriminator')
  end

  coerced_values = value.map do |name, v|
    s = properties[name]
    coerced, err = if s
                     remaining_keys.delete(name)
                     validatable.validate_schema(v, s)
                   else
                     # TODO: we need to perform a validation based on schema.additional_properties here, if
                     # additionalProperties are defined
                     [v, nil]
                   end

    return [nil, err] if err

    required_set.delete(name)
    [name, coerced]
  end

  remaining_keys.delete(discriminator_property_name) if discriminator_property_name

  if !remaining_keys.empty? && !parent_all_of && !schema.additional_properties
    # If object is nested in all of, the validation is already done in allOf validator. Or if
    # additionalProperties are defined, we will validate using that
    return [nil, OpenAPIParser::NotExistPropertyDefinition.new(remaining_keys, schema.object_reference)]
  end
  return [nil, OpenAPIParser::NotExistRequiredKey.new(required_set.to_a, schema.object_reference)] unless required_set.empty?

  value.merge!(coerced_values.to_h) if @coerce_value

  check_properties_number(value, schema)
end