Skip to main content

A simple and easy to use TOML validator for Python.

Project description

TOML Validator

top language code size last commit issues contributors PyPI License

A simple and easy to use TOML validator for Python.

Installation

You can install the package from PyPI:

pip install tomlval

The package is available for Python 3.11 and newer.

Concepts

Before using the package, there are some concepts you may need to understand for the most optimal use of the package.

Key

A key is the name of a field in a TOML file, such as name, person.name, etc. Keys must conform to the TOML specification, which means keys are either snake_case or SCREAMING_SNAKE_CASE. For the validator, keys may also include wildcards, such as *name, person.*, etc.

Handler

A handler is a function that is called for a certain key. Handlers can be of the following types:

  • Types, such as int, str, float.
  • Tuples/Lists of types, such as (int, str), [int, str].
  • Anonymous functions (lambda)
  • Named functions (Callable objects, such as def my_handler(key, value): ...)

The following argument configurations are supported:

  • fn()
  • fn(key)
  • fn(value)
  • fn(key, value)

If the handler has any other parameters than key or value, the validator will raise a TOMLHandlerError.

Handlers may return any type, but is is recommended to use the return type as an error message if the value is invalid. The validator considers a None return value a successful validation.

Schema

The schema is used to bring give the validator default values. The schema is defined in the TOMLSchema class, and is passed to the TOMLValidator class. To create a schema, you pass a dictionary with the keys and their respective allowed types.

Here is an example of a schema:

{
    "single_type": str,
    "list_of_strings": [str],
    "mixed_list:" [str, int],
    "multiple_types": (int, float),
    "optional?": str,
    "nested": {
        "key": str
    }
}

When a schema is defined, the validator will also check if values are missing and if their types are correct. If a handler is defined for a key, the validator will use the handler instead of the type defined in the schema.

Validator

The validator is the core of the package. It is used to validate a TOML file. A schema is optionally passed to the validator, and handlers are added using the add_handler method. Once you feel ready, you can call the validate method to get a dictionary of errors.

Currently, there are two type of error structures, for type errors and all other errors.

Type errors are structured as follows:

"key": (message, (value, expected_type, actual_type))

expected_type and actual_type can be either type or tuple[type]

All other errors have a slightly simpler structure:

"key": (message, value)

The point of the validator is to parse the data and get the errors in a clean and easy way. What you do with the errors is up to you.

Example

Here is a full example of how to use the validator.

import pathlib
import tomllib
import datetime
from tomlval import TOMLValidator, TOMLSchema

# Load data
path = pathlib.Path("data.toml")

with path.open("rb") as file:
    data = tomllib.load(file)

# Define schema (optional)
structure = {
    "first_name": str,
    "last_name": str,
    "age": int,
    "email": str,
    "phone": str,
    "birthday": datetime.datetime,
    "address": {
        "street": str,
        "city": str,
        "zip": int
    }
}

schema = TOMLSchema(structure) # If the struture is invalid, a TOMLSchemaError is raised

# Define validator
validator = TOMLValidator(data, schema)

# Add handlers
validator.add_handler("*_name", lambda key: None if key in ["first_name", "last_name"] else "invalid-key")
validator.add_handler("age", lambda value: None if 18 < value < 100 else "invalid-age")
validator.add_handler("*", lambda: "invalid-key")

# Validate the data
errors = validator.validate()

Future Plans

Future plans are found in the TODO file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

tomlval-1.0.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

tomlval-1.0.3-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file tomlval-1.0.3.tar.gz.

File metadata

  • Download URL: tomlval-1.0.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tomlval-1.0.3.tar.gz
Algorithm Hash digest
SHA256 385fcab8ec8502b83afd0591a818b95d0bd66bf55bc71d18f9c7b11f99c921d9
MD5 63327f913eb0befffd449bd654012d95
BLAKE2b-256 6c116859c0488badbbd8a1b3fe9b2069a4e127025cd9e6c36985a025d7f6e953

See more details on using hashes here.

File details

Details for the file tomlval-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: tomlval-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tomlval-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2965449a7f3524f20c9d57078688f7dfaddaa68d16fa9c13469e8a5524cad72a
MD5 4a37bc58c07c682f8a1c784af73c5089
BLAKE2b-256 208d82f488ff035f7e63332aaef1c9d3606e0c040da183ed23a4868496489b52

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