Define your validation schema inline

The TurboGears docs show how to assign validators for individual parameters in the validate decorator like this:

@validate(validators={'a':validators.Int(), 'b':validators.DateConverter()})
@error_handler()
def f(self, a, b, tg_errors=None):
# Now a is already an integer and b is already a datetime.date object,
# unless there were some validation errors.

That’s great, but there are some validations that depend on numerous parameters at the same time. For example, you might want to make sure that an employee’s hire date precedes the termination date.

I already knew how to subclass validators.Schema to do this, and then pass that instance into the validate decorator like this:

class MattSchema(validators.Schema):
a = validators.Int()
b = validators.DateConverter()
chained_validators = [blah] # pretend that blah does some compound validation.

@validate(validators=MattSchema())
def f(self, a, b)

This approach is fine, but today I discovered that it is also possible to define a Schema inline, inside the validate decorator, and specify the chained_validators right there, like this:

@expose('.templates.shiftreports.overtime')
@validate(validators=validators.Schema(
a=validators.Int(),
dt=validators.DateConverter(),
chained_validators=[blah]),
state_factory=matt_state_factory)
def f(self, a, b):

What’s the point? Well, it seems wasteful to define a class and hide it in another file if that schema is only going to be used for exactly one controller. Also, this makes it really fast for me to mix and match comound validators with controllers. I don’t need to pop open my separate validators file where all my elaborate schemas live. I can define them right here.

I’m very forgetful too, so I like to keep my code shallow so that I can instantly see what the heck something does. With all the validators right there, I can easily figure out what the system intends to do.

However, I would define a Schema subclass as soon as I see that I need the same thing twice.

I’m happy that the FormEncode authors had the foresight to support this inline approach along with the declarative style.

Published by

matt

My name is Matt Wilson and I live in Cleveland Heights, Ohio. I love random emails from strangers, so get in touch! matt@tplus1.com.

  • http://www.chiccanvas.com canvas prints

    this is very helpful. I too like to keep my code shallow and Im going to check out FormEncode

  • http://www.freelancewebdesigner.me freelance web designer

    thank you matt
    was nice article
    keep going

  • Medela

    Thanks for the post I liked reading it, keep posting interesting stuff like this coming!

    Lisa

  • http://www.elite-proxy.net Craigslist Proxy

    Some good points raised in that post. Will be back to check for more.

  • http://sextoyreviewblog.com sex toys

    Thanks for explaining by means of giving code..Very useful and good post..I will try this and post my further comments here..

  • http://sites.google.com/site/craigslistproxies/ Craigslist Proxy

    Interesting post. I have made a twitter post about this. Others no doubt will like it like I did.

  • jannonces

    agence de communication
    I think that however, I would define a Schema subclass as soon as I see that I need the same thing twice its very unuseful.

  • http://blog.tplus1.com Matt Wilson

    I just wanted to document it so that I wouldn't forget how. Yeah, I
    agree that defining things inline is usually not ideal. It isn't easy
    to test or reuse the code. There are sometimes moments when it is
    useful though. It is nice to see everything all in one place.

  • jannonces

    agence de communication
    Yes Matt It is nice to see everything all in one place 🙂

  • chiropractors marketing

    Declarative style is more appropriate and much easy to understand. Thank you for giving us some insights about schema.

  • http://www.massagershop.com/body-part/back-massager.html back

    Indeed it is, I like that way as well, that's really the best way.

  • http://blog.tplus1.com Matt Wilson

    spam

  • http://robert2584.buzznet.com/user/main/ Martin

    Thanks for the post.

  • http://aaacreditguide.com/charge-offs/ charge off

    Very helpful. I found this on google and it fixed my issues.

  • http://www.frames4sale.com/Sunglasses/Prada.html Prada Sunglasses

    There are sometimes moments when it is useful though. It is nice to see everything all in one place. However, I would define a Schema subclass as soon as I see that I need the same thing twice.

    Thanks

  • http://www.frames4sale.com/Sunglasses/Prada.html Prada Sunglasses

    I’m very forgetful too, so I like to keep my code shallow so that I can instantly see what the heck something does. With all the validators right there, I can easily figure out what the system intends to do.

  • http://antivirus-pro.wetpaint.com/ antivirus pro

    Interesting post. I found this on google. Doesn't really fix what I need though!

  • http://www.frames4sale.com/Sunglasses/Prada.html Prada Sunglasses

    Looks a really good post! Feel good to see everything back to it's place! FormEncode authors are doing a really good job!

    Cheers!

  • http://www.sunglassesuk.com/Oakley_Sunglasses.asp Oakley Sunglasses

    Thanks for the post.. It really help me a lot..
    I'm looking forward for your next post..

  • http://www.valwriting.com essay help

    I cannot understand how does it work? Can any one help me in figuring this out?

  • http://www.cosmeticdentistryguide.co.uk/dentists/surrey-cosmetic-dentist.html cosmetic dentists surrey

    thanks you for sharing a priceless information you have posted. Keep doing this forever. Your article are really 100% beneficent in all ways.

  • http://www.psphomebrewdownloads.com Psp Homebrew Games

    I just wanted to document it so that I wouldn't forget how. Yeah, I
    agree that defining things inline is usually not ideal. It isn't easy
    to test or reuse the code. There are sometimes moments when it is
    useful though. It is nice to see everything all in one place.

  • http://www.goarticles.com/cgi-bin/showa.cgi?C=1829378 Acne

    I liked your inline validation schema. But I think it might some problem regarding parameters.

  • http://www.acnetreatmentsite.com/ Acne

    Thanks for sharing such a knowledgeable thing with us…..very useful.

  • http://www.goarticles.com/cgi-bin/showa.cgi?C=1822947 Teeth Whitening

    I always had problem in executing the inline statements and they were always accompanied with errors..I hope your suggestions might workout..

  • http://www.teethwhiteninginstructions.com/ Whiten Teeth

    Yes its right if every thing is in one place so that I can easily figure out what the system intends to do.

  • http://www.goarticles.com/cgi-bin/showa.cgi?C=1829301 Cellulite

    Hey I liked your validation schema. Hopefully it will work. Thanks for sharing information.

  • dentist

    good observation..
    thanks for sharing it..

    dentists on brighton
    Dentists in Brighton

  • millipo15

    This article gives the light in which we can observe the reality. this is very nice one and gives in depth information. thanks for this nice article Good post…..Valuable information for all.I will recommend my friends to read this for sure

    Thanks
    Kane
    ______________________________________________
    british heart foundation diet | cayenne pepper detox | diets for quick weight loss | easy diet plans | maple syrup detox | miracle cabbage soup diet | sugar busters diet | weight gain diets | british heart foundation diet | cayenne pepper detox | diets for quick weight loss | easy diet plans | maple syrup detox | sugar busters diet | weight gain diets | miracle cabbage soup diet

  • http://www.stylishtemplate.com Stylish Templates

    Also, this makes it really fast for me to mix and match comound validators with controllers. I don’t need to pop open my separate validators file where all my elaborate schemas live. I can define them right here.