class HaveAPI::Validator
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.
Attributes
Public Class Methods
Set validator name used in API documentation.
# File lib/haveapi/validator.rb, line 24 def name(v = nil) if v @name = v else @name end end
# File lib/haveapi/validator.rb, line 56 def initialize(key, opts) reconfigure(key, opts) end
Specify options this validator takes from the parameter definition.
# File lib/haveapi/validator.rb, line 34 def takes(*opts) @takes = opts end
Use the validator on given set of options in hash ‘opts`. Used options are removed from `opts`.
# 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
True if this validator uses any of options in hash ‘opts`.
# File lib/haveapi/validator.rb, line 39 def use?(opts) opts.keys.intersect?(@takes) end
Public Instance Methods
Return a hash documenting this validator.
# File lib/haveapi/validator.rb, line 78 def describe raise NotImplementedError end
# File lib/haveapi/validator.rb, line 60 def reconfigure(key, opts) @key = key @opts = opts setup end
Validators
should be configured by the given options. This method may be called multiple times, if the validator is reconfigured after it was created.
# File lib/haveapi/validator.rb, line 73 def setup raise NotImplementedError end
# File lib/haveapi/validator.rb, line 66 def useful? @useful.nil? ? true : @useful end
Return true if the value is valid.
# File lib/haveapi/validator.rb, line 83 def valid?(v) raise NotImplementedError end
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.
# File lib/haveapi/validator.rb, line 90 def validate(v, params) @params = params ret = valid?(v) @params = nil ret end
Protected Instance Methods
Returns the name of the option given to this validator. It may bear some meaning to the validator.
# File lib/haveapi/validator.rb, line 133 def opt @key end
Returns true if ‘@opts` is not a hash.
# File lib/haveapi/validator.rb, line 127 def simple? !@opts.is_a?(::Hash) end
This method has three modes of function.
-
If ‘v` is nil, it returns `@opts`. It is used if `@opts` is not a hash but a single value - abbreviation if we’re ok with default settings for given validator.
-
If ‘v` is not nil and `@opts` is not a hash, it returns `default`
-
If ‘v` is not nil and `@opts` is a hash and `@opts` is not nil, it is returned. Otherwise the `default` is returned.
# File lib/haveapi/validator.rb, line 107 def take(v = nil, default = nil) if v.nil? @opts else return default unless @opts.is_a?(::Hash) return @opts[v] unless @opts[v].nil? default end end
Declare validator as useless. Such validator does not do anything and can be removed from validator chain. Validator
can become useless when it’s configuration makes it so.
# File lib/haveapi/validator.rb, line 122 def useless @useful = false end