Class: NxtSchema::Validators::Equality

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

register_as, #translate_error

Constructor Details

#initialize(expectation) ⇒ Equality

Returns a new instance of Equality.



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

def initialize(expectation)
  @expectation = expectation
end

Instance Attribute Details

#expectationObject (readonly)

Returns the value of attribute expectation.



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

def expectation
  @expectation
end

Instance Method Details

#buildObject

Query for equality validator(:equality, 3) Query for equality validator(:eql, -> { 3 * 3 * 60 })



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

def build
  lambda do |node, value|
    expected_value = Callable.new(expectation, nil, value).call

    if value == expected_value
      true
    else
      node.add_error(
        translate_error(
          node.locale,
          actual: value,
          expected: expected_value
        )
      )
    end
  end
end