Class: OpenAPIParser::SchemaValidator::ArrayValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/openapi_parser/schema_validator/array_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, **_keyword_args) ⇒ Object

Parameters:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/openapi_parser/schema_validator/array_validator.rb', line 5

def coerce_and_validate(value, schema, **_keyword_args)
  return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Array)

  value, err = validate_max_min_items(value, schema)
  return [nil, err] if err

  value, err = validate_unique_items(value, schema)
  return [nil, err] if err

  # array type have an schema in items property
  items_schema = schema.items
  coerced_values = value.map do |v|
    coerced, err = validatable.validate_schema(v, items_schema)
    return [nil, err] if err

    coerced
  end

  value.each_index { |idx| value[idx] = coerced_values[idx] } if @coerce_value

  [value, nil]
end

#validate_max_min_items(value, schema) ⇒ Object



28
29
30
31
32
33
# File 'lib/openapi_parser/schema_validator/array_validator.rb', line 28

def validate_max_min_items(value, schema)
  return [nil, OpenAPIParser::MoreThanMaxItems.new(value, schema.object_reference)] if schema.maxItems && value.length > schema.maxItems
  return [nil, OpenAPIParser::LessThanMinItems.new(value, schema.object_reference)] if schema.minItems && value.length < schema.minItems

  [value, nil]
end

#validate_unique_items(value, schema) ⇒ Object



35
36
37
38
39
# File 'lib/openapi_parser/schema_validator/array_validator.rb', line 35

def validate_unique_items(value, schema)
  return [nil, OpenAPIParser::NotUniqueItems.new(value, schema.object_reference)] if schema.uniqueItems && value.length != value.uniq.length

  [value, nil]
end