mirror of
https://github.com/internetee/registry.git
synced 2025-07-22 02:35:57 +02:00
Introduce 'optional' validator to epp controllers
This commit is contained in:
parent
3a529135ea
commit
e6b36c409d
6 changed files with 96 additions and 16 deletions
30
app/validators/date_time_iso8601_validator.rb
Normal file
30
app/validators/date_time_iso8601_validator.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue