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

Return errors messages without hints #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/reform/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def success?; !failure? end

def errors(*args); filter_for(:errors, *args) end

def messages(*args); filter_for(:messages, *args) end
# using filter_for(:messages, *args) pulls also the hints for DRY validation
alias messages errors

def hints(*args); filter_for(:hints, *args) end

Expand Down Expand Up @@ -57,7 +58,7 @@ def initialize(result, path)

def errors(*args); traverse_for(:errors, *args) end

def messages(*args); traverse_for(:messages, *args) end
alias messages errors

def hints(*args); traverse_for(:hints, *args) end

Expand Down
10 changes: 5 additions & 5 deletions test/validation/dry_validation_new_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ class AlbumForm < TestForm

result.success?.must_equal false

form.errors.messages.must_equal(title: ["must be filled", "size cannot be less than 2"], "artist.email": ["must be filled"], "artist.label.location": ["must be filled"], "songs.title": ["must be filled"])
form.errors.messages.must_equal(title: ["must be filled"], "artist.email": ["must be filled"], "artist.label.location": ["must be filled"], "songs.title": ["must be filled"])
form.artist.errors.messages.must_equal(email: ["must be filled"], "label.location": ["must be filled"])
form.artist.label.errors.messages.must_equal(location: ["must be filled"])
form.songs[0].errors.messages.must_equal({})
form.songs[1].errors.messages.must_equal(title: ["must be filled"])

# #errors[]
form.errors[:nonsense].must_equal []
form.errors[:title].must_equal ["must be filled", "size cannot be less than 2"]
form.errors[:title].must_equal ["must be filled"]
form.artist.errors[:email].must_equal ["must be filled"]
form.artist.label.errors[:location].must_equal ["must be filled"]
form.songs[0].errors[:title].must_equal []
form.songs[1].errors[:title].must_equal ["must be filled"]

# #to_result
form.to_result.errors.must_equal(title: ["must be filled"])
form.to_result.messages.must_equal(title: ["must be filled", "size cannot be less than 2"])
form.to_result.messages.must_equal(title: ["must be filled"])
form.to_result.hints.must_equal(title: ["size cannot be less than 2"])
form.artist.to_result.errors.must_equal(email: ["must be filled"])
form.artist.to_result.messages.must_equal(email: ["must be filled"])
Expand Down Expand Up @@ -280,7 +280,7 @@ class SessionForm < TestForm
# invalid.
it do
form.validate({}).must_equal false
form.errors.messages.must_equal({username: ["must be filled"], email: ["must be filled"], special_class: ["must be filled", "must be ValidationGroupsTest::SomeClass"]})
form.errors.messages.must_equal(username: ["must be filled"], email: ["must be filled"], special_class: ["must be filled"])
end

# partially invalid.
Expand All @@ -293,7 +293,7 @@ class SessionForm < TestForm
it do
form.validate(username: "Helloween", email: "yo!", confirm_password: "9", special_class: SomeClass.new(id: 15)).must_equal false
form.errors.messages.inspect
.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"must be filled\", \"size cannot be less than 2\"]}"
.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"must be filled\"]}"
end
# 4th group with after: fails.
it do
Expand Down
22 changes: 14 additions & 8 deletions test/validation/dry_validation_old_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ class AlbumForm < TestForm
result.success?.must_equal false

# errors.messages
form.errors.messages.must_equal(title: ["must be filled", "size cannot be less than 2"], "artist.email": ["must be filled"], "artist.label.location": ["must be filled"], "songs.title": ["must be filled"])
form.errors.messages.must_equal(title: ["must be filled"], "artist.email": ["must be filled"], "artist.label.location": ["must be filled"], "songs.title": ["must be filled"])
form.artist.errors.messages.must_equal(email: ["must be filled"], "label.location": ["must be filled"])
form.artist.label.errors.messages.must_equal(location: ["must be filled"])
form.songs[0].errors.messages.must_equal({})
form.songs[1].errors.messages.must_equal(title: ["must be filled"])

# #errors[]
form.errors[:nonsense].must_equal []
form.errors[:title].must_equal ["must be filled", "size cannot be less than 2"]
form.errors[:title].must_equal ["must be filled"]
form.artist.errors[:email].must_equal ["must be filled"]
form.artist.label.errors[:location].must_equal ["must be filled"]
form.songs[0].errors[:title].must_equal []
form.songs[1].errors[:title].must_equal ["must be filled"]

# #to_result
form.to_result.errors.must_equal(title: ["must be filled"])
form.to_result.messages.must_equal(title: ["must be filled", "size cannot be less than 2"])
form.to_result.messages.must_equal(title: ["must be filled"])
form.to_result.hints.must_equal(title: ["size cannot be less than 2"])
form.artist.to_result.errors.must_equal(email: ["must be filled"])
form.artist.to_result.messages.must_equal(email: ["must be filled"])
Expand Down Expand Up @@ -272,7 +272,13 @@ class SessionForm < TestForm
# invalid.
it do
form.validate({}).must_equal false
form.errors.messages.must_equal({username: ["must be filled"], email: ["must be filled"], special_class: ["must be filled", "must be ValidationGroupsTest::SomeClass"]})
form.errors.messages.must_equal({username: ["must be filled"], email: ["must be filled"], special_class: ["must be filled"]})
end

# invalid due wrong special class type
it do
form.validate(username: "Helloween", email: "yo", confirm_password: "9", special_class: Session.new).must_equal false
form.errors.messages.must_equal(special_class: ["must be ValidationGroupsTest::SomeClass"])
end

# partially invalid.
Expand All @@ -285,7 +291,7 @@ class SessionForm < TestForm
it do
form.validate(username: "Helloween", email: "yo!", confirm_password: "9", special_class: SomeClass.new(id: 15)).must_equal false
form.errors.messages.inspect
.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"must be filled\", \"size cannot be less than 2\"]}"
.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"must be filled\"]}"
end
# 4th group with after: fails.
it do
Expand Down Expand Up @@ -365,12 +371,12 @@ class Session2Form < TestForm
# invalid.
it do
form.validate({}).must_equal false
form.errors.messages.must_equal(password: ["must be filled", "size cannot be less than 6"], username: ["must be filled"], email: ["must be filled", "you're a bad person"])
form.errors.messages.must_equal(password: ["must be filled"], username: ["must be filled"], email: ["must be filled"])
end

it do
form.validate(email: 1).must_equal false
form.errors.messages.inspect.must_equal "{:password=>[\"must be filled\", \"size cannot be less than 6\"], :username=>[\"must be filled\"], :email=>[\"you're a bad person\"]}"
form.errors.messages.inspect.must_equal "{:password=>[\"must be filled\"], :username=>[\"must be filled\"], :email=>[\"you're a bad person\"]}"
end
end

Expand Down Expand Up @@ -485,7 +491,7 @@ def good_musical_taste?(value)
form.band.errors.full_messages.must_equal ["Name must be filled", "Label Location must be filled"]
form.band.label.errors.full_messages.must_equal ["Location must be filled"]
form.producers.first.errors.full_messages.must_equal ["Name must be filled"]
form.errors.full_messages.must_equal ["Title must be filled", "Title you're a bad person", "Hit Title must be filled", "Songs Title must be filled", "Producers Name must be filled", "Band Name must be filled", "Band Label Location must be filled"]
form.errors.full_messages.must_equal ["Title must be filled", "Hit Title must be filled", "Songs Title must be filled", "Producers Name must be filled", "Band Name must be filled", "Band Label Location must be filled"]
end

describe "only 1 nested validation" do
Expand Down