Class: Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/request_validator.rb

Overview

This Validator class holds all the method for our operation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Object

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



20
21
22
23
24
25
26
# File 'lib/request_validator.rb', line 20

def initialize(params = {})
    @response = {
        has_errors: false,
        errors: []
    }
    @injected_params = params
end

Class Method Details

.configure {|@config| ... } ⇒ Object

Returns new instance of configuration

Yields:



32
33
34
35
36
# File 'lib/request_validator.rb', line 32

def self.configure
    @config ||= Configuration.new
    yield(@config) if block_given?
    @config
end

Instance Method Details

#check(key) ⇒ Object



60
61
62
63
64
# File 'lib/request_validator.rb', line 60

def check(key)
    @value = @injected_params[key]
    @key = key
    self
end

#configObject



38
39
40
# File 'lib/request_validator.rb', line 38

def config
    @config || Validator.configure
end

#isArrayObject



93
94
95
96
97
# 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

#isEmailObject



75
76
77
78
79
# 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) ⇒ Object



87
88
89
90
91
# 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

#isStringObject



81
82
83
84
85
# 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

#notEmptyObject



70
71
72
73
# File 'lib/request_validator.rb', line 70

def notEmpty
    @response[:errors] << message_composer(@key, "cannot be empty")  if @value.length < 1
    self
end

#resultObject



105
106
107
108
# File 'lib/request_validator.rb', line 105

def result
    @response[:has_errors] = @response[:errors].length > 0 ? true : false
    @response
end

#validate(object, checks) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#validUrlObject



99
100
101
102
103
# 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

#withMessage(message) ⇒ Object



66
67
68
# File 'lib/request_validator.rb', line 66

def withMessage(message)
    @custom_error_message = message
end