Class: Subvalid::Validators::FormatValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/subvalid/validators/format_validator.rb

Class Method Summary collapse

Class Method Details

.validate(object, validation_result = ValidationResult.new, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/subvalid/validators/format_validator.rb', line 4

def self.validate(object, validation_result=ValidationResult.new, *args)
  options = args.to_h rescue args
  with = nil
  message = "is invalid"
  case options
  when Regexp
    with = options
  when Hash
    with = options.fetch(:with)
    message = options[:message] || message
  else
    raise "don't know what to do with #{options}"
  end
  validation_result.add_error(message) unless with.match(object)
end