Sha256: 6a533bf9003ad810459b765d8cc38e6156e360806661f37b204ffd9554041f2d

Contents?: true

Size: 1.09 KB

Versions: 43

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Cocina
  module Models
    module Validators
      # Validates that Purl matches the external identifier (druid)
      class PurlValidator
        def self.validate(clazz, attributes)
          new(clazz, attributes).validate
        end

        def initialize(clazz, attributes)
          @clazz = clazz
          @attributes = attributes
        end

        def validate
          return unless meets_preconditions?

          return if identifier_from_druid == identifier_from_purl

          raise ValidationError, "Purl mismatch: #{druid} purl does not match object druid."
        end

        private

        attr_reader :clazz, :attributes

        def meets_preconditions?
          purl
        end

        def druid
          @druid ||= attributes[:externalIdentifier]
        end

        def purl
          @purl ||= attributes.dig(:description, :purl)
        end

        def identifier_from_druid
          druid.delete_prefix('druid:')
        end

        def identifier_from_purl
          purl.split('/').last
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
cocina-models-0.99.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.99.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.98.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.98.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.96.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.95.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.95.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.94.2 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.94.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.94.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.93.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.93.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.92.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.91.4 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.91.3 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.91.2 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.91.1 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.91.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.90.0 lib/cocina/models/validators/purl_validator.rb
cocina-models-0.89.1 lib/cocina/models/validators/purl_validator.rb