5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-02 07:24:17 +00:00

en lugar de asumir un valor, informar el error

This commit is contained in:
f 2021-05-13 12:52:49 -03:00
parent 8a5e965e61
commit f5834b5c1b

View file

@ -11,6 +11,19 @@ class MetadataDocumentDate < MetadataTemplate
document.date
end
# Siempre es obligatorio
def required
true
end
def validate
super
errors << I18n.t('metadata.date.invalid_format') unless valid_format?
errors.empty?
end
# El valor puede ser un Date, Time o una String en el formato
# "yyyy-mm-dd"
#
@ -24,8 +37,6 @@ class MetadataDocumentDate < MetadataTemplate
case self[:value]
when String
begin
raise Date::Error unless /\A\d{2,4}-\d{1,2}-\d{1,2}\z/ ~= self[:value]
Date.iso8601(self[:value]).to_time
rescue Date::Error
value_from_document || default_value
@ -34,4 +45,13 @@ class MetadataDocumentDate < MetadataTemplate
value_from_document || default_value
end
end
private
def valid_format?
return true if self[:value].is_a?(Time)
@valid_format_re ||= /\A\d{2,4}-\d{1,2}-\d{1,2}\z/
@valid_format_re =~ self[:value].to_s
end
end