From 554d45322d058a812f1211e85e34b058817ce6cb Mon Sep 17 00:00:00 2001 From: Emanuele Magliozzi Date: Sat, 10 Aug 2019 12:29:43 +1000 Subject: [PATCH] Return errors messages without hints --- lib/reform/result.rb | 5 +++-- test/validation/dry_validation_test.rb | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/reform/result.rb b/lib/reform/result.rb index e43dedc0..a06f4b55 100644 --- a/lib/reform/result.rb +++ b/lib/reform/result.rb @@ -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 @@ -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 diff --git a/test/validation/dry_validation_test.rb b/test/validation/dry_validation_test.rb index fe6e3eaa..37d39258 100644 --- a/test/validation/dry_validation_test.rb +++ b/test/validation/dry_validation_test.rb @@ -58,7 +58,7 @@ 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({}) @@ -66,7 +66,7 @@ class AlbumForm < TestForm # #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 [] @@ -74,7 +74,7 @@ class AlbumForm < TestForm # #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"]) @@ -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. @@ -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 @@ -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 @@ -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