Class: HaveAPI::Validators::Format

Inherits:
HaveAPI::Validator show all
Defined in:
lib/haveapi/validators/format.rb

Overview

Checks that the value is or is not in specified format.

Short form:

string :param, format: /^[a-z0-9]+$/

Full form:

string :param, format: {
  rx: /^[a-z0-9]+$/,
  match: true/false,
  message: 'the error message'
}

Instance Attribute Summary

Attributes inherited from HaveAPI::Validator

#message, #params

Instance Method Summary collapse

Methods inherited from HaveAPI::Validator

#initialize, name, #reconfigure, takes, use, use?, #useful?, #validate

Constructor Details

This class inherits a constructor from HaveAPI::Validator

Instance Method Details

#describeObject



26
27
28
29
30
31
32
33
# File 'lib/haveapi/validators/format.rb', line 26

def describe
  {
    rx: @rx.source,
    match: @match,
    description: @desc,
    message: @message
  }
end

#setupObject



19
20
21
22
23
24
# File 'lib/haveapi/validators/format.rb', line 19

def setup
  @rx = simple? ? take : take(:rx)
  @match = take(:match, true)
  @desc = take(:desc)
  @message = take(:message, @desc || '%{value} is not in a valid format')
end

#valid?(v) ⇒ Boolean

Returns:



35
36
37
38
39
40
41
42
# File 'lib/haveapi/validators/format.rb', line 35

def valid?(v)
  if @match
    @rx.match(v) ? true : false

  else
    @rx.match(v) ? false : true
  end
end