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

how can I set the parameter allow_unknown to support unknown parameters #22

Open
jakehu opened this issue Sep 2, 2021 · 1 comment
Open

Comments

@jakehu
Copy link

jakehu commented Sep 2, 2021

Hello, how can I set the parameter allow_unknown to support unknown parameters

@XReaper95
Copy link

XReaper95 commented Oct 4, 2021

Facing the same problem. by looking at the code it seems impossible to put it at the top level. Because the decorator just uses validator = Validator(schema) there is no way to add it to the top. For internal dictionaries you can just use :

{
   'a_text': {'type': 'string', 'required': True},
   'a_dict': {
                'type': 'dict',
                'required': True,
                'schema': { .... } ,
                'allow_unknown': True
        }
}

Maybe there is another way to do it but can't find anything on Cerberus documentation. I will try to make a PR when I have time. For now I recommend creating a fork and doing it yourself, something like:

def validate_json(schema, clean=False, status_code=400, allow_unknown=False):
    '''Decorator. Validates request body json.

    When *clean* is true, normalized data is passed to the decorated method
    as *valid_json*.

    Args:
        schema (dict): Cerberus-compatible schema description
        clean (bool): should cleaned json be passed to the decorated method
        status_code (number): status code to return when data is incorrect
    '''
    validator = Validator(schema, allow_unknown)

    def vd(f):
            ...... # the rest of the function, nothing needs to be changed

or maybe:

def validate_json(schema, clean=False, status_code=400, **kwargs):
    '''Decorator. Validates request body json.

    When *clean* is true, normalized data is passed to the decorated method
    as *valid_json*.

    Args:
        schema (dict): Cerberus-compatible schema description
        clean (bool): should cleaned json be passed to the decorated method
        status_code (number): status code to return when data is incorrect
    '''
    validator = Validator(schema, **kwargs)

    def vd(f):
            ...... # the rest of the function, nothing needs to be changed

for a more generic solution.

Obs: I haven't tested any of this code yet.

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

2 participants