Sha256: 94ee9b6536699c383ef73b6d8ba6d9513a03c3cd60c958a85c7290be239299b3
Contents?: true
Size: 1.18 KB
Versions: 7
Compression:
Stored size: 1.18 KB
Contents
module Semi class ValidationError < RuntimeError; end module_function def validate(value, rules) if rules.class == String rules = rules.split(/,\s?/) end # build a checklist for rules that match 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.to_s) end end failures = tests.each_pair.map { |r,t| r if not t }.compact unless failures.empty? raise ValidationError, "#{value}(#{value.class}) does not validate against #{failures} rules" end end end
Version data entries
7 entries across 7 versions & 1 rubygems