Class: Restapi::Validator::RegexpValidator

Inherits:
BaseValidator show all
Defined in:
lib/restapi/validator.rb

Overview

validate arguments value with regular expression

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

find, #to_json, #to_s, #valid?

Constructor Details

#initialize(argument) ⇒ RegexpValidator

Returns a new instance of RegexpValidator.



79
80
81
# File 'lib/restapi/validator.rb', line 79

def initialize(argument)
  @regexp = argument
end

Class Method Details

.build(argument, options, proc) ⇒ Object



87
88
89
# File 'lib/restapi/validator.rb', line 87

def self.build(argument, options, proc)
  self.new(argument) if argument.is_a? Regexp
end

Instance Method Details

#descriptionObject



95
96
97
# File 'lib/restapi/validator.rb', line 95

def description
  "Parameter has to match regular expression /#{@regexp.source}/."
end

#errorObject



91
92
93
# File 'lib/restapi/validator.rb', line 91

def error
  "Parameter #{@param_name} expecting to match /#{@regexp.source}/, got '#{@error_value}'"
end

#validate(value) ⇒ Object



83
84
85
# File 'lib/restapi/validator.rb', line 83

def validate(value)
  value =~ @regexp
end