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

message[RW]
params[RW]

Public Class Methods

name(v = nil) click to toggle source

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
new(key, opts) click to toggle source
# File lib/haveapi/validator.rb, line 56
def initialize(key, opts)
  reconfigure(key, opts)
end
takes(*opts) click to toggle source

Specify options this validator takes from the parameter definition.

# File lib/haveapi/validator.rb, line 34
def takes(*opts)
  @takes = opts
end
use(opts) click to toggle source

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
use?(opts) click to toggle source

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

describe() click to toggle source

Return a hash documenting this validator.

# File lib/haveapi/validator.rb, line 78
def describe
  raise NotImplementedError
end
reconfigure(key, opts) click to toggle source
# File lib/haveapi/validator.rb, line 60
def reconfigure(key, opts)
  @key = key
  @opts = opts
  setup
end
setup() click to toggle source

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
useful?() click to toggle source
# File lib/haveapi/validator.rb, line 66
def useful?
  @useful.nil? ? true : @useful
end
valid?(v) click to toggle source

Return true if the value is valid.

# File lib/haveapi/validator.rb, line 83
def valid?(v)
  raise NotImplementedError
end
validate(v, params) click to toggle source

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

opt() click to toggle source

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
simple?() click to toggle source

Returns true if ‘@opts` is not a hash.

# File lib/haveapi/validator.rb, line 127
def simple?
  !@opts.is_a?(::Hash)
end
take(v = nil, default = nil) click to toggle source

This method has three modes of function.

  1. 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.

  2. If ‘v` is not nil and `@opts` is not a hash, it returns `default`

  3. 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
useless() click to toggle source

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