Skip to content

Commit

Permalink
Support a :skip_invalid option in validation_helpers for not adding a…
Browse files Browse the repository at this point in the history
…n error to a column that already has an error

In some cases, it doesn't make sense to add an error if the column
already has an error.
  • Loading branch information
jeremyevans committed Jan 26, 2021
1 parent 54a8500 commit f65f463
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Support a :skip_invalid option in validation_helpers for not adding an error to a column that already has an error (jeremyevans)

* Support :adder, :remover, and :clearer association options that use keyword arguments in Ruby 2.7+ (jeremyevans)

* Make pg_interval use the same number of seconds per year and per month as ActiveSupport::Duration when using ActiveSupport 5.1+ (jeremyevans)
Expand Down
8 changes: 6 additions & 2 deletions lib/sequel/plugins/validation_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Plugins
# :message :: The message to use. Can be a string which is used directly, or a
# proc which is called. If the validation method takes a argument before the array of attributes,
# that argument is passed as an argument to the proc.
# :skip_invalid :: Do not try to validate columns that are already invalid.
#
# The default validation options for all models can be modified by
# overridding the Model#default_validation_helpers_options private method.
Expand Down Expand Up @@ -281,14 +282,17 @@ def default_validation_helpers_options(type)
DEFAULT_OPTIONS[type]
end

# Skip validating any attribute that matches one of the allow_* options.
# Skip validating any attribute that matches one of the allow_* options,
# or already has an error if the skip_invalid option is given.
#
# Otherwise, yield the attribute, value, and passed option :message to
# the block. If the block returns anything except nil or false, add it as
# an error message for that attributes.
def validatable_attributes(atts, opts)
am, an, ab, m = opts.values_at(:allow_missing, :allow_nil, :allow_blank, :message)
am, an, ab, m, si = opts.values_at(:allow_missing, :allow_nil, :allow_blank, :message, :skip_invalid)
from_values = opts[:from] == :values
Array(atts).each do |a|
next if si && errors.on(a)
next if am && !values.has_key?(a)
v = from_values ? values[a] : get_column_value(a)
next if an && v.nil?
Expand Down
12 changes: 12 additions & 0 deletions spec/extensions/validation_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def o.blank?
@m.must_be :valid?
end

it "should take an :skip_invalid option to not validate a column that already has an error" do
@c.set_validations{validates_not_null(:value); validates_not_null(:value, :skip_invalid=>true)}
@m.wont_be :valid?
@m.errors.on(:value).must_equal ['is not present']
end

it "should add validation errors even if columns that already have an error" do
@c.set_validations{validates_not_null(:value); validates_not_null(:value)}
@m.wont_be :valid?
@m.errors.on(:value).must_equal ['is not present', 'is not present']
end

it "should allow a proc for the :message option" do
@c.set_validations{validates_format(/.+_.+/, :value, :message=>proc{|f| "doesn't match #{f.inspect}"})}
@m.value = 'abc_'
Expand Down

0 comments on commit f65f463

Please sign in to comment.