Module: PaynetEasy::PaynetEasyApi::Util::Validator

Includes:
Error
Defined in:
lib/paynet_easy/paynet_easy_api/util/validator.rb

Constant Summary collapse

EMAIL =

Validate value as email

'email'
IP =

Validate value as IP address

'ip'
URL =

Validate value as URL

'url'
MONTH =

Validate value as month

'month'
YEAR =

Validate value as year

'year'
PHONE =

Validate value as phone number

'phone'
AMOUNT =

Validate value as payment amount

'amount'
CURRENCY =

Validate value as currency

'currency'
CVV2 =

Validate value as card verification value

'cvv2'
ZIP_CODE =

Validate value as zip code

'zip_code'
COUNTRY =

Validate value as two-letter country or state code

'country'
DATE =

Validate value as date in format MMDDYY

'date'
SSN =

Validate value as last four digits of social security number

'ssn'
CREDIT_CARD_NUMBER =

Validate value as credit card number

'credit_card_number'
ID =

Validate value as different IDs (client, paynet, card-ref)

'id'
MEDIUM_STRING =

Validate value as medium string

'medium_string'
LONG_STRING =

Validate value as long string

'long_string'
@@rule_regexps =

Regular expressions for some validation rules

{
    EMAIL                 => /@+/,
    PHONE                 => /^[0-9\-\+\(\)\s]{6,15}$/i,
    AMOUNT                => /^[0-9\.]{1,11}$/i,
    CURRENCY              => /^[A-Z]{1,3}$/i,
    CVV2                  => /^[\S\s]{3,4}$/i,
    ZIP_CODE              => /^[\S\s]{1,10}$/i,
    COUNTRY               => /^[A-Z]{1,2}$/i,
    YEAR                  => /^[0-9]{1,2}$/i,
    DATE                  => /^[0-9]{6}$/i,
    SSN                   => /^[0-9]{1,4}$/i,
    CREDIT_CARD_NUMBER    => /^[0-9]{1,20}$/i,
    ID                    => /^[\S\s]{1,20}$/i,
    MEDIUM_STRING         => /^[\S\s]{1,50}$/i,
    LONG_STRING           => /^[\S\s]{1,128}$/i
}

Class Method Summary collapse

Class Method Details

.validate_by_rule(value, rule, fail_on_error = true) ⇒ TrueClass|FalseClass

Validates value by given rule. Rule can be one of Validator constants or regExp.

Parameters:

  • value (Object)

    Value for validation

  • rule (String|Regexp)

    Rule for validation

  • fail_on_error (TrueClass|FalseClass) (defaults to: true)

    Throw exception on invalid value or not

Returns:

  • (TrueClass|FalseClass)

    Validation result



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/paynet_easy/paynet_easy_api/util/validator.rb', line 87

def self.validate_by_rule(value, rule, fail_on_error = true)
  begin
    valid = case rule
    when URL    then URI::ABS_URI === value
    when IP     then IPAddr.new value
    when MONTH  then (1..12).include? value.to_i
    else
      regexp = @@rule_regexps.key?(rule) ? @@rule_regexps[rule] : rule
      regexp === value.to_s
    end
  rescue Exception
    valid = false
  end

  if valid
    true
  elsif fail_on_error
    raise ValidationError, "Value '#{value}' does not match rule '#{rule}'"
  else
    false
  end
end