Class: Validator
- Inherits:
-
Object
- Object
- Validator
- Defined in:
- lib/request_validator.rb
Overview
This Validator class holds all the method for our operation
Class Method Summary collapse
-
.configure {|@config| ... } ⇒ Object
Returns new instance of configuration.
Instance Method Summary collapse
- #check(key) ⇒ Object
- #config ⇒ Object
-
#initialize(params = {}) ⇒ Object
constructor
Initialize the Validator.new and sets injected_params to to be remembered thoughout the cause of operation.
- #isArray ⇒ Object
- #isEmail ⇒ Object
- #isIn(arr) ⇒ Object
- #isString ⇒ Object
- #notEmpty ⇒ Object
- #result ⇒ Object
- #validate(object, checks) ⇒ Object
- #validUrl ⇒ Object
- #withMessage(message) ⇒ Object
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
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 |
#config ⇒ Object
38 39 40 |
# File 'lib/request_validator.rb', line 38 def config @config || Validator.configure end |
#isArray ⇒ Object
93 94 95 96 97 |
# File 'lib/request_validator.rb', line 93 def isArray check = @value.kind_of?(Array) @response[:errors] << (@key, "must be an array") if !check self end |
#isEmail ⇒ Object
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] << (@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] << (@key, "is not a valid option") if !check self end |
#isString ⇒ Object
81 82 83 84 85 |
# File 'lib/request_validator.rb', line 81 def isString check = !(@value =~ /[^a-zA-Z0-9]/).nil? @response[:errors] << (@key, "is not a valid string") if check self end |
#notEmpty ⇒ Object
70 71 72 73 |
# File 'lib/request_validator.rb', line 70 def notEmpty @response[:errors] << (@key, "cannot be empty") if @value.length < 1 self end |
#result ⇒ Object
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
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 |
#validUrl ⇒ Object
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] << (@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() = end |