mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Exit restriction validation on first restriction error
This commit is contained in:
parent
2244d5e74d
commit
2822e368e3
@ -20,6 +20,8 @@ module ValidatesTimeliness
|
|||||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
|
RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
|
||||||
|
|
||||||
def self.kind
|
def self.kind
|
||||||
:timeliness
|
:timeliness
|
||||||
end
|
end
|
||||||
@ -45,17 +47,22 @@ module ValidatesTimeliness
|
|||||||
value = parse(raw_value) if value.is_a?(String) || options[:format]
|
value = parse(raw_value) if value.is_a?(String) || options[:format]
|
||||||
value = type_cast_value(value, @type)
|
value = type_cast_value(value, @type)
|
||||||
|
|
||||||
return add_error(record, attr_name, :"invalid_#{@type}") if value.blank?
|
add_error(record, attr_name, :"invalid_#{@type}") and return if value.blank?
|
||||||
|
|
||||||
|
validate_restrictions(record, attr_name, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_restrictions(record, attr_name, value)
|
||||||
@restrictions_to_check.each do |restriction|
|
@restrictions_to_check.each do |restriction|
|
||||||
begin
|
begin
|
||||||
restriction_value = type_cast_value(evaluate_option_value(options[restriction], record), @type)
|
restriction_value = type_cast_value(evaluate_option_value(options[restriction], record), @type)
|
||||||
unless value.send(RESTRICTIONS[restriction], restriction_value)
|
unless value.send(RESTRICTIONS[restriction], restriction_value)
|
||||||
return add_error(record, attr_name, restriction, restriction_value)
|
add_error(record, attr_name, restriction, restriction_value) and break
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
unless ValidatesTimeliness.ignore_restriction_errors
|
unless ValidatesTimeliness.ignore_restriction_errors
|
||||||
add_error(record, attr_name, "Error occurred validating #{attr_name} for #{restriction.inspect} restriction:\n#{e.message}")
|
message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ]
|
||||||
|
add_error(record, attr_name, message) and break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -136,7 +136,7 @@ describe ValidatesTimeliness::Validator do
|
|||||||
let(:person) { Person.new(:birth_date => Date.today) }
|
let(:person) { Person.new(:birth_date => Date.today) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Person.validates_time :birth_date, :is_at => lambda { raise }
|
Person.validates_time :birth_date, :is_at => lambda { raise }, :before => lambda { raise }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be added when ignore_restriction_errors is false" do
|
it "should be added when ignore_restriction_errors is false" do
|
||||||
@ -152,6 +152,13 @@ describe ValidatesTimeliness::Validator do
|
|||||||
person.errors[:birth_date].should be_empty
|
person.errors[:birth_date].should be_empty
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should exit on first error' do
|
||||||
|
with_config(:ignore_restriction_errors, false) do
|
||||||
|
person.valid?
|
||||||
|
person.errors[:birth_date].should have(1).items
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#format_error_value" do
|
describe "#format_error_value" do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user