0

I use cerberus for validate my data, like:

document = {
    "region": 77,
    "drivers": {
        "data": [
            { "birthday": "2004-01-01", "kbm_class": "3", "driving_experience": 10 },
            { "birthday": "1988-01-01", "kbm_class": "3", "driving_experience": 10 }],
        "type": "limited" 
    },
    "engine_power": 80,
    "is_taxi": False
}

and i use scheme like:

schema = {
    'engine_power': {'type': 'integer'},
    'region':  {'type': 'integer'},
    'is_taxi': {'type': 'boolean'},
    'drivers': {
        'schema': {
            'type': {'type': 'string'},
            'data': {
                'type': 'list',
                'schema': {
                    'type': 'dict',
                    'schema': {               
                        'birthday': {'type': 'string', 'validator': validate_age},
                        'kbm_class': {'type': 'string'},
                        'driving_experience': {'type': 'integer'}
                    }
                }
            }
        }
    }
}

When I get an error object, it has too much nesting: Cerberus creates a list for each level of nesting:

{'drivers': [{'data': [{0: [{'birthday': ['driver too young']}]}]}]}

Can i get something like:

{'drivers': {'data': [{'birthday': ['driver too young']}]}}
  • There's a type definition missing in the schema for `drivers`.Should be dict I guess. – gcw May 04 '17 at 16:38
  • You need to have the list index to know exactly where is the validation error. How can you know which item is wrong if you have many items and no index? e.g. response for 4 items where first and last have errors: `{'drivers': {'data': {0: {'birthday': 'driver too young'}, 3: {'birthday': 'driver too young'}}}}` – gcw May 04 '17 at 16:48
  • You're right, I did not think about the index, but i still want to remove nesting in lists Perhaps it is needed if I have a list of errors and for each error there is a list item. But I want to get the format exactly as in your comments, can I do that? – alexei rysev May 09 '17 at 15:13
  • The response in my comment was pasted as is from cerberus==0.9.2, using your code with `drivers` type set to `dict`. It's not what you are getting? – gcw May 09 '17 at 15:32
  • Yes! You must add your last comment as answer, and i mark it as right answer – alexei rysev May 09 '17 at 19:42

1 Answers1

2

Using cerberus==0.9.2 you get a slimmer error response like this:

{'drivers': {'data': {0: {'birthday': 'driver too young'}, 3: {'birthday': 'driver too young'}}}}

You cannot get rid of the indexes because they are important to show which items from the list contains errors.

gcw
  • 1,639
  • 1
  • 18
  • 37