lib/cocina/models/validators/description_values_validator.rb in cocina-models-0.92.0 vs lib/cocina/models/validators/description_values_validator.rb in cocina-models-0.93.0
- old
+ new
@@ -10,33 +10,34 @@
end
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
end
@@ -49,13 +50,19 @@
def validate_obj(obj, path)
validate_hash(obj, path) if obj.is_a?(Hash)
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) && /\A\s+\z/.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)
# This matches the format used by descriptive spreadsheets
path_str = ''