Introduce 'optional' validator to epp controllers

This commit is contained in:
Martin Lensment 2015-01-23 13:08:35 +02:00
parent 3a529135ea
commit e6b36c409d
6 changed files with 96 additions and 16 deletions

View file

@ -0,0 +1,30 @@
class DateTimeIso8601Validator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if self.class.validate(value)
record.errors[attribute] << (options[:message] || I18n.t('unknown_expiry_absolute_pattern'))
end
class << self
def validate(value)
return true if value.empty?
begin
DateTime.parse(value)
rescue => _e
return false
end
true
end
def validate_epp(obj, value)
return if validate(value)
{
code: '2005',
msg: I18n.t(:unknown_expiry_absolute_pattern),
value: { obj: obj, val: value }
}
end
end
end

View file

@ -1,9 +1,30 @@
class DurationIso8601Validator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value.present?
return if self.class.validate(value)
record.errors[attribute] << (options[:message] || record.errors.generate_message(attribute, :unknown_pattern))
end
ISO8601::Duration.new(value)
rescue => _e
record.errors[attribute] << (options[:message] || record.errors.generate_message(attribute, :unknown_pattern))
class << self
def validate(value)
return true if value.empty?
begin
ISO8601::Duration.new(value)
rescue => _e
return false
end
true
end
def validate_epp(obj, value)
return if validate(value)
{
code: '2003',
msg: I18n.t(:unknown_expiry_relative_pattern),
value: { obj: obj, val: value }
}
end
end
end