Class: NxtSchema::Validators::Pattern

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

register_as, #translate_error

Constructor Details

#initialize(pattern) ⇒ Pattern

Returns a new instance of Pattern.



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

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

Instance Method Details

#buildObject



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

def build
  lambda do |node, value|
    if value.match(pattern)
      true
    else
      message = translate_error(node.locale, value: value, pattern: pattern)
      node.add_error(message)
      false
    end
  end
end