Sha256: c54f7aa32431d51bb5fa5cd51284204e3129821bb1a43c49ca23fb18ede7f35f

Contents?: true

Size: 949 Bytes

Versions: 2

Compression:

Stored size: 949 Bytes

Contents

module Semi
  class ValidationError < RuntimeError; end

  module_function
  def validate(value, rules)
    if rules.class == String
      rules = rules.split(/,\s?/)
    end
    tests = Hash[ rules.collect { |r| [r, false] } ]

    tests.keys.each do |rule|
      case rule
    when 'required'
        tests[rule] = true unless value.nil?
      when 'integer'
        tests[rule] = true if value.is_a? Integer
      when 'string'
        tests[rule] = true if value.is_a? String
      when 'boolean'
        tests[rule] = true if value.is_a? Boolean
      end

      # test for regular expression
      if rule.start_with?('/') and rule.end_with?('/')
        re = Regexp.new(rule[1..-2])
        tests[rule] = true if re.match(value)
      end
    end

    failures = tests.each_pair.map { |r,t| r if not t }.compact
    unless failures.empty?
      raise ValidationError, "#{value} does not validate against #{failures} rules"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
semi-0.2.1 lib/semi/validate.rb
semi-0.2.0 lib/semi/validate.rb