Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear errors before validating models #279

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions spec/granite/validations/validator_spec.cr
Expand Up @@ -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
Expand All @@ -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 %}
3 changes: 3 additions & 0 deletions src/granite/validators.cr
Expand Up @@ -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