Class: HaveAPI::Validator

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

Overview

Base class for all validators.

All validators can have a short and a full form. The short form is used when default configuration is sufficient. Custom settings can be set using the full form.

The short form means the validator is configured as ‘<option> => <single value>`. The full form is `<option> => { hash with configuration options }`.

It is up to each validator what exactly the short form means and what options can be set. Specify only those options that you wish to override. The only common option is ‘message` - the error message sent to the client if the provided value did not pass the validator.

The ‘message` can contain `%value`, which is replaced by the actual value that did not pass the validator.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(key, opts)
  reconfigure(key, opts)
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

Class Method Details

.name(v = nil) ⇒ Object

Set validator name used in API documentation.



24
25
26
27
28
29
30
31
# File 'lib/haveapi/validator.rb', line 24

def name(v = nil)
  if v
    @name = v

  else
    @name
  end
end

.takes(*opts) ⇒ Object

Specify options this validator takes from the parameter definition.



34
35
36
# File 'lib/haveapi/validator.rb', line 34

def takes(*opts)
  @takes = opts
end

.use(opts) ⇒ Object

Use the validator on given set of options in hash ‘opts`. Used options are removed from `opts`.



45
46
47
48
49
50
51
# File 'lib/haveapi/validator.rb', line 45

def use(opts)
  keys = opts.keys & @takes

  raise 'too many keys' if keys.size > 1

  new(keys.first, opts.delete(keys.first))
end

.use?(opts) ⇒ Boolean

True if this validator uses any of options in hash ‘opts`.

Returns:



39
40
41
# File 'lib/haveapi/validator.rb', line 39

def use?(opts)
  opts.keys.intersect?(@takes)
end

Instance Method Details

#describeObject

Return a hash documenting this validator.

Raises:

  • (NotImplementedError)


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

def describe
  raise NotImplementedError
end

#reconfigure(key, opts) ⇒ Object



60
61
62
63
64
# File 'lib/haveapi/validator.rb', line 60

def reconfigure(key, opts)
  @key = key
  @opts = opts
  setup
end

#setupObject

Validators should be configured by the given options. This method may be called multiple times, if the validator is reconfigured after it was created.

Raises:

  • (NotImplementedError)


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

def setup
  raise NotImplementedError
end

#useful?Boolean

Returns:



66
67
68
# File 'lib/haveapi/validator.rb', line 66

def useful?
  @useful.nil? ? true : @useful
end

#valid?(v) ⇒ Boolean

Return true if the value is valid.

Returns:

Raises:

  • (NotImplementedError)


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

def valid?(v)
  raise NotImplementedError
end

#validate(v, params) ⇒ Object

Calls method valid?, but before calling it sets instance variable ‘@params`. It contains of hash of all other parameters. The validator may use this information as it will.



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

def validate(v, params)
  @params = params
  ret = valid?(v)
  @params = nil
  ret
end