Sha256: aabfd230a2349ea1a66f826ef437dc1295d1b34820fa22ee262cd9a3ae6ceb22

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

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] = Semi::Variables::Integer.validate(value)
      when 'string'
        tests[rule] = Semi::Variables::String.validate(value)
      when 'boolean'
        tests[rule] = Semi::Variables::Boolean.validate(value)
      when 'path'
        tests[rule] = Semi::Variables::Path.validate(value)
      when 'url'
        tests[rule] = Semi::Variables::Url.validate(value)
      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

4 entries across 4 versions & 1 rubygems

Version Path
semi-0.3.3 lib/semi/validate.rb
semi-0.3.2 lib/semi/validate.rb
semi-0.3.1 lib/semi/validate.rb
semi-0.3.0 lib/semi/validate.rb