Sha256: b6a0b764567f64b6fe2a32a4a4a2849d97a5da14fca1c0adb9dc20aa8d855f16

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 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,
          # Removing until production data can be remediated and/or additional types can be added to configuration.
          # See also spec/cocina/models/validatable_spec.rb:59
          # DescriptionTypesValidator,
          DescriptionValuesValidator,
          DateTimeValidator
        ].freeze

        def self.validate(clazz, attributes)
          # This gets rid of nested model objects.
          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
          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

1 entries across 1 versions & 1 rubygems

Version Path
cocina-models-0.91.4 lib/cocina/models/validators/validator.rb