Class: Restapi::Validator::BaseValidator

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

Overview

to create new validator, inherit from Restapi::Validator::Base and implement class method build and instance method validate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#param_nameObject

Returns the value of attribute param_name.



9
10
11
# File 'lib/restapi/validator.rb', line 9

def param_name
  @param_name
end

Class Method Details

.find(argument, options, block) ⇒ Object

find the right validator for given options



12
13
14
15
16
17
18
# File 'lib/restapi/validator.rb', line 12

def self.find(argument, options, block)
  self.subclasses.each do |validator_type|
    validator = validator_type.build(argument, options, block)
    return validator if validator
  end
  return nil
end

Instance Method Details

#descriptionObject

validator description



32
33
34
# File 'lib/restapi/validator.rb', line 32

def description
  "TODO: validator description"
end

#to_jsonObject



40
41
42
# File 'lib/restapi/validator.rb', line 40

def to_json
  self.description
end

#to_sObject



36
37
38
# File 'lib/restapi/validator.rb', line 36

def to_s
  self.description
end

#valid?(value) ⇒ Boolean

check if value is valid

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/restapi/validator.rb', line 21

def valid?(value)
  if self.validate(value)
    @error_value = nil
    true
  else
    @error_value = value
    false
  end
end