Skip to content

Commit

Permalink
Return errors messages without hints
Browse files Browse the repository at this point in the history
  • Loading branch information
emaglio committed Aug 11, 2019
1 parent c82adf8 commit 45d712c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/reform/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(result, form)
# PROTOTYPING. THIS WILL GO TO A SEPARATE GEM IN REFORM 2.4/3.0.
DottedErrors = ->(form, prefix, hash) do
result = form.to_result

result.messages.collect { |k, v| hash[[*prefix, k].join(".").to_sym] = v }

form.schema.each(twin: true) do |dfn|
Expand Down
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 @@ -49,7 +50,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
24 changes: 15 additions & 9 deletions test/validation/dry_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ class AlbumForm < TestForm
let(:form) { AlbumForm.new(Album.new(nil, Artist.new(nil, Label.new), [Song.new(nil), Song.new(nil)])) }

it "everything wrong" do
result = form.(title: nil, artist: {email: ""}, songs: [{title: "Clams have feelings too"}, {title: ""}])
result = form.(title: nil, artist: {email: "", age: 17}, songs: [{title: "Clams have feelings too"}, {title: ""}])

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 @@ -275,7 +275,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 @@ -288,7 +294,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 @@ -369,12 +375,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 @@ -490,7 +496,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

0 comments on commit 45d712c

Please sign in to comment.