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_description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

#expected_type, find, inherited, #param_name, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ RegexpValidator

Returns a new instance of RegexpValidator.



108
109
110
111
# File 'lib/restapi/validator.rb', line 108

def initialize(param_description, argument)
  super(param_description)
  @regexp = argument
end

Class Method Details

.build(param_description, argument, options, proc) ⇒ Object



117
118
119
# File 'lib/restapi/validator.rb', line 117

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

Instance Method Details

#descriptionObject



125
126
127
# File 'lib/restapi/validator.rb', line 125

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

#errorObject



121
122
123
# File 'lib/restapi/validator.rb', line 121

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

#validate(value) ⇒ Object



113
114
115
# File 'lib/restapi/validator.rb', line 113

def validate(value)
  value =~ @regexp
end