Sha256: 82900d101f2247d7c26abf85386b237b6bcd31639f672e3d69b04c4085d92573

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require_relative "../validators"
require "#{Gem::Specification.find_by_name("phonelib").gem_dir}/lib/validators/phone_validator"

module Phonofy
  module Validators
    class ExtendedPhoneValidator < PhoneValidator
      def validate_each(record, attribute, value)
        return if options[:allow_blank] && value.blank?

        @phone = parse(value, specified_country(record))
        valid  = countries.present? ? valid_country? : phone_valid?
        valid  = valid && valid_types? && valid_extensions?

        record.errors.add(attribute, message(error_message), options) unless valid
      end

      private

      def message(custom = :invalid)
        options[:message] || custom
      end

      def error_message
        return :invalid_country if countries.present? && !valid_country?
        return :invalid if !countries.present? && !phone_valid?
        return :invalid_type unless valid_types?
        return :invalid_extension unless valid_extensions?

        :invalid
      end

      def valid_country?
        return true unless options[:countries]

        countries.any? { |country| @phone.valid_for_country?(country) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phonofy-0.1.2 lib/validators/extended_phone_validator.rb
phonofy-0.1.1 lib/validators/extended_phone_validator.rb