Sha256: 493a249bb89fe1e49b004582b0852ba43f8dcc94acdd2b4a5d42f035706ecf7b

Contents?: true

Size: 532 Bytes

Versions: 4

Compression:

Stored size: 532 Bytes

Contents

module OmgValidator
  module Validators
    # Checks whether input is a valid zip code
    #
    # validates :zip_code, zip_code: true
    class ZipCodeValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        return nil if value.nil?
        reg = /^\d{5}(-\d{4})?$/i
        unless reg.match(value)
          record.errors[attribute] = "must be a valid zip code"
        end
      end
    end
  end
end

ActiveModel::Validations::ZipCodeValidator = OmgValidator::Validators::ZipCodeValidator

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
omg_validator-0.0.6.2 lib/omg_validator/validators/zip_code_validator.rb
omg_validator-0.0.6.1 lib/omg_validator/validators/zip_code_validator.rb
omg_validator-0.0.6 lib/omg_validator/validators/zip_code_validator.rb
omg_validator-0.0.5 lib/omg_validator/validators/zip_code_validator.rb