Class: HaveAPI::Validator
- Inherits:
-
Object
- Object
- HaveAPI::Validator
- 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.
Direct Known Subclasses
HaveAPI::Validators::Acceptance, HaveAPI::Validators::Confirmation, HaveAPI::Validators::Custom, HaveAPI::Validators::Exclusion, HaveAPI::Validators::Format, HaveAPI::Validators::Inclusion, HaveAPI::Validators::Length, HaveAPI::Validators::Numericality, HaveAPI::Validators::Presence
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
-
.name(v = nil) ⇒ Object
Set validator name used in API documentation.
-
.takes(*opts) ⇒ Object
Specify options this validator takes from the parameter definition.
-
.use(opts) ⇒ Object
Use the validator on given set of options in hash
opts
. -
.use?(opts) ⇒ Boolean
True if this validator uses any of options in hash
opts
.
Instance Method Summary collapse
-
#describe ⇒ Object
Return a hash documenting this validator.
-
#initialize(key, opts) ⇒ Validator
constructor
A new instance of Validator.
- #reconfigure(key, opts) ⇒ Object
-
#setup ⇒ Object
Validators should be configured by the given options.
- #useful? ⇒ Boolean
-
#valid?(v) ⇒ Boolean
Return true if the value is valid.
-
#validate(v, params) ⇒ Object
Calls method valid?, but before calling it sets instance variable @params.
Constructor Details
#initialize(key, opts) ⇒ Validator
Returns a new instance of Validator.
55 56 57 |
# File 'lib/haveapi/validator.rb', line 55 def initialize(key, opts) reconfigure(key, opts) end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
53 54 55 |
# File 'lib/haveapi/validator.rb', line 53 def @message end |
#params ⇒ Object
Returns the value of attribute params.
53 54 55 |
# File 'lib/haveapi/validator.rb', line 53 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 |
# File 'lib/haveapi/validator.rb', line 45 def use(opts) keys = opts.keys & @takes fail '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
.
39 40 41 |
# File 'lib/haveapi/validator.rb', line 39 def use?(opts) !(opts.keys & @takes).empty? end |
Instance Method Details
#describe ⇒ Object
Return a hash documenting this validator.
77 78 79 |
# File 'lib/haveapi/validator.rb', line 77 def describe raise NotImplementedError end |
#reconfigure(key, opts) ⇒ Object
59 60 61 62 63 |
# File 'lib/haveapi/validator.rb', line 59 def reconfigure(key, opts) @key = key @opts = opts setup end |
#setup ⇒ Object
Validators should be configured by the given options. This method may be called multiple times, if the validator is reconfigured after it was created.
72 73 74 |
# File 'lib/haveapi/validator.rb', line 72 def setup raise NotImplementedError end |
#useful? ⇒ Boolean
65 66 67 |
# File 'lib/haveapi/validator.rb', line 65 def useful? @useful.nil? ? true : @useful end |
#valid?(v) ⇒ Boolean
Return true if the value is valid.
82 83 84 |
# File 'lib/haveapi/validator.rb', line 82 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.
89 90 91 92 93 94 |
# File 'lib/haveapi/validator.rb', line 89 def validate(v, params) @params = params ret = valid?(v) @params = nil ret end |