Class: DataValidator::FormatValidator

Inherits:
BaseValidator show all
Defined in:
lib/validations/format.rb

Instance Attribute Summary

Attributes inherited from BaseValidator

#errors, #name, #options, #value

Instance Method Summary collapse

Methods inherited from BaseValidator

#add_error, #initialize

Constructor Details

This class inherits a constructor from DataValidator::BaseValidator

Instance Method Details

#check_validity!Object



5
6
7
8
9
10
11
# File 'lib/validations/format.rb', line 5

def check_validity!
  unless options.include?(:with) ^ options.include?(:without)  # ^ == xor, or "exclusive or"
    raise ArgumentError, 'Either :with or :without must be supplied (but not both)'
  end
  check_options_validity(options, :with)
  check_options_validity(options, :without)
end

#validateObject



13
14
15
16
17
18
19
# File 'lib/validations/format.rb', line 13

def validate
  if regexp = options[:with]
    add_error :invalid if value.to_s !~ regexp
  elsif regexp = options[:without]
    add_error :invalid if value.to_s =~ regexp
  end
end