Schema Validations Plugin
Listen to this article
After listening to Prag Dave's Keynote Speech this afternoon, I was motivated to implement some of the things he'd been asking for. Here's my first cut at it.
As the doco says, the plugin reads some—ok only one at the moment but we'll see how many others I get done before the beer runs out—database column constraints and tries to apply the closest corresponding rails validation. The first one I implemented reads the NOT NULL constraints against columns and generates a corresponding validates_presence_of.
I literally just whipped it up with no tests or what-not and I've only played with it against PostgreSQL, so if it has bugs or behaves oddly for whatever reason, please let me know, send me as much info as possible and I'll make it work. Nothing better than having real people testing it for me ;-)
Update: Ok, so far the beer has lasted long enough to implement validation of numbers (including specific support for integers) and lengths of strings.
Update: Now calls validates_presence_of anytime you declare a belongs_to association for a NOT NULL foreign-key column.
Update: Single-column unique indexes are now converted to validates_uniqueness_of.
Comments
Hi, me again: the mad bug-reporter. :)
Just wondering what the rationale is for ignoring foo_id columns: I'm sure there is a good reason, but I can't quite work it out.
cheers,
iain
Posted by: iain | July 20, 2006 04:44 AM
I currently ignore primary key, _id, _count, _at and _on columns. I figured that _id columns probably needed special treatment but hadn't thought much further than that to be honest. I guess I was being conservative :)
Posted by: Simon Harris
|
July 20, 2006 09:25 AM
Fair enough, that makes sense. I do kinda think the foreign key fields should be validated but I agree that it's not totally obvious generally how to do it. hmm.
I guess I'll get in touch if I work out a general solution. :)
cheers!
Posted by: iain | July 20, 2006 09:40 AM
How about intercepting calls to belongs_to and adding validation there? The reason xxx_id columns themselves can't be validated for null is that when valid? runs, the id may not have been assigned yet but there will be an object sitting xxx attribute which _can_ be checked.
Posted by: Simon Harris
|
July 20, 2006 09:48 AM
Ah yes, that explains it, thanks. Would calling validates_presence_of :xxx be enough for that to work? In the case that the xxx_id column is not null, there would have to be an object in the xxx attribute for the object to be valid, right?
cheers
Posted by: iain | July 20, 2006 10:20 AM
yup. I usually manually code the validates_presence_of :xxx and it works just fine. So really we're talking about just doing that automagically. See if I can find some time this afternoon and check it in.
Posted by: Simon Harris
|
July 20, 2006 10:31 AM
Well, here's a patch that seems to do the job:
http://geeksoc.org/~ibroadfo/validate_foreign.diff
Rigorously tested! that is, with this I can create valid model objects by setting :xxx_id columns. :)
Posted by: iain | July 20, 2006 10:36 AM
err, and of course all my unit tests still pass. phew!
Posted by: iain | July 20, 2006 10:44 AM
Added support for belongs_to associations. I think this is "safer" and produces slightly friendlier messages than just processing the _id fields directly. Ditto with the tests :)
Posted by: Simon Harris
|
July 20, 2006 11:01 AM
Cool, yes that's nicer.
I now have no validates_* calls in my models at all. :)
Posted by: iain | July 20, 2006 11:33 AM
Hi, bugging you again: r44 misses a reference to child on line 31. Good catch on the inclusion_of [true, false] thing, I had been using that in my models anyway and hadn't noticed that we'd lost it in just using presence_of.
I'm not sure what the best way to report things like this is: would email make more sense? I suspect yes. If I'm going to keep nitpicking, is it worth setting up a dev mailing list on rubyforge, so you can filter me out with ease? :)
cheers
Posted by: iain | July 24, 2006 08:47 AM
There have always been two forums specifically for bugs and feature requests: http://rubyforge.org/tracker/index.php?group_id=1699
:)
Posted by: Simon Harris
|
July 24, 2006 09:09 AM
Aye. one-liners seem a bit much for a whole bugreport though, plus I'm lazy. I guess I can force myself to submit these 5-character patches via rubyforge anyway. :)
cheers
Posted by: iain | July 24, 2006 09:34 AM
Don't get me wrong, I really appreciate the fact that you even bother reporting them, it's just easier for me to manage as I have a dozen or so projects to keep track of :)
Posted by: Simon Harris
|
July 24, 2006 09:38 AM
Hey Simon, long time no chat ;-) I think you might have some competition over at http://magicmodels.rubyforge.org/
Posted by: Ted | August 9, 2006 02:43 AM
After releasing a gem extension for ActiveRecords (http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/) I was sent the following email:
"This sounds a lot like one of Simon's pet projects
http://www.redhillconsulting.com.au/blogs/simon/archives/000339.html
"Perhaps the two of you can combine forces and rule the galaxy ;-)"
So, which half of the galaxy do you want? :)
Could I adapt some of the ideas from your schema_validations plugin into this gem?
Cheers
Nic
Posted by: Dr Nic | August 9, 2006 05:22 AM
It's all open source. Feel free to take whatever you want :)
Posted by: Simon Harris
|
August 9, 2006 08:43 AM
http://drnicwilliams.com/2006/09/21/dr-nic-magic-models-validate-anything-anytime-anywhere/
Hopefully the Magic Models is a happy home for these great ideas!
Cheers
Nic
Posted by: Dr Nic | September 21, 2006 10:29 PM
Fantastic. Looks great.
Posted by: Simon Harris
|
September 21, 2006 10:37 PM