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.63.1 lib/cocina/models/validator.rb
cocina-models-0.63.0 lib/cocina/models/validator.rb
cocina-models-0.62.1 lib/cocina/models/validator.rb
cocina-models-0.62.0 lib/cocina/models/validator.rb
cocina-models-0.61.3 lib/cocina/models/validator.rb
cocina-models-0.61.2 lib/cocina/models/validator.rb
cocina-models-0.61.1 lib/cocina/models/validator.rb
cocina-models-0.61.0 lib/cocina/models/validator.rb
cocina-models-0.60.0 lib/cocina/models/validator.rb
cocina-models-0.59.1 lib/cocina/models/validator.rb
cocina-models-0.59.0 lib/cocina/models/validator.rb
cocina-models-0.58.2 lib/cocina/models/validator.rb
cocina-models-0.58.1 lib/cocina/models/validator.rb
cocina-models-0.58.0 lib/cocina/models/validator.rb
cocina-models-0.57.0 lib/cocina/models/validator.rb
cocina-models-0.56.1 lib/cocina/models/validator.rb
cocina-models-0.56.0 lib/cocina/models/validator.rb
cocina-models-0.55.0 lib/cocina/models/validator.rb
cocina-models-0.54.0 lib/cocina/models/validator.rb
cocina-models-0.54.0.beta.2 lib/cocina/models/validator.rb