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.

Basic usage

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

result = schema_validator(schema, data)

if not result:
    print(f'Schema not valid. Missing: {result.missing_keys}, additional: {result.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.

Type checking

The util supports simple schema type checking.

Currently, the supported types in the schema are:

  • Any
  • int
  • float
  • str
  • bool

If the type is Any, no type checking is done.

If there's a type mismatch, the errors are placed in the type_errors attribute of the result, which is a list of type errors.

The general format of a single type error is:

{
  'path': 'the.path.to.the.value.in.data',
  'expected': the_expected_type_as_defined_in_the_schema,
  'actual': the_actual_type_of_the_value
}

Here's an example:

from simple_schema_validator import schema_validator


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

data = {
  'user': 'Some User',
  'profile': {
    'email': 'someuser@hacksoft.io',
    'name': 'Some User',
    'age': "29"
  },
  'tokens': {
    'jwt': 'some token value',
    'refresh': 'some token value',
    'firebase': 'some token value'
  }

}

result = schema_validator(schema, data)


assert bool(result) is False
assert result.type_errors == [{'path': 'profile.age', 'expected': int, 'actual': str}]

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.5.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

simple_schema_validator-0.0.5-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file simple_schema_validator-0.0.5.tar.gz.

File metadata

  • Download URL: simple_schema_validator-0.0.5.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for simple_schema_validator-0.0.5.tar.gz
Algorithm Hash digest
SHA256 696b4117d67ac05947b9c0c7d58394d46f192e149194a26c42c0013229ec24aa
MD5 fdb87ca98ae347fd8872aa5291e88e99
BLAKE2b-256 af7011f9433d9c1cd9a6d2cc1fe0df6932745fb2506e307b49353e629c6ea7d2

See more details on using hashes here.

File details

Details for the file simple_schema_validator-0.0.5-py2.py3-none-any.whl.

File metadata

  • Download URL: simple_schema_validator-0.0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for simple_schema_validator-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9c5a9dea4af37f389b588ee395bec2daca401b99d178cf10f22d68210402ce36
MD5 a1d659e8f233ef37d36aafe6ea7289f4
BLAKE2b-256 b503c976c7f0a1dd1e378c1844bf655729051a6cfd14b39f306ef8e9ed62c2ac

See more details on using hashes here.

Supported by

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