简体   繁体   中英

python cerberus - how to catch UNALLOWED_VALUE?

How do I catch the UNALLOWED_VALUE error?

# my schema
schema = {
    'sort': {
        'type': 'list',
        'empty': False,
        'required': True,
        'schema': {
            'type': 'dict',
            'schema': {
                'property': {
                    'type': 'string',
                    'required': True,
                    'allowed': ['test']
                },
                'direction': {
                    'type': 'string',
                    'required': True,
                    'allowed': ['asc', 'desc']
                }
            }
        }
    }
}

# my raw data
sort = {'sort': [{'property': '', 'direction': 'asc'}, {'property': '', 'direction': 'desc'}]}

# my error
{'sort': [{0: [{'property': ['unallowed value ']}], 1: [{'property': ['unallowed value ']}]}]}

cerberus.errors.UNALLOWED_VALUE in v._errors - does not work

thanks for the answer

So I don't have a good answer, but I have an explanation with a not-so-fun workaround for you.

You can start to understand the issue here by looking at the docs . If you look at the info property, you'll see that it mentions how bulk validations have their individual errors stuffed in here. The issue you're having is that the UNALLOWED_VALUE error you expect, is being masked by other errors. To see this more clearly you can look at the underlying error object that is being returned (use._errors instead of.errors). When you do that you will see:

[ValidationError @ 0x1fe369bcbe0 ( document_path=('sort',),schema_path=('sort', 'schema'),code=0x82,constraint={'type': 'dict', 'schema': {'property': {'type': 'string', 'required': True, 'allowed': ['test']}, 'direction': {'type': 'string', 'required': True, 'allowed': ['asc', 'desc']}}},value=[{'property': '', 'direction': 'asc'}, {'property': '', 'direction': 'desc'}],info=([ValidationError @ 0x1fe369bc4f0 ( document_path=('sort', 0),schema_path=('sort', 'schema', 'schema'),code=0x81,constraint={'property': {'type': 'string', 'required': True, 'allowed': ['test']}, 'direction': {'type': 'string', 'required': True, 'allowed': ['asc', 'desc']}},value={'property': '', 'direction': 'asc'},info=([ValidationError @ 0x1fe369bcb80 ( document_path=('sort', 0, 'property'),schema_path=('sort', 'schema', 'schema', 'property', 'allowed'),code=0x44,constraint=['test'],value="",info=('',) )],) ), ValidationError @ 0x1fe369bcdc0 ( document_path=('sort', 1),schema_path=('sort', 'schema', 'schema'),code=0x81,constraint={'property': {'type': 'string', 'required': True, 'allowed': ['test']}, 'direction': {'type': 'string', 'required': True, 'allowed': ['asc', 'desc']}},value={'property': '', 'direction': 'desc'},info=([ValidationError @ 0x1fe369bcbb0 ( document_path=('sort', 1, 'property'),schema_path=('sort', 'schema', 'schema', 'property', 'allowed'),code=0x44,constraint=['test'],value="",info=('',) )],) )],) )]

It's not pretty to look at, but if you analyze it you will see that there are other errors wrapping the one you want to see. So when you simply test by using a simple UNALLOWED_VALUE in v.errors check, it fails. You can crudely make this work in your specific example code if you dig all the way down though. You'd need a check like: cerberus.errors.UNALLOWED_VALUE in v._errors[0].info[0][0].info[0] . Obviously, this is gross and I don't recommend it.

I'm not quite sure if there is a mechanism in the library to handle this better, but as a workaround you can simply check the info property of errors you find for instances of more ValidationError classes. This isn't clean and maybe you can do more with the Validator class error_handler parameter, but hopefully this helps a bit!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM