Module: OpenapiContracts::Validators::SchemaValidation

Included in:
Parameters, RequestBody, ResponseBody
Defined in:
lib/openapi_contracts/validators/schema_validation.rb

Class Method Summary collapse

Class Method Details

.error_to_message(error) ⇒ Object



5
6
7
8
9
# File 'lib/openapi_contracts/validators/schema_validation.rb', line 5

def error_to_message(error)
  msg = error['error']
  msg.sub!(/^value/, error['data'].to_json) if error['data'].to_json.length < 50
  msg
end

.schema_draft_version(schema) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/openapi_contracts/validators/schema_validation.rb', line 11

def schema_draft_version(schema)
  if schema.openapi_version.blank? || schema.openapi_version < Gem::Version.new('3.1')
    JSONSchemer.openapi30
  else
    JSONSchemer.openapi31
  end
end

.validate_schema(schema, data) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/openapi_contracts/validators/schema_validation.rb', line 28

def validate_schema(schema, data)
  validation_schemer(schema).validate(data).map do |err|
    error_to_message(err)
  end
rescue JSONSchemer::UnknownRef => e
  # This usually happens when discriminators encounter unknown types
  ["Could not resolve pointer #{e.message.inspect}"]
end

.validation_schemer(schema) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/openapi_contracts/validators/schema_validation.rb', line 19

def validation_schemer(schema)
  schemer = JSONSchemer.schema(schema.raw, meta_schema: schema_draft_version(schema))
  if schema.pointer.any?
    schemer.ref(schema.fragment)
  else
    schemer
  end
end