module Medivo class ArrayValidator < ActiveModel::Validator def validate(record) for field in options[:fields] value = record.attributes[field] record.errors.add(field, "#{field} must be an array") unless value.is_a? Array end end end class TestTypesValidator < ActiveModel::Validator def validate(record) for field in options[:fields] value = record.attributes[field] valid = value.is_a?(String) and value.split(",").is_a?(Array) rescue false record.errors.add(field, "#{field} must be a comma delimited string of one of more test id's' like so: \"test_type1\" or \"test_type1, test_type2\" ") unless valid end end end # A valid date for the orders is a little different that usual .. so take note '%Y%m%d' # has no slashes or dashes .. so a date is valid that looks like: '20111202' class DateValidator < ActiveModel::Validator def validate(record) for field in options[:fields] value = record.attributes[field] date = Date.strptime(value, '%Y%m%d') rescue nil record.errors.add(field, "invalid #{field}. needs to be in '%Y%m%d' format") unless date end end end end