Class: AttrValidator::Validators::RegexpValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_validator/validators/regexp_validator.rb

Class Method Summary collapse

Class Method Details

.validate(value, regexp) ⇒ Array

Validates that given value match regexp if regexp validation exists

Parameters:

  • value

    String value to match with regexp

  • regexp (Regexp)

    regexp to match

Returns:

  • (Array)

    empty array if number is valid, array of error messages otherwise



7
8
9
10
11
12
13
14
15
# File 'lib/attr_validator/validators/regexp_validator.rb', line 7

def self.validate(value, regexp)
  return [] if value.nil?

  errors = []
  unless !!regexp.match(value)
    errors << AttrValidator::I18n.t('errors.does_not_match')
  end
  errors
end

.validate_options(regexp) ⇒ Object



17
18
19
# File 'lib/attr_validator/validators/regexp_validator.rb', line 17

def self.validate_options(regexp)
  AttrValidator::ArgsValidator.is_string_or_regexp!(regexp, :validation_rule)
end