Class: Coradoc::Validation::Rules::Format

Inherits:
Coradoc::Validation::Rule show all
Defined in:
lib/coradoc/validation.rb

Overview

Format validation with regex

Instance Attribute Summary

Attributes inherited from Coradoc::Validation::Rule

#name, #options

Instance Method Summary collapse

Methods inherited from Coradoc::Validation::Rule

#initialize

Constructor Details

This class inherits a constructor from Coradoc::Validation::Rule

Instance Method Details

#validate(element, _context = {}) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/coradoc/validation.rb', line 275

def validate(element, _context = {})
  field = options[:field]
  pattern = options[:pattern]
  value = element.send(field) if element.respond_to?(field)

  return [] if value.nil?

  return ["#{field} has invalid format"] unless pattern.match?(value.to_s)

  []
end