Skip to main content

Validate python dictionaries (mongodb docs etc) using a JSON schema

Project description

dict-schema-validator

Validate python dictionaries (mongodb docs etc) using a JSON schema.

Install

pip install dict-schema-validator

JSON Schema example

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"
  }]
}

Getting Started

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"

Supported Field Types

  • string
  • bool
  • int
  • float
  • number (value can have int or float type)
  • date (for datetime)
  • null (for None values)
  • ObjectId (for bson.objectid.ObjectId)

JSON Schema Format

  • Exact type: "field_1" : "type"
  • One of the possible types: "field_1" : ["type1", "type2"]
  • Define a dictionary: "field_1": {"field_1_1" : "type", ...}
  • Define an array: "field_1": [ {"field_1_1" : "type", ...} ]

TODO

  • enums for string type
  • value validators
  • required (true, false) attribute for fields
  • array of base types support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict-schema-validator-0.2.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dict_schema_validator-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file dict-schema-validator-0.2.0.tar.gz.

File metadata

  • Download URL: dict-schema-validator-0.2.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2

File hashes

Hashes for dict-schema-validator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b3c6f34ff27a139de3493153b0a5518f0860a3a217dd89c04cee1a5faee00548
MD5 97558dbc6797d8e619f577cdd35dfd2f
BLAKE2b-256 0a5c7722ce94928f1ac1441bd5326b5a5a05d77b4ae78658056232d20c09322b

See more details on using hashes here.

File details

Details for the file dict_schema_validator-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dict_schema_validator-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2

File hashes

Hashes for dict_schema_validator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba9b899cc20a89ee0c9bd666da4ee162a2d0783dc6be0c8ceb020c77bc073aa8
MD5 34298f02cbef20e7ec79d05a6cc0a26e
BLAKE2b-256 280fd919e992f0ff607b7f3e81e5b1a4a9be5e0e664ff7a2f7d2796faa70bd64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page