On 04/18/2013 09:05 PM, Chris Rossi wrote:
Well, the card_num schema node is going to be a child, most likely, of a
mapping schema node.  You could put the validator there.

Chris


Something like this maybe:

class CreditCardSchema(colander.Schema):
    card_num = colander.SchemaNode(colander.String())
    exp_month = colander.SchemaNode(colander.Integer(),
                   validator=colander.Range(1,12))
    exp_year = colander.SchemaNode(colander.Integer(),
                   validator = colander.Range(2013,2014))
    @staticmethod
    def validator(form, value):
        if value['exp_year']==2013 and value['exp_month']<6:
             raise(colander.Invalid(form, u'Wrong year and month'))
        elif value['exp_year']==2014 and value['exp_month']>6:
             raise(colander.Invalid(form, u'Wrong year and month'))

class LoadCreditCard(colander.Schema):
    ... all the other stuff goes here ...
    credit_card = CreditCardSchema(validator=CreditCardSchema.validator)

x = LoadCreditCard()

x.deserialize({'credit_card':{'card_num':'xxxxx', 'exp_month':'12', 'exp_year':'2014'}})
---------------------------------------------------------------------------
Invalid Traceback (most recent call last)<ipython-input18-b82991abfbd3> in <module>() ----> 1 x.deserialize({'credit_card':{'card_num':'xxxxx', 'exp_month':'12', 'exp_year':'2014'}})

....
Invalid: {'credit_card': u'Wrong year and month'}




On Thu, Apr 18, 2013 at 7:53 PM, Vincent Catalano
<[email protected] <mailto:[email protected]>> wrote:

    Hello Pyramid community,

    I was wondering if any of you have used Colander and could help me
    out with a validation question. I want to validate a credit card schema:

    {'card_num': 'xxxxxxxxxx', 'exp_month': 12, 'exp_year': 2013}

    The problem is that the validation for the expiration month will
    change depending on the year. Is there  any way to pass in other
    nodes to the give nodes 'validation' method? OR is it possible (or a
    good practice) to throw a colander exception in an 'after_bind' method?

    Thanks!

    --
    You received this message because you are subscribed to the Google
    Groups "pylons-discuss" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [email protected]
    <mailto:pylons-discuss%[email protected]>.
    To post to this group, send email to [email protected]
    <mailto:[email protected]>.
    Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google
Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to