« That Weird Devil Number | Main | A Plea to the Ruby on Rails Core Team »

Schema Validations Plugin

Listen to this articleListen 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

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 :)

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!

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.

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

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.

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. :)

err, and of course all my unit tests still pass. phew!

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 :)

Cool, yes that's nicer.

I now have no validates_* calls in my models at all. :)

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

There have always been two forums specifically for bugs and feature requests: http://rubyforge.org/tracker/index.php?group_id=1699

:)

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

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 :)

Hey Simon, long time no chat ;-) I think you might have some competition over at http://magicmodels.rubyforge.org/

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

It's all open source. Feel free to take whatever you want :)

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

Fantastic. Looks great.

Post a comment