basic validations and .valid?
Reported by hkstar | November 13th, 2008 @ 06:27 PM
I would love to see some (super-basic) validations support to replace the hacks I've come up with for the moment.
What I am doing now is just making an array of fields I wish to be present, then on before(:create) doing a check like this:
def validate_essentials check = ['name', 'email', 'timezone', 'password'] check.clone.each do |c|
check.delete_if {|x| x == c } if !self[c].blank?
end if check.blank?
return true
else
raise RuntimeError, check.first
end end
That gives me basic validations and a way to pass the missing field around, but as you can see it's not very elegant. It would be nice to be able to set some "required fields" once per class, then have a .valid? method to see if they were all satisfied, hooked into the official save method.
What do you think? Outside the scope of CouchRest? Or useful? : )
thanks for your excellent library btw, it's a pleasure to work with.
Comments and changes to this ticket
-
hkstar November 13th, 2008 @ 06:30 PM
sorry, should have checked formatting before posting. Not that the code was particularly valuable, but still ..
Let me give it another shot:
@@@def validate_essentials check = ['name', 'email', 'timezone', 'password'] check.clone.each do |c|
check.delete_if {|x| x == c } if !self[c].blank?
end if check.blank?
return true
else
raise RuntimeError, check.first
end end@@@
-
hkstar November 13th, 2008 @ 06:35 PM
So it seems the code formatting just does not work at all. Cool.
-
hkstar November 17th, 2008 @ 12:00 PM
Here's a patch for a super-simple implementation of this. All it does is define a number of fields you want to check, eg.
validate :name, :email
And then refuse to save (returns false) if they're not there. Also gives you .valid? and an .invalid_fields method.
http://friendpaste.com/GWTHxAzw
Not sure if it's really the kind of thing you have in mind, but works for me :)
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 ยป
"CouchDB, close to the metal." <a href="http://github.com/jchris/couchrest/tree/master">CouchRest</a> is a RESTful layer for accessing CouchDB, based off CouchDB's included Javascript reference client. CouchRest also includes helpers for running large queries etc. There is also a base class for ActiveRecord / Datamapper style ORM, called CouchRest::Model.