Class: Cocina::Models::Validators::OpenApiValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina/models/validators/open_api_validator.rb

Overview

Perform validation against openapi

Class Method Summary collapse

Class Method Details

.validate(clazz, attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cocina/models/validators/open_api_validator.rb', line 8

def self.validate(clazz, attributes)
  return unless clazz.name

  method_name = clazz.name.split('::').last
  request_operation = root.request_operation(:post, "/validate/#{method_name}")

  # JSON.parse forces serialization of objects like DateTime.
  json_attributes = JSON.parse(attributes.to_json)
  # Inject cocinaVersion if needed and not present.
  if operation_has_cocina_version?(request_operation) && !json_attributes.include?('cocinaVersion')
    json_attributes['cocinaVersion'] = Cocina::Models::VERSION
  end

  request_operation.validate_request_body('application/json', json_attributes)
rescue OpenAPIParser::OpenAPIError => e
  raise ValidationError, e.message
end