module Snapi::Validator

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

Constants

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

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

Note base on lib/shells/gsm.rb

DOMAIN_REGEX

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

HOSTNAME_REGEX

Source: RFC952, RFC1034

INTERFACE_REGEX
IP_V4_REGEX

IPV4 Source: answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/

IP_V6_REGEX

IPV6 Source: gist.github.com/294476

MAC_REGEX

Mac Source: www.ruby-forum.com/topic/139401

NUM_LETTERS_SP_CHARS
ON_OFF_REGEX

SIMPLE ON / OFF (CHECKBOXES)

PORT_REGEX

mmmmmmmm

SIMPLE_COMMAND_REGEX

Source:

SNAPI_FUNCTION_NAME

validate the name for a function name

TRUEFALSE_REGEX

TRUE OR FALSE

URI_REGEX

Public Class Methods

format_types() click to toggle source

A helper to get get the keys off the validation_regex hash

@returns Array of symbols

# File lib/snapi/validator.rb, line 28
def format_types
  validation_regex.keys
end
valid_input?(key,string) click to toggle source

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

@params key, Symbol key which maps to one of the keys in the validation_regex method below @params string, String to check @returns Boolean, true if string checks out

# File lib/snapi/validator.rb, line 14
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) click to toggle source

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

@param format, Symbol to check @returns Boolean, true if the requested format is in format_types array

# File lib/snapi/validator.rb, line 37
def valid_regex_format?(format)
  format_types.include?(format)
end
validation_regex() click to toggle source

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

@returns Hash, dictionary of symbols and Regexp arrays

# File lib/snapi/validator.rb, line 45
def validation_regex
  {
    :address             => [HOSTNAME_REGEX, DOMAIN_REGEX, IP_V4_REGEX, IP_V6_REGEX],
    :anything            => [/.*/],
    :bool                => [TRUEFALSE_REGEX],
    :command             => [SIMPLE_COMMAND_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],
    :json                => [JsonValidator],
    :mac                 => [MAC_REGEX],
    :snapi_function_name => [SNAPI_FUNCTION_NAME],
    :on_off              => [ON_OFF_REGEX],
    :port                => [PORT_REGEX],
    :uri                 => [URI_REGEX],
  }
end

Private Instance Methods

format_types() click to toggle source

A helper to get get the keys off the validation_regex hash

@returns Array of symbols

# File lib/snapi/validator.rb, line 28
def format_types
  validation_regex.keys
end
valid_input?(key,string) click to toggle source

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

@params key, Symbol key which maps to one of the keys in the validation_regex method below @params string, String to check @returns Boolean, true if string checks out

# File lib/snapi/validator.rb, line 14
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) click to toggle source

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

@param format, Symbol to check @returns Boolean, true if the requested format is in format_types array

# File lib/snapi/validator.rb, line 37
def valid_regex_format?(format)
  format_types.include?(format)
end
validation_regex() click to toggle source

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

@returns Hash, dictionary of symbols and Regexp arrays

# File lib/snapi/validator.rb, line 45
def validation_regex
  {
    :address             => [HOSTNAME_REGEX, DOMAIN_REGEX, IP_V4_REGEX, IP_V6_REGEX],
    :anything            => [/.*/],
    :bool                => [TRUEFALSE_REGEX],
    :command             => [SIMPLE_COMMAND_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],
    :json                => [JsonValidator],
    :mac                 => [MAC_REGEX],
    :snapi_function_name => [SNAPI_FUNCTION_NAME],
    :on_off              => [ON_OFF_REGEX],
    :port                => [PORT_REGEX],
    :uri                 => [URI_REGEX],
  }
end