Class: Apipie::Validator::BaseValidator

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

Overview

to create new validator, inherit from Apipie::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.



12
13
14
# File 'lib/apipie/validator.rb', line 12

def initialize(param_description)
  @param_description = param_description
end

Instance Attribute Details

#param_descriptionObject

Returns the value of attribute param_description.



10
11
12
# File 'lib/apipie/validator.rb', line 10

def param_description
  @param_description
end

Class Method Details

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

find the right validator for given options



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

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



16
17
18
19
# File 'lib/apipie/validator.rb', line 16

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

Instance Method Details

#descriptionObject

validator description



46
47
48
# File 'lib/apipie/validator.rb', line 46

def description
  "TODO: validator description"
end

#errorObject



50
51
52
# File 'lib/apipie/validator.rb', line 50

def error
  ParamInvalid.new(param_name, @error_value, 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



65
66
67
# File 'lib/apipie/validator.rb', line 65

def expected_type
  'string'
end

#merge_with(other_validator) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/apipie/validator.rb', line 69

def merge_with(other_validator)
  raise NotImplementedError, "Dont know how to merge #{self.inspect} with #{other_validator.inspect}"
end

#param_nameObject



41
42
43
# File 'lib/apipie/validator.rb', line 41

def param_name
  @param_description.name
end

#params_orderedObject



73
74
75
# File 'lib/apipie/validator.rb', line 73

def params_ordered
  nil
end

#to_jsonObject



58
59
60
# File 'lib/apipie/validator.rb', line 58

def to_json
  self.description
end

#to_sObject



54
55
56
# File 'lib/apipie/validator.rb', line 54

def to_s
  self.description
end

#valid?(value) ⇒ Boolean

check if value is valid

Returns:

  • (Boolean)


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

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