Class: HaveAPI::Validators::Inclusion
- Inherits:
-
HaveAPI::Validator
- Object
- HaveAPI::Validator
- HaveAPI::Validators::Inclusion
- Defined in:
- lib/haveapi/validators/inclusion.rb
Overview
Checks that the value is from given set of allowed values.
Short form:
string :param, choices: i(one two three)
Full form:
string :param, choices: {
values: i(one two three),
message: 'the error message'
}
Option choices is an alias to include.
Instance Attribute Summary
Attributes inherited from HaveAPI::Validator
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
#describe ⇒ Object
26 27 28 29 30 31 |
# File 'lib/haveapi/validators/inclusion.rb', line 26 def describe { values: @values, message: , } end |
#setup ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/haveapi/validators/inclusion.rb', line 18 def setup @values = (simple? ? take : take(:values)).map! do |v| v.is_a?(::Symbol) ? v.to_s : v end = take(:message, '%{value} cannot be used') end |
#valid?(v) ⇒ Boolean
33 34 35 36 37 38 39 40 |
# File 'lib/haveapi/validators/inclusion.rb', line 33 def valid?(v) if @values.is_a?(::Hash) @values.has_key?(v) else @values.include?(v) end end |