Class: ApipieDSL::Validator::BaseValidator

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

Overview

To create a new validator, inherit from ApipieDSL::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.



25
26
27
# File 'lib/apipie_dsl/validator.rb', line 25

def initialize(param_description)
  @param_description = param_description
end

Instance Attribute Details

#param_descriptionObject (readonly)

Returns the value of attribute param_description.



23
24
25
# File 'lib/apipie_dsl/validator.rb', line 23

def param_description
  @param_description
end

Class Method Details

.build(_param_description, _argument, _options, &_block) ⇒ Object

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/apipie_dsl/validator.rb', line 29

def self.build(_param_description, _argument, _options, &_block)
  raise NotImplementedError
end

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



56
57
58
59
60
61
62
# File 'lib/apipie_dsl/validator.rb', line 56

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

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  @validators ||= []
  @validators.unshift(subclass)
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
95
96
# File 'lib/apipie_dsl/validator.rb', line 92

def ==(other)
  return false unless self.class == other.class

  param_description == other.param_description
end

#descriptionObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/apipie_dsl/validator.rb', line 37

def description
  raise NotImplementedError
end

#docsObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/apipie_dsl/validator.rb', line 74

def docs
  raise NotImplementedError
end

#expected_typeObject



78
79
80
# File 'lib/apipie_dsl/validator.rb', line 78

def expected_type
  'string'
end

#inspectObject



45
46
47
48
49
# File 'lib/apipie_dsl/validator.rb', line 45

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

#inspected_fieldsObject



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

def inspected_fields
  [:param_description]
end

#merge_with(other_validator) ⇒ Object

Raises:

  • (NotImplementedError)


86
87
88
89
90
# File 'lib/apipie_dsl/validator.rb', line 86

def merge_with(other_validator)
  return self if self == other_validator

  raise NotImplementedError, "Don't know how to merge #{inspect} with #{other_validator.inspect}"
end

#sub_paramsObject



82
83
84
# File 'lib/apipie_dsl/validator.rb', line 82

def sub_params
  nil
end

#to_sObject



70
71
72
# File 'lib/apipie_dsl/validator.rb', line 70

def to_s
  description
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



64
65
66
67
68
# File 'lib/apipie_dsl/validator.rb', line 64

def valid?(value)
  return true if validate(value)

  raise ParamInvalid.new(@param_description.name, value, description)
end

#validate(_value) ⇒ Object

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/apipie_dsl/validator.rb', line 33

def validate(_value)
  raise NotImplementedError
end