lib/sequel/plugins/validation_helpers.rb in sequel-4.48.0 vs lib/sequel/plugins/validation_helpers.rb in sequel-4.49.0

- old
+ new

@@ -88,10 +88,12 @@ :operator=>{:message=>lambda{|operator, rhs| "is not #{operator} #{rhs}"}}, :type=>{:message=>lambda{|klass| klass.is_a?(Array) ? "is not a valid #{klass.join(" or ").downcase}" : "is not a valid #{klass.to_s.downcase}"}}, :presence=>{:message=>lambda{"is not present"}}, :unique=>{:message=>lambda{'is already taken'}} } + DEFAULT__OPTIONS = DEFAULT_OPTIONS + Sequel::Deprecation.deprecate_constant(self, :DEFAULT_OPTIONS) module InstanceMethods # Check that the attribute values are the given exact length. def validates_exact_length(exact, atts, opts=OPTS) validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) if v.nil? || v.length != exact} @@ -127,11 +129,17 @@ # Check that the attribute values are not longer than the given max length. # # Accepts a :nil_message option that is the error message to use when the # value is nil instead of being too long. def validates_max_length(max, atts, opts=OPTS) - validatable_attributes_for_type(:max_length, atts, opts){|a,v,m| v ? validation_error_message(m, max) : validation_error_message(opts[:nil_message] || DEFAULT_OPTIONS[:max_length][:nil_message]) if v.nil? || v.length > max} + validatable_attributes_for_type(:max_length, atts, opts) do |a,v,m| + if v.nil? + validation_error_message(opts[:nil_message] || default_validation_helpers_options(:max_length)[:nil_message]) + elsif v.length > max + validation_error_message(m, max) + end + end end # Check that the attribute values are not shorter than the given min length. def validates_min_length(min, atts, opts=OPTS) validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) if v.nil? || v.length < min} @@ -266,10 +274,10 @@ # The default options hash for the given type of validation. Can # be overridden on a per-model basis for different per model defaults. # The hash return must include a :message option that is either a # proc or string. def default_validation_helpers_options(type) - DEFAULT_OPTIONS[type] + DEFAULT__OPTIONS[type] end # Skip validating any attribute that matches one of the allow_* options. # Otherwise, yield the attribute, value, and passed option :message to # the block. If the block returns anything except nil or false, add it as