19


i looking for tool, or examples to/how to validate dictionaries in python.
For example, i have dict:

test = {'foo' : 'bar', 'nested' : {'foo1' : 'bar1', 'foo2' : 'bar2'} }

And now i must validate it. Lets say, value for key foo must be boolean False or non-empty string. Next, if key foo1 have value bar1, that key foo2 must be int in range 1..10. I wrote simple function to do this, but this is not what i exactly want. Yea, sure, i can test every single item in dict with if..else, but if dict have >50 elements, then it is a bit not comfortable.

Is there any good tool/lib to do this in Python? I not looking for parsers, only fast and effective way to do this right.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Galmi
  • 743
  • 1
  • 11
  • 18

5 Answers5

29

Voluptous is a nice tool that does this http://pypi.python.org/pypi/voluptuous

anijhaw
  • 8,954
  • 7
  • 35
  • 36
4

You can also try the link below:
https://github.com/sunlightlabs/validictory
Its a great package that helps in validation in an easier way

Vidz
  • 545
  • 1
  • 6
  • 16
3

I highly recommend Cerberus for its readability or jsonschema because it uses the JSON Schema standard

Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
0

Webster is a pypi package that does dictionary validation and value regex validation.. this allows you to insure that the dictionary has all the keys its supposed to and the values are more or less what you would expect.

https://pypi.python.org/pypi/Webster

  • 1
    since this was a 4 years old question with an already accepted answer it would better if you give more details. Are you proposing a more innovative solution ? If yes please explain it. Have you replicated the example proposed ? – aberna Feb 12 '15 at 20:59
0

This dict-schema-validator package is a very simple way to validate python dictionaries.

Here is a simple schema representing a Customer:

{
  "_id":          "ObjectId",
  "created":      "date",
  "is_active":    "bool",
  "fullname":     "string",
  "age":          ["int", "null"],
  "contact": {
    "phone":      "string",
    "email":      "string"
  },
  "cards": [{
    "type":       "string",
    "expires":    "date"
  }]
}

Validation:

from datetime import datetime
import json
from dict_schema_validator import validator


with open('models/customer.json', 'r') as j:
    schema = json.loads(j.read())

customer = {
    "_id":          123,
    "created":      datetime.now(),
    "is_active":    True,
    "fullname":     "Jorge York",
    "age":          32,
    "contact": {
        "phone":    "559-940-1435",
        "email":    "york@example.com",
        "skype":    "j.york123"
    },
    "cards": [
        {"type": "visa", "expires": "12/2029"},
        {"type": "visa"},
    ]
}

errors = validator.validate(schema, customer)
for err in errors:
    print(err['msg'])

Output:

[*] "_id" has wrong type. Expected: "ObjectId", found: "int"
[+] Extra field: "contact.skype" having type: "str"
[*] "cards[0].expires" has wrong type. Expected: "date", found: "str"
[-] Missing field: "cards[1].expires"
Jean DuPont
  • 411
  • 7
  • 22