class OpenapiFirst::SchemaValidation

Attributes

raw_schema[R]

Public Class Methods

new(schema, write: true) click to toggle source
# File lib/openapi_first/schema_validation.rb, line 9
def initialize(schema, write: true)
  @raw_schema = schema
  custom_keywords = {}
  custom_keywords['writeOnly'] = proc { |data| !data } unless write
  custom_keywords['readOnly'] = proc { |data| !data } if write
  @schemer = JSONSchemer.schema(
    schema,
    keywords: custom_keywords,
    before_property_validation: proc do |data, property, property_schema, parent|
      convert_nullable(data, property, property_schema, parent)
    end
  )
end

Public Instance Methods

validate(input) click to toggle source
# File lib/openapi_first/schema_validation.rb, line 23
def validate(input)
  @schemer.validate(input)
end

Private Instance Methods

convert_nullable(_data, _property, property_schema, _parent) click to toggle source
# File lib/openapi_first/schema_validation.rb, line 29
def convert_nullable(_data, _property, property_schema, _parent)
  return unless property_schema.is_a?(Hash) && property_schema['nullable'] && property_schema['type']

  property_schema['type'] = [*property_schema['type'], 'null']
  property_schema.delete('nullable')
end