Class: Apes::Validators::ZipCodeValidator

Inherits:
BaseValidator
  • Object
show all
Defined in:
lib/apes/validators.rb

Overview

Validates ZIP codes.

Constant Summary

VALID_REGEX =

The pattern to recognized valid ZIP codes.

/^(\d{5}(-\d{1,4})?)$/

Instance Method Summary (collapse)

Methods inherited from BaseValidator

#validate_each

Constructor Details

- (Apes::Validators::ZipCodeValidator) initialize(options)

Creates a new validator.

Parameters:

  • options (Hash)

    The options for the validations.



167
168
169
# File 'lib/apes/validators.rb', line 167

def initialize(options)
  super(options.reverse_merge(default_message: "must be a valid ZIP code"))
end

Instance Method Details

- (Boolean) check_valid?(value)

Checks if the value is valid for this validator.

Parameters:

  • value (Object)

    The value to validate.

Returns:

  • (Boolean)

    true if the value is valid, false otherwise.



175
176
177
# File 'lib/apes/validators.rb', line 175

def check_valid?(value)
  value.blank? || value =~ VALID_REGEX
end