class Validator

This Validator class holds all the method for our operation

Public Class Methods

configure() { |config| ... } click to toggle source

Returns new instance of configuration @return [Object] instance of class Configuration

# File lib/request_validator.rb, line 32
def self.configure
    @config ||= Configuration.new
    yield(@config) if block_given?
    @config
end
new(params = {}) click to toggle source

Initialize the Validator.new and sets injected_params to to be remembered thoughout the cause of operation

@param params [Hash] value to be remembered @return [Object] instance of class Validator

# File lib/request_validator.rb, line 20
def initialize(params = {})
    @response = {
        has_errors: false,
        errors: []
    }
    @injected_params = params
end

Public Instance Methods

check(key) click to toggle source
# File lib/request_validator.rb, line 60
def check(key)
    @value = @injected_params[key]
    @key = key
    self
end
config() click to toggle source
# File lib/request_validator.rb, line 38
def config
    @config || Validator.configure
end
isArray() click to toggle source
# File lib/request_validator.rb, line 93
def isArray
    check = @value.kind_of?(Array)
    @response[:errors] << message_composer(@key, "must be an array")  if !check
    self
end
isEmail() click to toggle source
# File lib/request_validator.rb, line 75
def isEmail
    check = (@value =~ /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/).nil?
    @response[:errors] << message_composer(@key, "is not a valid email address")  if check
    self
end
isIn(arr) click to toggle source
# File lib/request_validator.rb, line 87
def isIn(arr)
    check = arr.include? @value
    @response[:errors] << message_composer(@key, "is not a valid option")  if !check
    self
end
isString() click to toggle source
# File lib/request_validator.rb, line 81
def isString
    check = !(@value =~ /[^a-zA-Z0-9]/).nil?
    @response[:errors] << message_composer(@key, "is not a valid string")  if check
    self
end
notEmpty() click to toggle source
# File lib/request_validator.rb, line 70
def notEmpty
    @response[:errors] << message_composer(@key, "cannot be empty")  if @value.length < 1
    self
end
result() click to toggle source
# File lib/request_validator.rb, line 105
def result
    @response[:has_errors] = @response[:errors].length > 0 ? true : false
    @response
end
validUrl() click to toggle source
# File lib/request_validator.rb, line 99
def validUrl
    check = (@value =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix).nil?
    @response[:errors] << message_composer(@key, "is not a valid url")  if check
    self
end
validate(object, checks) click to toggle source
# File lib/request_validator.rb, line 42
def validate(object, checks)
    raise ArgumentError, 'Object cannnot be empty' unless !object.empty? || config.skip_main_object_empty
    checks.each_pair do |key, againsts|
        res = @@functions_hash[againsts]
        if res.nil?
            next if config.error_mode == 'surpressed'
            raise ArgumentError, "Invalid identifier - #{againsts} - Corresponding method not found"
        end
        res = res.call(object[key], key)
        if !res[:valid]
            p res[:error] 
            @response[:errors] << res[:error] 
        end
    end
    @response[:has_errors] = @response[:errors].length > 0 ? true : false
    @response
end
withMessage(message) click to toggle source
# File lib/request_validator.rb, line 66
def withMessage(message)
    @custom_error_message = message
end

Private Instance Methods

message_composer(key, default_msg) click to toggle source
# File lib/request_validator.rb, line 154
def message_composer(key, default_msg)
    if @custom_error_message
        "#{key} #{@custom_error_message}"
    else
        "#{key} #{default_msg}"
    end
end