lib/compel/validation.rb in compel-0.1.3 vs lib/compel/validation.rb in compel-0.2.0

- old
+ new

@@ -1,65 +1,51 @@ module Compel module Validation - def valid?(value, options) - validate(value, options).length == 0 - end - - def validate!(value, options) - errors = validate(value, options) - - if errors.length > 0 - raise ParamValidationError, errors[0] - end - end - - def validate(value, options) + def validate(value, type, options) errors = [] options.each do |option, option_value| # most of this code snippet is from sinatra-param gem # https://github.com/mattt/sinatra-param # by Mattt Thompson (@mattt) - begin - case option.to_sym - when :required - raise ParamValidationError, 'is required' if option_value && value.nil? - when :blank - raise ParamValidationError, 'cannot be blank' if !option_value && case value - when String - !(/\S/ === value) - when Array, Hash - value.empty? - else - value.nil? - end - when :format - raise ParamValidationError, 'must be a string if using the format validation' unless value.kind_of?(String) - raise ParamValidationError, "must match format #{option_value}" unless value =~ option_value - when :is - raise ParamValidationError, "must be #{option_value}" unless value === option_value - when :in, :within, :range - raise ParamValidationError, "must be within #{option_value}" unless value.nil? || case option_value - when Range - option_value.include?(value) - else - Array(option_value).include?(value) - end - when :min - raise ParamValidationError, "cannot be less than #{option_value}" unless value.nil? || option_value <= value - when :max - raise ParamValidationError, "cannot be greater than #{option_value}" unless value.nil? || option_value >= value - when :length - raise ParamValidationError, "cannot have length different than #{option_value}" unless value.nil? || option_value == "#{value}".length - when :min_length - raise ParamValidationError, "cannot have length less than #{option_value}" unless value.nil? || option_value <= value.length - when :max_length - raise ParamValidationError, "cannot have length greater than #{option_value}" unless value.nil? || option_value >= value.length + case option.to_sym + when :required + errors << 'is required' if option_value && value.nil? + when :format + if type == Coercion::String && !value.nil? + errors << "must match format #{option_value.source}" unless value =~ option_value + else + errors << 'must be a string if using the format validation' end - rescue ParamValidationError => exception - errors << exception.message + when :is + errors << "must be #{option_value}" unless value === option_value + when :in, :within, :range + errors << "must be within #{option_value}" unless value.nil? || case option_value + when Range + option_value.include?(value) + else + Array(option_value).include?(value) + end + when :min + errors << "cannot be less than #{option_value}" unless value.nil? || option_value <= value + when :max + errors << "cannot be greater than #{option_value}" unless value.nil? || option_value >= value + when :length + errors << "cannot have length different than #{option_value}" unless value.nil? || option_value == "#{value}".length + when :min_length + unless value.kind_of?(String) + errors << 'must be a string if using the min_length validation' + else + errors << "cannot have length less than #{option_value}" unless value.nil? || option_value <= value.length + end + when :max_length + unless value.kind_of?(String) + errors << 'must be a string if using the max_length validation' + else + errors << "cannot have length greater than #{option_value}" unless value.nil? || option_value >= value.length + end end end errors end