Class: OpenAPIParser::SchemaValidator::ObjectValidator

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

Instance Attribute Summary

Attributes inherited from Base

#validatable

Instance Method Summary collapse

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, 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



7
8
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
# File 'lib/openapi_parser/schema_validators/object_validator.rb', line 7

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

  return [value, nil] unless schema.properties

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

  coerced_values = value.map do |name, v|
    s = schema.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

  [value, nil]
end