Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferred validators and missing #197

Open
mazvv opened this issue Nov 15, 2014 · 2 comments
Open

Deferred validators and missing #197

mazvv opened this issue Nov 15, 2014 · 2 comments

Comments

@mazvv
Copy link

mazvv commented Nov 15, 2014

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.

@Eger37
Copy link

Eger37 commented Jan 18, 2023

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

@mvattuone
Copy link

I also ran into this issue, and our schema is indeed bound.

The only way I was able to resolve it was by overriding the missing values in deserialize - this seems like a hack, but I don't see a better way as of now.

Ideally it would be great to find some way to have the validators run even if missing sets None - I imagine the problem is that None implies there is no value, or nothing to validate, since None is valid according to missing

    # For some `SchemaNode`
    def deserialize(self, cstruct):
        request = self.bindings["request"]
        blah = request.json_body.get("blah")

        if (
            blah is None
            or blah is ""
        ):
            self.missing = required
            self.missing_msg = "Custom Message"

        return super().deserialize(cstruct)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants