Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a validator for was-seed objects #736

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/cocina/models/validators/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Validator
DescriptionValuesValidator,
DateTimeValidator,
LanguageTagValidator,
ReservedFilenameValidator
ReservedFilenameValidator,
WasSeedValidator
].freeze

def self.validate(clazz, attributes)
Expand Down
32 changes: 32 additions & 0 deletions lib/cocina/models/validators/was_seed_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Cocina
module Models
module Validators
# Validates that descriptive has a webarchive url
class WasSeedValidator
def self.validate(_, attributes)
new(_, attributes).validate
end

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

def validate
return unless attributes[:type] == 'https://cocina.sul.stanford.edu/models/webarchive-seed'

archived_website_url = attributes.dig(:description, :access, :url)&.
find { |url| url[:displayLabel] == 'Archived website' }&.fetch(:value)
return if archived_website_url&.include?('/*/')

raise ValidationError, 'Archived website url is not present'
end

private

attr_reader :clazz, :attributes
end
end
end
end
65 changes: 65 additions & 0 deletions spec/cocina/models/validators/was_seed_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Cocina::Models::Validators::WasSeedValidator do
let(:validate) { described_class.validate(Cocina::Models::DRO, props) }

let(:props) do
{
externalIdentifier: 'druid:bc123df4567',
type: Cocina::Models::ObjectType[:'webarchive-seed'],
access: { view: 'public', download: 'none' },
structural: { contains: [ ] },
description:
}
end


context 'when a valid webarchive' do
let(:description) do
{
access: {
url: [
{
displayLabel: 'Archived website',
value: 'https://swap.stanford.edu/was/*/http://markey.house.gov/content/letters-major-data-brokers'
}
]
}
}
end
it 'does not raise' do
expect { validate }.not_to raise_error
end
end

context 'when an malformed url' do
let(:description) do
{
access: {
url: [
{
displayLabel: 'Archived website',
value: 'https://swap.stanford.edu/was//http://markey.house.gov/content/letters-major-data-brokers'
}
]
}
}
end

it 'is not valid' do
expect { validate }.to raise_error(Cocina::Models::ValidationError)
end
end

context 'when an no url' do
let(:description) do
{ }
end

it 'is not valid' do
expect { validate }.to raise_error(Cocina::Models::ValidationError)
end
end
end