Shoo
Shoo

Reputation: 43

Validate list of dicts using cerberus

Started to use cerberus for contract testing purposes.

It works perfectly in cases, when we got dict-based JSON structure, e.g:

{'results': [{"key": "value"}, {"key": "value"}, {"key":"value}]}

But everything goes bad when response is just a list of dicts, e.g.:

[{"key": "value"}, {"key": "value"}, {"key":"value}]

The basic error im facing with is:

cerberus.validator.DocumentError: '[{'id': 'XXX', 'is_supported': True}]' is not a document, must be a dict

Specifying top level type: "list" in schema not helped, returning schema error:

cerberus.schema.SchemaError: {'type': ['must be of dict type']}

The naive solution is just to pass through list and validate all the entities inside. But looks like there should be more proper way to do this.

As far as i understand (and as google says), top level array is a valid JSON structure, so there should be a way to validate it properly, isn't it?

Upvotes: 1

Views: 3389

Answers (1)

George Fields
George Fields

Reputation: 112

I'd prefer to post this as a comment but I don't have the reputation. Nonetheless, someone else asked the same thing here: https://github.com/pyeve/cerberus/issues/220

One solution was to write a Validator subclass, and another was to use cerberus-list-schema, which apparently is based on Cerberus but supports list and array schemas as well.

Upvotes: 2

Related Questions