Skip to main content

A dictionary validation library based on partial, composable and expressive rules.

Project description

Certum

A dictionary validation library based on partial, composable and expressive rules.

Why use it

In case you need to assert some propeties from a particular dictionary in a DSL manner without comparing the entire structure or asserting fields.

Certum comes with the following features:

  • Easy to use
  • English friendly declaration
  • Error accumulation
  • Customizable
  • Anti KeyError

Using Certum

You can't use certum package for the moment, coming soon.

How it works

Basic usage

Imagine you have a very long json and you want to verify that it contains the following informations:

  • He should contains a key named 'name' containing a string.
  • He should contains a key named 'entities' being a list containing unique elements.
  • He should contains a key named 'nested' containing a key 'value' equals to 4.
from certum import ensure, that

my_obj = {
    "name": "hello",
    "entities": [1, 3, 5],
    "nested": {
        "value": 4
    }
}

validator = ensure(my_obj).respects(
    that("name").is_instance_of(str),
    that("entities").has_unique_elements(),
    that("nested", "value").equals(4)
)

validator.check()

Error handling

If there is errors, certum will accumulate and return errors elegantly:

from certum import ensure, that

my_obj = {
    "name": 2,
    "entities": [1, 3, 3],
    "nested": {
        "value": 2
    }
}

validator = (
    ensure(my_obj)
    .respects(
        that("name").is_instance_of(str),
        that("name").equals("Hello"),
        that("entities").foreach(this.equals(1)),
        that("nested", "value").equals(4),
    )
)

validator.check()

# certum.exception.CertumException: 

# [name] => The value is instance of int, expected str.
# [name] => The value is 2, expected Hello.
# [entities -> 2] => The value is 3, expected 1.
# [entities -> 1] => The value is 3, expected 1.
# [nested -> value] => The value is 2, expected 4.

Strategies

Erros can be sorted, filtered and printed using different strategies.

As an example, you may want to try the GroupedPrinting strategy with the AlphabeticalSorting strategy, this will give you a list of errors like this:

from certum import ensure, that, this
from certum.strategy.printing.grouped import GroupedPrinting
from certum.strategy.sorting.alphabetical import AlphabeticalSorting


my_obj = {
    "name": 2,
    "entities": [1, 3, 3],
    "nested": {
        "value": 2
    }
}

validator = (
    ensure(my_obj)
    .respects(
        that("name").is_instance_of(str),
        that("name").equals("Hello"),
        that("entities").foreach(this.equals(1)),
        that("nested", "value").equals(4),
    )
    .using(GroupedPrinting(), AlphabeticalSorting())
)

validator.check()

# certum.exception.CertumException: 

# entities -> 1   => The value is 3, expected 1.
# entities -> 2   => The value is 3, expected 1.
# name            => The value is 2, expected Hello.
#                    The value is instance of int, expected str.
# nested -> value => The value is 2, expected 4.

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

certum-1.0a1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

certum-1.0a1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file certum-1.0a1.tar.gz.

File metadata

  • Download URL: certum-1.0a1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/5.10.8-100.fc32.x86_64

File hashes

Hashes for certum-1.0a1.tar.gz
Algorithm Hash digest
SHA256 31a61475d51774fed96ea3939b7935f8ef5a1dbba1222a113e6fb73d3f1d5dcc
MD5 104d8f68cf1c099c84283d85ede529b6
BLAKE2b-256 fc93a7863e850709efc6dc7ea5227a31cf5fd37a5972d35e2e70fca8d5954a64

See more details on using hashes here.

File details

Details for the file certum-1.0a1-py3-none-any.whl.

File metadata

  • Download URL: certum-1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/5.10.8-100.fc32.x86_64

File hashes

Hashes for certum-1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 7c01f8a1bd3ca940617a70858976b55f3aed7337351152599d3f55f6a9fbb420
MD5 c7990c13901cc6d9b8594193653a4ce4
BLAKE2b-256 2060bf511343e9bb046b4b5f2a40bb47a9532331ccb6921fad54f195fe137c0e

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