Class: PureValidator::Validators::UrlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/pure_validator/validators/url_validator.rb

Constant Summary collapse

URL_REGEXP =
/^(https?:\/\/)?([\w\.-]+)\.([a-z]{2,6}\.?)(\/[\w\.]*)*\/?$/

Class Method Summary collapse

Class Method Details

.validate(value, url_flag) ⇒ Array

Validates that string is a valid url

Parameters:

  • value (String)

    string to validate

  • url (Boolean)

    should given string be url or not

Returns:

  • (Array)

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



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pure_validator/validators/url_validator.rb', line 8

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

  errors = []
  if url_flag
    errors << PureValidator::I18n.t('errors.invalid_url') unless !!URL_REGEXP.match(value)
  else
    errors << PureValidator::I18n.t('errors.can_not_be_url') if !!URL_REGEXP.match(value)
  end

  errors
end

.validate_options(url_flag) ⇒ Object



21
22
23
# File 'lib/pure_validator/validators/url_validator.rb', line 21

def self.validate_options(url_flag)
  PureValidator::ArgsValidator.is_boolean!(url_flag, :validation_rule)
end