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



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

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



26
27
28
29
# File 'lib/apipie/validator.rb', line 26

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

Instance Method Details

#==(other) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/apipie/validator.rb', line 88

def ==(other)
  return false unless self.class == other.class
  if param_description == other.param_description
    true
  else
    false
  end
end

#descriptionObject

validator description



56
57
58
# File 'lib/apipie/validator.rb', line 56

def description
  "TODO: validator description"
end

#errorObject



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

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



75
76
77
# File 'lib/apipie/validator.rb', line 75

def expected_type
  'string'
end

#inspectObject



20
21
22
23
24
# File 'lib/apipie/validator.rb', line 20

def inspect
  string = "#<#{self.class.name}:#{self.object_id} "
  fields = inspected_fields.map {|field| "#{field}: #{self.send(field)}"}
  string << fields.join(", ") << ">"
end

#inspected_fieldsObject



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

def inspected_fields
  [:param_description]
end

#merge_with(other_validator) ⇒ Object

Raises:

  • (NotImplementedError)


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

def merge_with(other_validator)
  return self if self == other_validator
  raise NotImplementedError, "Don't know how to merge #{self.inspect} with #{other_validator.inspect}"
end

#param_nameObject



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

def param_name
  @param_description.name
end

#params_orderedObject



84
85
86
# File 'lib/apipie/validator.rb', line 84

def params_ordered
  nil
end

#to_jsonObject



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

def to_json
  self.description
end

#to_sObject



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

def to_s
  self.description
end

#valid?(value) ⇒ Boolean

check if value is valid

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/apipie/validator.rb', line 41

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