Class: NxtSchema::Validators::OptionalNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

register_as, #translate_error

Constructor Details

#initialize(conditional, missing_key) ⇒ OptionalNode

Returns a new instance of OptionalNode.



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

def initialize(conditional, missing_key)
  @conditional = conditional
  @missing_key = missing_key
end

Instance Attribute Details

#conditionalObject (readonly)

Returns the value of attribute conditional.



10
11
12
# File 'lib/nxt_schema/validators/optional_node.rb', line 10

def conditional
  @conditional
end

#missing_keyObject (readonly)

Returns the value of attribute missing_key.



10
11
12
# File 'lib/nxt_schema/validators/optional_node.rb', line 10

def missing_key
  @missing_key
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nxt_schema/validators/optional_node.rb', line 12

def build
  lambda do |node, value|
    args = [node, value]

    return if conditional.call(*args.take(conditional.arity))
    return if node.send(:keys).include?(missing_key.to_sym)

    message = ErrorMessages.resolve(
      node.locale,
      :required_key_missing,
      key: missing_key,
      target: node.input
    )

    node.add_error(message)
  end
end