Revert "Revert "Registry 569""

This reverts commit 4786dbb
This commit is contained in:
Artur Beljajev 2017-10-22 23:57:11 +03:00
parent 0f352cab24
commit d520b5b157
50 changed files with 2462 additions and 323 deletions

View 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

View 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

View 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

View file

@ -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