Method: String#validate

Defined in:
lib/kiss/ext/core.rb

#validate(format, options = {}) ⇒ Object

Validates value against specified format. If required is true, value must contain a non-whitespace character. If required is false, value need not match format if and only if value contains only whitespace.



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/kiss/ext/core.rb', line 673

def validate(format, options = {})
  label = options[:label] || 'value'
  if options[:required] && (self.blank?)
    # value required
    raise "#{label} required and missing"
  elsif format && !self.blank?
    format_class = Kiss::Format.lookup(format)
    
    begin
      format_class.validate(self, options[:context] || {})
    rescue Kiss::Format::ValidateError => e
      raise "validation error on #{label}: #{e.message}"
    end
  end
end