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

Constructor Details

#initialize(param_description) ⇒ BaseValidator

Returns a new instance of BaseValidator.



11
12
13
# File 'lib/restapi/validator.rb', line 11

def initialize(param_description)
  @param_description = param_description
end

Instance Attribute Details

#param_descriptionObject

Returns the value of attribute param_description.



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

def param_description
  @param_description
end

Class Method Details

.find(param_description, argument, options, block) ⇒ Object

find the right validator for given options



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

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

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  @validators ||= []
  @validators.insert 0, subclass
end

Instance Method Details

#descriptionObject

validator description



45
46
47
# File 'lib/restapi/validator.rb', line 45

def description
  "TODO: validator description"
end

#expected_typeObject

what type is expected, mostly string this information is used in cli client thor supported types — :string, :hash, :array, :numeric, or :boolean



60
61
62
# File 'lib/restapi/validator.rb', line 60

def expected_type
  'string'
end

#param_nameObject



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

def param_name
  @param_description.name
end

#to_jsonObject



53
54
55
# File 'lib/restapi/validator.rb', line 53

def to_json
  self.description
end

#to_sObject



49
50
51
# File 'lib/restapi/validator.rb', line 49

def to_s
  self.description
end

#valid?(value) ⇒ Boolean

check if value is valid

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/restapi/validator.rb', line 30

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