Skip to main content

A dead-simple utility that validates if object has a certain structure.

Project description

Simple schema validator

A dead-simple utility that validates if object has a certain structure. Used in some of our projects.

pip install simple_schema_validator

An example:

Lets say we have an API that returns the following data:

{
  "user": 1,
  "profile": {
    "email": "some@user.com",
    "name": "Some User",
    "age": 20
  },
  "tokens": {
    "jwt": "...",
    "refresh": "...",
    "firebase": "...",
  }
}

And we are writing a simple integration test, that wants to assure the response has a certain structure.

Then we can use the schema validator like so:

from simple_schema_validator import schema_validator

data = get_data_from_api()

schema = {
  'user': Any,
  'profile': {
    'email': Any,
    'name': Any,
    'age': Any
  },
  'tokens': {
    'jwt': Any,
    'refresh': Any,
    'firebase': Any
  }
}

valid, missing_keys, additional_keys = schema_validator(schema, data)

assert valid, f'Response not valid, missing: {missing_keys}, additional: {additional_keys}'
  • missing_keys are those keys that are required in the schema, but not found in data.
  • additional_keys are those keys present in data, but not required by the schema.
  • Nested keys are represented with "dot" notation - profile.email, tokens.jwt, etc.

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

simple_schema_validator-0.0.3.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

simple_schema_validator-0.0.3-py2.py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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