Skip to content

Commit

Permalink
Validate if values are whitespace only
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-collier committed Nov 1, 2023
1 parent d50e32d commit 63c7075
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/cocina/models/validators/description_values_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ def self.validate(clazz, attributes)
def initialize(clazz, attributes)
@clazz = clazz
@attributes = attributes
@error_paths = []
@error_paths_multiple = []
@error_paths_blank = []
end

def validate
return unless meets_preconditions?

validate_obj(attributes, [])

return if error_paths.empty?

raise ValidationError, "Multiple value, groupedValue, structuredValue, and parallelValue in description: #{error_paths.join(', ')}"
raise ValidationError, "Multiple value, groupedValue, structuredValue, and parallelValue in description: #{error_paths_multiple.join(', ')}" unless error_paths_multiple.empty?
raise ValidationError, "Blank value in description: #{error_paths_blank.join(', ')}" unless error_paths_blank.empty?
end

private

attr_reader :clazz, :attributes, :error_paths
attr_reader :clazz, :attributes, :error_paths_blank, :error_paths_multiple

def meets_preconditions?
[Cocina::Models::Description, Cocina::Models::RequestDescription].include?(clazz)
end

def validate_hash(hash, path)
validate_values(hash, path)
validate_values_for_blanks(hash, path)
validate_values_for_multiples(hash, path)
hash.each do |key, obj|
validate_obj(obj, path + [key])
end
Expand All @@ -51,10 +52,16 @@ def validate_obj(obj, path)
validate_array(obj, path) if obj.is_a?(Array)
end

def validate_values(hash, path)
def validate_values_for_blanks(hash, path)
return unless hash[:value] && hash[:value].is_a?(String) && /^\s+$/.match?(hash[:value]) # rubocop:disable Style/SafeNavigation

error_paths_blank << path_to_s(path)
end

def validate_values_for_multiples(hash, path)
return unless hash.count { |key, value| %i[value groupedValue structuredValue parallelValue].include?(key) && value.present? } > 1

error_paths << path_to_s(path)
error_paths_multiple << path_to_s(path)
end

def path_to_s(path)
Expand Down
21 changes: 21 additions & 0 deletions spec/cocina/models/validators/description_values_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@
}
end

let(:blank_title_props) do
{
title: [
{
value: ' ',
parallelValue: [{ value: 'A title' }, { value: 'Another title' }]
}
]
}
end

let(:request_desc_props) do
desc_props.dup.tap do |props|
props.delete(:purl)
Expand Down Expand Up @@ -84,6 +95,16 @@
end.to raise_error(Cocina::Models::ValidationError, 'Multiple value, groupedValue, structuredValue, and parallelValue in description: title1, relatedResource1.title1')
end
end

describe 'when a blank value in description' do
let(:props) { blank_title_props }

it 'is not valid' do
expect do
validate
end.to raise_error(Cocina::Models::ValidationError, 'Blank value in description: title1')
end
end
end

describe '#meets_preconditions?' do
Expand Down

0 comments on commit 63c7075

Please sign in to comment.