colander icon indicating copy to clipboard operation
colander copied to clipboard

Deferred validators and missing

Open mazvv opened this issue 10 years ago • 2 comments

Hi There is a problem with deferred validators and missing. For example schema

class CrosspaymentSchema(ResourceSchema):
    date = colander.SchemaNode(
        Date(),
    )
    account_from_id = colander.SchemaNode(
        colander.Integer(),
        missing=None,
        validator=account_from_validator
    )
    subaccount_from_id = colander.SchemaNode(
        colander.Integer(),
        missing=None,
    )

and validator

class AccountFromValidator(object):
    def __init__(self, request):
        self.request = request

    def __call__(self, node, value):
        request = self.request
        subaccount_from_id = cast_int(request.params.get('subaccount_from_id'))
        if not value and not subaccount_from_id:
            raise colander.Invalid(
                node,
                _(u'Set at least one account or subaccount from any section'),
            )
        if value and subaccount_from_id:
            raise colander.Invalid(
                node,
                _(u'Set only account or subaccount or clear both')
            )

@colander.deferred
def account_from_validator(node, kw):
    request = kw.get('request')
    return colander.All(AccountFromValidator(request))

I expect to get 'Set at least one account or subaccount from any section' error if both account_from_id and subaccount_from_id is empty. But I have not validation errors.

mazvv avatar Nov 15 '14 14:11 mazvv

May be you need to bind your schema? https://docs.pylonsproject.org/projects/colander/en/latest/binding.html

Eger37 avatar Jan 18 '23 13:01 Eger37