Sha256: 7eed2d01f16b1f7211f37fe4f1ed0f9ee154e030f405c6d8831571cfc782c2aa
Contents?: true
Size: 899 Bytes
Versions: 9
Compression:
Stored size: 899 Bytes
Contents
require 'phony' require 'countries' module ActiveModel module Validations class PhoneValidator < EachValidator def validate_each(record, attribute, value) plausible = case (country = options[:country]) when ->(s) { s.blank? } # Without a country, try to figure out if it sounds like # a plausible phone number. Phony.plausible?(value) else # In the presence of the country option, provide Phony the country # code associated with it. country_code = ISO3166::Country.new(country.to_s.upcase).country_code Phony.plausible?(value, :cc => country_code) end if !plausible record.errors.add(attribute, :invalid) end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems