Class: Apipie::Validator::BaseValidator

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

Overview

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



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

def initialize(param_description)
  @param_description = param_description
end

Instance Attribute Details

#param_descriptionObject

Returns the value of attribute param_description.



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

def param_description
  @param_description
end

Class Method Details

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

find the right validator for given options



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

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



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

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

.raise_if_missing_params {|missing_params| ... } ⇒ Object

Yields:

  • (missing_params)


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

def self.raise_if_missing_params
  missing_params = []
  yield missing_params
  if missing_params.size > 1
    raise ParamMultipleMissing.new(missing_params)
  elsif missing_params.size == 1
    raise ParamMissing.new(missing_params.first)
  end
end

Instance Method Details

#==(other) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/apipie/validator.rb', line 107

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

#descriptionObject

validator description



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

def description
  "TODO: validator description"
end

#errorObject



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

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



90
91
92
# File 'lib/apipie/validator.rb', line 90

def expected_type
  'string'
end

#format_description_value(value) ⇒ Object



71
72
73
# File 'lib/apipie/validator.rb', line 71

def format_description_value(value)
  "<code>#{CGI::escapeHTML(value.to_s)}</code>"
end

#ignore_allow_blank?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/apipie/validator.rb', line 94

def ignore_allow_blank?
  false
end

#inspectObject



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

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

#inspected_fieldsObject



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

def inspected_fields
  [:param_description]
end

#merge_with(other_validator) ⇒ Object

Raises:

  • (NotImplementedError)


98
99
100
101
# File 'lib/apipie/validator.rb', line 98

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



62
63
64
# File 'lib/apipie/validator.rb', line 62

def param_name
  @param_description.name
end

#params_orderedObject



103
104
105
# File 'lib/apipie/validator.rb', line 103

def params_ordered
  nil
end

#to_jsonObject



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

def to_json
  self.description
end

#to_sObject



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

def to_s
  self.description
end

#valid?(value) ⇒ Boolean

check if value is valid

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/apipie/validator.rb', line 52

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