
autovalidation for validates_length doesn't respect default error messages
Reported by ojak | May 16th, 2009 @ 01:51 AM | in 0.10.0
Expected behavior
- When using auto-validations or manual validations, calling Model.valid? should return Model.errors with {:key=>["error_message"]} for properties that fail validation.
- Model.errors should not contain any nil values.
Actual Behavior (as of 0.9.11 gem)
- Auto-validations perform as expected
- Manual validations return {:key=>[nil]} for failed validations.
Model
class Contact
...
property :zip, String
validates_length :zip, :length => (5..10)
end
IRB test
irb> @contact = Contact.new
irb> @contact.valid?
irb> @contact.errors
#<DataMapper::Validate::ValidationErrors:0x2569c9c @errors={:zip=>[nil]}
Comments and changes to this ticket
-
Martin Gamsjaeger (snusnu) July 9th, 2009 @ 05:41 PM
- Assigned user set to Martin Gamsjaeger (snusnu)
- State changed from unconfirmed to accepted
- Milestone set to 0.10.0
As can be seen below this is still an issue in the current next branch.
require "rubygems" require "dm-core" require "dm-validations" require "spec" DataMapper::Logger.new(STDOUT, :debug) DataMapper.setup(:default, "sqlite3::memory:") class Contact include DataMapper::Resource property :id, Serial property :zip, String validates_length :zip, :length => (5..10) end describe "manual validations" do before(:all) do DataMapper.auto_migrate! end it "should return :key => ['error message'] instead of :key => [nil] when validation fails" do contact = Contact.new contact.valid? contact.errors.on(:zip).should_not == [ nil ] end end # mungo:snippets snusnu$ spec -cfs 853.rb # # manual validations # ~ (0.000143) SELECT sqlite_version(*) # ~ (0.000110) DROP TABLE IF EXISTS "contacts" # ~ (0.000021) PRAGMA table_info("contacts") # ~ (0.000394) CREATE TABLE "contacts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "zip" VARCHAR(50)) # - should return :key => ['error message'] instead of :key => [nil] when validation fails (FAILED - 1) # # 1) # 'manual validations should return :key => ['error message'] instead of :key => [nil] when validation fails' FAILED # expected not: == [nil], # got: [nil] # ./853.rb:23: # # Finished in 0.005831 seconds # # 1 example, 1 failure
-
Martin Gamsjaeger (snusnu) July 11th, 2009 @ 06:47 PM
I had a look into this and actually :length is no valid option for validates_length. You can see a list of valid options at
http://github.com/datamapper/dm-more/blob/8cc9e7b586736cbe0aebd4035...
However, if I'm not mistaken, then actually auto_validations misbehave, so it's actually the other way round. Have a look at the spec below.
require "rubygems" require "dm-core" require "dm-validations" require "spec" DataMapper::Logger.new(STDOUT, :debug) DataMapper.setup(:default, "sqlite3::memory:") class Contact include DataMapper::Resource property :id, Serial property :zip, String property :address, String, :length => (1..100) # test auto validation validates_length :zip, :in => (5..10) # test manual validation end describe "auto validations" do before(:all) do DataMapper.auto_migrate! end it "should return :key => ['error message'] when validation fails" do contact = Contact.new contact.valid? contact.errors.on(:address).respond_to?(:each).should be_true # FAILS contact.errors.on(:address).should_not be_empty contact.errors.on(:address).should_not == [ nil ] contact.errors.on(:address).should == [ "Address must be between 1 and 100 characters long" ] end end describe "manual validations" do before(:all) do DataMapper.auto_migrate! end it "should return :key => ['error message'] when validation fails" do contact = Contact.new contact.valid? contact.errors.on(:zip).should_not be_empty contact.errors.on(:zip).should_not == [ nil ] contact.errors.on(:zip).should == [ "Zip must be between 5 and 10 characters long" ] end end # mungo:snippets snusnu$ spec -cfs 853.rb # # auto validations # ~ (0.000153) SELECT sqlite_version(*) # ~ (0.000111) DROP TABLE IF EXISTS "contacts" # ~ (0.000022) PRAGMA table_info("contacts") # ~ (0.000390) CREATE TABLE "contacts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "zip" VARCHAR(50), "address" VARCHAR(100)) # - should return :key => ['error message'] when validation fails (FAILED - 1) # # manual validations # ~ (0.000146) DROP TABLE IF EXISTS "contacts" # ~ (0.000010) PRAGMA table_info("contacts") # ~ (0.000163) CREATE TABLE "contacts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "zip" VARCHAR(50), "address" VARCHAR(100)) # - should return :key => ['error message'] when validation fails # # 1) # 'auto validations should return :key => ['error message'] when validation fails' FAILED # expected true, got false # ./853.rb:24: # # Finished in 0.008026 seconds # # 2 examples, 1 failure
-
Martin Gamsjaeger (snusnu) July 11th, 2009 @ 07:04 PM
- State changed from accepted to not-applicable
I added #968 as a follow up to this ticket and closed this one.
-
Martin Gamsjaeger (snusnu) July 12th, 2009 @ 12:54 AM
- State changed from not-applicable to accepted
- Tag set to autovalidation, default, doesn't, error, for, messages, respect, validates_length
#968 is not applicable, but I reopened this ticket with a new title. The real problem seems to be what's described in my comments to this ticket.
-
Martin Gamsjaeger (snusnu) July 12th, 2009 @ 12:56 AM
- Tag cleared.
- Title changed from Manual validations returning :key=>[nil] errors. to autovalidation for validates_length doesn't respect default error messages
Sorry I should go to bed, entered the title into the tags textfield ...
-
Martin Gamsjaeger (snusnu) July 12th, 2009 @ 11:18 AM
- State changed from accepted to not-applicable
Obviously I really got confused about this whole issue yesterday. Actually, DM behaves totally right! To prove the invalid assumption I made (for the changed ticket title) here is a small spec that shows that DM does the right thing.
I'm marking this as not applicable.
require "rubygems" require "dm-core" require "dm-validations" require "spec" DataMapper::Logger.new(STDOUT, :debug) DataMapper.setup(:default, "sqlite3::memory:") class Contact include DataMapper::Resource property :id, Serial property :address, String, :length => (1..100) # test auto validation end describe "auto validations" do before(:all) do DataMapper.auto_migrate! end it "should return :key => ['error message'] when validation fails" do contact = Contact.new contact.address = '' contact.should_not be_valid contact.errors.on(:address).respond_to?(:each).should be_true contact.errors.on(:address).should_not be_empty contact.errors.on(:address).should_not == [ nil ] end end # mungo:snippets snusnu$ spec -cfs 853.rb # # auto validations # ~ (0.000153) SELECT sqlite_version(*) # ~ (0.000111) DROP TABLE IF EXISTS "contacts" # ~ (0.000022) PRAGMA table_info("contacts") # ~ (0.000366) CREATE TABLE "contacts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "address" VARCHAR(100) NOT NULL) # - should return :key => ['error message'] when validation fails # # Finished in 0.00581 seconds # # 1 example, 0 failures
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
Referenced by
-
968 [dm-validations] autovalidations don't respect default error message This is a follow up to #853 which is not applicable itsel...