Class: Cocina::Models::Validators::Validator

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

Overview

Perform validation against all other Validators

Constant Summary collapse

VALIDATORS =
[
  OpenApiValidator,
  DarkValidator,
  PurlValidator,
  CatalogLinksValidator,
  AssociatedNameValidator,
  DescriptionTypesValidator,
  DescriptionValuesValidator,
  DateTimeValidator
].freeze

Class Method Summary collapse

Class Method Details

.validate(clazz, attributes) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cocina/models/validators/validator.rb', line 19

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