Module: AcroForge::Validator

Defined in:
lib/acroforge/validator.rb

Class Method Summary collapse

Class Method Details

.valid?(value, type, options = []) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/acroforge/validator.rb', line 10

def self.valid?(value, type, options = [])
  return true if value.nil? || value.to_s.empty?

  case type
  when :money
    value.to_s.gsub(/[$,]/, "").match?(/^\d+(\.\d+)?$/)
  when :date
    begin
      Date.parse(value.to_s)
      true
    rescue ArgumentError, TypeError
      false
    end
  when :email
    value.to_s.match?(URI::MailTo::EMAIL_REGEXP)
  when :number
    value.to_s.gsub(/[\s-]/, "").match?(/^\d+$/)
  when :boolean
    ["true", "false", "yes", "no", "1", "0", "on", "off"].include?(value.to_s.downcase)
  when :select
    val_str = value.to_s.downcase
    options.any? { |o| o.to_s.downcase == val_str }
  else
    true
  end
end