Class: HaveAPI::Validators::Exclusion

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

Overview

Checks that the value is not reserved.

Short form:

string :param, exclude: %i(one two three)

Full form:

string :param, exclude: {
  values: %i(one two three),
  message: 'the error message'
}

In this case, the value could be anything but ‘one`, `two` or `three`.

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



29
30
31
32
33
34
# File 'lib/haveapi/validators/exclusion.rb', line 29

def describe
  {
    values: @values,
    message: @message
  }
end

#setupObject



21
22
23
24
25
26
27
# File 'lib/haveapi/validators/exclusion.rb', line 21

def setup
  @values = (simple? ? take : take(:values)).map! do |v|
    v.is_a?(::Symbol) ? v.to_s : v
  end

  @message = take(:message, '%{value} cannot be used')
end

#valid?(v) ⇒ Boolean

Returns:



36
37
38
# File 'lib/haveapi/validators/exclusion.rb', line 36

def valid?(v)
  !@values.include?(v)
end