Sha256: 7298c09fdc0c67a7a83d94cbc91e23c9c339283b315a4ef4599090b821b894ed

Contents?: true

Size: 867 Bytes

Versions: 1

Compression:

Stored size: 867 Bytes

Contents

class OpenAPIParser::SchemaValidator
  class ObjectValidator < Base

    # @param [Hash] value
    # @param [OpenAPIParser::Schemas::Schema] schema
    def coerce_and_validate(value, schema)
      return validator.validate_error(value, schema) unless value.is_a?(Hash)

      return [value, nil] unless schema.properties
      required_set = schema.required ? schema.required.to_set : Set.new

      coerced_values = value.map do |name, v|
        s = schema.properties[name]
        coerced, err = validator.validate_schema(v, s)
        return [nil, err] if err

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

      return [nil, OpenAPIParser::NotExistRequiredKey.new(required_set.to_a, schema.object_reference)] unless required_set.empty?

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

      [value, nil]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openapi_parser-0.1.3 lib/openapi_parser/schema_validators/object_validator.rb