Skip to content

Commit

Permalink
Validate relatedResource structuredValue titles
Browse files Browse the repository at this point in the history
  • Loading branch information
lwrubel committed Jan 29, 2024
1 parent ed9ffe7 commit 32f2602
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/cocina/models/validators/description_values_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ def validate_values_for_multiples(hash, path)
end

def validate_title_type(hash, path)
# only apply to title.structuredValue with value
return unless hash[:value] && path.first == :title && path.include?(:structuredValue)
# only apply to title.structuredValue or relatedResource.title.structuredValue with value
return unless hash[:value] && (path.first == :title || related_resource_title?(path)) && path.include?(:structuredValue)

# if there is a "value" key, make sure there is also a "type" key, only for title.structuredValue
error_paths_missing_title_type << path_to_s(path) unless hash[:type]
end

def related_resource_title?(path)
path.first == :relatedResource && path.third == :title
end

def path_to_s(path)
# This matches the format used by descriptive spreadsheets
path_str = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
relatedResource: [
{
title: [
{ value: 'A related title' }
{ value: 'A related title' },
{ structuredValue: [{ value: 'A related title', type: 'main title' }]}
],
type: 'related to'
}
Expand Down Expand Up @@ -85,6 +86,23 @@
}
end

let(:related_resource_no_title_type_props) do
{
title: [
{ value: 'A title' }
],
purl: 'https://purl.stanford.edu/bc123df4567',
relatedResource: [
{
title: [
{ structuredValue: [{ value: 'A related title'}] }
],
type: 'related to'
}
]
}
end

let(:request_desc_props) do
desc_props.dup.tap do |props|
props.delete(:purl)
Expand Down Expand Up @@ -145,6 +163,16 @@
end.to raise_error(Cocina::Models::ValidationError, 'Missing type for value in description: title1.structuredValue2')
end
end

describe 'when no type for related resource structured value title in description' do
let(:props) { related_resource_no_title_type_props }

it 'is not valid' do
expect do
validate
end.to raise_error(Cocina::Models::ValidationError, 'Missing type for value in description: relatedResource1.title1.structuredValue1')
end
end
end

describe '#meets_preconditions?' do
Expand Down

0 comments on commit 32f2602

Please sign in to comment.