From bb4cac735002686dd66c8dd0abcd1255aee8e725 Mon Sep 17 00:00:00 2001 From: Jordane Lew Date: Sat, 8 Sep 2018 12:45:48 +1200 Subject: [PATCH] Clear errors before validating models --- spec/granite/validations/validator_spec.cr | 12 ++++++++++++ src/granite/validators.cr | 3 +++ 2 files changed, 15 insertions(+) diff --git a/spec/granite/validations/validator_spec.cr b/spec/granite/validations/validator_spec.cr index 573a63d9..f5e7159e 100644 --- a/spec/granite/validations/validator_spec.cr +++ b/spec/granite/validations/validator_spec.cr @@ -58,6 +58,7 @@ require "../../spec_helper" subject.valid?.should eq false end end + describe "validates using block without field" do it "returns true if passwords match" do subject = PasswordTest.new @@ -73,5 +74,16 @@ require "../../spec_helper" subject.valid?.should eq false end end + + describe "validates cleanly after previously failing" do + it "returns true if name is rectified after after failing" do + subject = NameTest.new + subject.name = "" + subject.valid?.should eq false + + subject.name = "name" + subject.valid?.should eq true + end + end end {% end %} diff --git a/src/granite/validators.cr b/src/granite/validators.cr index 059b1f74..4e79134a 100644 --- a/src/granite/validators.cr +++ b/src/granite/validators.cr @@ -43,11 +43,14 @@ module Granite::Validators end def valid? + errors.clear + @@validators.each do |validator| unless validator[:block].call(self) errors << Error.new(validator[:field], validator[:message]) end end + errors.empty? end end