Module: Snapi::Validator

Defined in:
lib/snapi/validator.rb

Overview

Helpful module to check strings against named Regexp patterns which are stored in here.

Constant Summary collapse

ADAPTER_REGEX =

__ _ _ _ _ _ __

| ___ \  ___| | | |  _  | |   |  _  \
| |_/ / |__ | |_| | | | | |   | | | |
| ___ \  __||  _  | | | | |   | | | |
| |_/ / |___| | | \ \_/ / |___| |/ /
\____/\____/\_| |_/\___/\_____/___( )
                                  |/

 _____ _   _  _____  ______  ___ _____ _____ ___________ _   _  _____
|_   _| | | ||  ___| | ___ \/ _ \_   _|_   _|  ___| ___ \ \ | |/  ___|
  | | | |_| || |__   | |_/ / /_\ \| |   | | | |__ | |_/ /  \| |\ `--.
  | | |  _  ||  __|  |  __/|  _  || |   | | |  __||    /| . ` | `--. \
  | | | | | || |___  | |   | | | || |   | | | |___| |\ \| |\  |/\__/ /
  \_/ \_| |_/\____/  \_|   \_| |_/\_/   \_/ \____/\_| \_\_| \_/\____/

Note base on lib/shells/gsm.rb

/^(unlocked_gsm|verizon_virgin_mobile|tmobile)$/
CRON_REGEX =

Note set in lib/shells/scheduler.rb

/^(1_minute|5_minutes|15_minutes|60_minutes)$/
SIMPLE_COMMAND_REGEX =

Source:

/^([a-zA-Z0-9\.\s\-\_\/\\\,\=]+)*$/i
HOSTNAME_REGEX =

Source: RFC952, RFC1034

/^[a-z0-9-]{1,63}$/i
DOMAIN_REGEX =

Source: RFC952, RFC1034, does not take max length into account

/^([a-z0-9-]{1,63}\.)*[a-z0-9-]{1,63}$/i
SNAPI_FUNCTION_NAME =

validate the name for a function name

/[a-z0-9_]/
INTERFACE_REGEX =
/^([a-z]+)+([0-9]+)+$/i
IP_V4_REGEX =
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
IP_V6_REGEX =

IPV6 Source: gist.github.com/294476

/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|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

SIMPLE ON / OFF (CHECKBOXES)

/^(on|off)$/i
STRING_REGEX =

Source:

/^([a-zA-Z0-9\.\-\_]+)*$/i
TRUEFALSE_REGEX =

TRUE OR FALSE

/^(true|false)$/i
PORT_REGEX =

mmmmmmmm

/^(6[0-5][0-5][0-3][0-5]|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[1-9])$/
NUM_LETTERS_SP_CHARS =
/^[\w\[\]\!\@\#\$\%\^\&\*\(\)\{\}\:\;\<\>\+\-]*$/i
URI_REGEX =
/https?:\/\/[\S]+/

Class Method Summary collapse

Class Method Details

.format_typesObject

A helper to get get the keys off the validation_regex hash



27
28
29
# File 'lib/snapi/validator.rb', line 27

def format_types
  validation_regex.keys
end

.valid_input?(key, string) ⇒ Boolean

Core method of the module which attempts to check if a provided string matches any of the regex’s as identified by the key

Returns:

  • (Boolean)

Raises:



13
14
15
16
17
18
19
20
21
22
# File 'lib/snapi/validator.rb', line 13

def valid_input?(key,string)
  raise InvalidFormatError unless valid_regex_format?(key)

  boolarray = validation_regex[key].map do |regxp|
    (string =~ regxp) == 0 ? true : false
  end

  return true if boolarray.include?(true)
  false
end

.valid_regex_format?(format) ⇒ Boolean

A helper to validate that the requested symbols is a valid type of format we can check against

Parameters:

  • format,

    Symbol to check

Returns:

  • (Boolean)


36
37
38
# File 'lib/snapi/validator.rb', line 36

def valid_regex_format?(format)
  format_types.include?(format)
end

.validation_regexObject

A helper dictionary which returns and array of valid Regexp patterns in exchange for a valid key



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/snapi/validator.rb', line 44

def validation_regex
  {
    :address             => [HOSTNAME_REGEX, DOMAIN_REGEX, IP_V4_REGEX, IP_V6_REGEX],
    :anything            => [/.*/],
    :bool                => [TRUEFALSE_REGEX],
    :command             => [SIMPLE_COMMAND_REGEX],
    :cron                => [CRON_REGEX],
    :gsm_adapter         => [ADAPTER_REGEX],
    :hostname            => [HOSTNAME_REGEX],
    :interface           => [INTERFACE_REGEX],
    :ip                  => [IP_V4_REGEX, IP_V6_REGEX],
    :ipv6                => [IP_V6_REGEX],
    :ipv4                => [IP_V4_REGEX],
    :mac                 => [MAC_REGEX],
    :snapi_function_name => [SNAPI_FUNCTION_NAME],
    :string              => [STRING_REGEX],
    :on_off              => [ON_OFF_REGEX],
    :port                => [PORT_REGEX],
    :uri                 => [URI_REGEX],
    :username            => [HOSTNAME_REGEX],
    :password            => [NUM_LETTERS_SP_CHARS]
  }
end