Sha256: 7d8e19aded05b3ac692555835745f0dfb2fc3d1b87dd79fafbac1b745c384234

Contents?: true

Size: 1.63 KB

Versions: 17

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Cocina
  module Models
    module Validators
      # Perform validation against all other Validators
      class Validator
        VALIDATORS = [
          OpenApiValidator,
          DarkValidator,
          PurlValidator,
          CatalogLinksValidator,
          AssociatedNameValidator,
          DescriptionTypesValidator,
          DescriptionValuesValidator,
          DateTimeValidator
        ].freeze

        def self.validate(clazz, attributes)
          # This gets rid of nested model objects.
          # Once DSA is on Rails 6, this can be:
          # attributes_hash = attributes.to_h.deep_transform_values do |value|
          #   value.class.name.starts_with?('Cocina::Models') ? value.to_h : value
          # end.with_indifferent_access
          # And add require 'active_support/core_ext/hash/deep_transform_values' to models file.

          # In the meantime, copying code.
          attributes_hash = deep_transform_values(attributes.to_h) do |value|
            value.class.name.starts_with?('Cocina::Models') ? value.to_h : value
          end.deep_symbolize_keys.with_indifferent_access
          VALIDATORS.each { |validator| validator.validate(clazz, attributes_hash) }
        end

        def self.deep_transform_values(object, &block)
          case object
          when Hash
            object.transform_values { |value| deep_transform_values(value, &block) }
          when Array
            object.map { |e| deep_transform_values(e, &block) }
          else
            yield(object)
          end
        end
        private_class_method :deep_transform_values
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cocina-models-0.91.2 lib/cocina/models/validators/validator.rb
cocina-models-0.91.1 lib/cocina/models/validators/validator.rb
cocina-models-0.91.0 lib/cocina/models/validators/validator.rb
cocina-models-0.90.0 lib/cocina/models/validators/validator.rb
cocina-models-0.89.1 lib/cocina/models/validators/validator.rb
cocina-models-0.89.0 lib/cocina/models/validators/validator.rb
cocina-models-0.88.0 lib/cocina/models/validators/validator.rb
cocina-models-0.87.1 lib/cocina/models/validators/validator.rb
cocina-models-0.87.0 lib/cocina/models/validators/validator.rb
cocina-models-0.86.0 lib/cocina/models/validators/validator.rb
cocina-models-0.85.0 lib/cocina/models/validators/validator.rb
cocina-models-0.84.5 lib/cocina/models/validators/validator.rb
cocina-models-0.84.4 lib/cocina/models/validators/validator.rb
cocina-models-0.84.3 lib/cocina/models/validators/validator.rb
cocina-models-0.84.2 lib/cocina/models/validators/validator.rb
cocina-models-0.84.1 lib/cocina/models/validators/validator.rb
cocina-models-0.84.0 lib/cocina/models/validators/validator.rb