mirror of
https://github.com/internetee/registry.git
synced 2025-05-28 16:39:55 +02:00
parent
0f352cab24
commit
d520b5b157
50 changed files with 2462 additions and 323 deletions
12
lib/validators/e164_validator.rb
Normal file
12
lib/validators/e164_validator.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
class E164Validator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, _value)
|
||||
length_validator = ActiveModel::Validations::
|
||||
LengthValidator.new(maximum: 17, attributes: attribute)
|
||||
length_validator.validate(record)
|
||||
|
||||
format_validator = ActiveModel::Validations::
|
||||
FormatValidator.new(with: /\+[0-9]{1,3}\.[0-9]{1,14}?/,
|
||||
attributes: attribute)
|
||||
format_validator.validate(record)
|
||||
end
|
||||
end
|
11
lib/validators/iso31661_alpha2_validator.rb
Normal file
11
lib/validators/iso31661_alpha2_validator.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class Iso31661Alpha2Validator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
record.errors.add(attribute, :invalid_iso31661_alpha2) unless valid_country_code?(value)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_country_code?(country_code)
|
||||
Country.new(country_code)
|
||||
end
|
||||
end
|
13
lib/validators/iso8601_validator.rb
Normal file
13
lib/validators/iso8601_validator.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Iso8601Validator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if options[:date_only]
|
||||
record.errors.add(attribute, :invalid_iso8601_date) unless value =~ date_format
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def date_format
|
||||
/\d{4}-\d{2}-\d{2}/
|
||||
end
|
||||
end
|
|
@ -2,11 +2,11 @@ class PhoneValidator < ActiveModel::EachValidator
|
|||
def validate_each(record, attribute, value)
|
||||
return if record.errors[:phone].any?
|
||||
|
||||
splitted_phone = value.split('.')
|
||||
country_code = splitted_phone.first
|
||||
phone_number = splitted_phone.second
|
||||
phone_parts = value.split('.')
|
||||
country_code = phone_parts.first
|
||||
subscriber_no = phone_parts.second
|
||||
|
||||
if zeros_only?(country_code) || zeros_only?(phone_number)
|
||||
if zeros_only?(country_code) || zeros_only?(subscriber_no)
|
||||
record.errors.add(attribute, :invalid)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue