Sha256: a26d6c89fadb6c76dcd59ef68dd081fd663b6b871815766253ee91bd668ad84e

Contents?: true

Size: 936 Bytes

Versions: 24

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

module Cocina
  module Models
    # Perform validation against openapi
    class Validator
      def self.validate(clazz, attributes)
        method_name = clazz.name.split('::').last
        request_operation = root.request_operation(:post, "/validate/#{method_name}")
        # JSON.parse forces serialization of objects like DateTime.
        request_operation.validate_request_body('application/json', JSON.parse(attributes.to_json))
      rescue OpenAPIParser::OpenAPIError => e
        raise ValidationError, e.message
      end

      # rubocop:disable Style/ClassVars
      def self.root
        @@root ||= OpenAPIParser.parse(YAML.load_file(openapi_path))
      end
      # rubocop:enable Style/ClassVars
      private_class_method :root

      def self.openapi_path
        ::File.expand_path('../../../openapi.yml', __dir__)
      end
      private_class_method :openapi_path
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
cocina-models-0.53.1 lib/cocina/models/validator.rb
cocina-models-0.54.0.beta.1 lib/cocina/models/validator.rb
cocina-models-0.53.0 lib/cocina/models/validator.rb
cocina-models-0.52.0 lib/cocina/models/validator.rb