Class: NxtSchema::Validators::Included

Inherits:
Validator
  • Object
show all
Defined in:
lib/nxt_schema/validators/included_in.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

register_as, #translate_error

Constructor Details

#initialize(target) ⇒ Included

Returns a new instance of Included.



4
5
6
# File 'lib/nxt_schema/validators/included_in.rb', line 4

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/nxt_schema/validators/included_in.rb', line 9

def target
  @target
end

Instance Method Details

#buildObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/nxt_schema/validators/included_in.rb', line 11

def build
  lambda do |node, value|
    if target.include?(value)
      true
    else
      message = translate_error(node.locale, value: value, target: target)
      node.add_error(message)
    end
  end
end