A simple validation library.
Project description
Validobj
Validobj is library that takes semistructured data (for example JSON and YAML configuration files) and converts it to more structured Python objects. It places the emphasis on good error messages and simplicity of implementation and usage rather than full generality, extensibility or runtime performance. This makes it particularly suitable for processing configuration files and giving good user feedback. It is best viewed as a tool to deal with the heavy lifting of validation, to be complemented by purpose specific checks.
Validobj has no schema and instead relies on newer Python functionality (such as dataclasses) and enums of the data as well as a small subset of the typing module to specify the expected shape.
It requires Python 3.7 and has no other dependencies.
Example
import dataclasses
import enum
from typing import Mapping, Set, Tuple, List
from validobj import parse_input
inp = {
'global_environment': {'CI_ACTIVE': '1'},
'stages': [
{
'name': 'compile',
'os': ['linux', 'mac'],
'script_path': 'build.sh',
'disk_permissions': ['READ', 'WRITE', 'EXECUTE'],
},
{
'name': 'test',
'os': ['linux', 'mac'],
'script_path': 'test.sh',
'framework_version': [4, 0],
},
],
}
class DiskPermissions(enum.Flag):
READ = enum.auto()
WRITE = enum.auto()
EXECUTE = enum.auto()
class OS(enum.Enum):
mac = enum.auto()
windows = enum.auto()
linux = enum.auto()
@dataclasses.dataclass
class Job:
name: str
os: Set[OS]
script_path: str
framework_version: Tuple[int, int] = (1, 0)
disk_permissions: DiskPermissions = DiskPermissions.READ
@dataclasses.dataclass
class CIConf:
stages: List[Job]
global_environment: Mapping[str, str] = dataclasses.field(default_factory=dict)
print(parse_input(inp, CIConf))
# This results in a dataclass instance with the correct types:
#
#CIConf(
# stages=[
# Job(
# name='compile',
# os={<OS.linux: 3>, <OS.mac:1>},
# script_path='build.sh',
# framework_version=(1, 0),
# disk_permissions=<DiskPermissions.EXECUTE|WRITE|READ: 7>,
# ),
# Job(
# name='test',
# os={<OS.linux: 3>, <OS.mac: 1>},
# script_path='test.sh',
# framework_version=(4, 0),
# disk_permissions='<DiskPermissions.READ: 1>',
# ),
# ],
# global_environment={'CI_ACTIVE': '1'},
#)
#
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file validobj-0.1.tar.gz
.
File metadata
- Download URL: validobj-0.1.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a2e78c18aaf7db88a9dc43eb17235c4399d019faf9690c244a74e0bb24ec33b |
|
MD5 | ff013b291890d819acd1e4cc30866b74 |
|
BLAKE2b-256 | 1ff4c0bacb92b0e18b6c54d1fa071aa5c7347619b6ea7a6525ca548fe687777d |
File details
Details for the file validobj-0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: validobj-0.1-py2.py3-none-any.whl
- Upload date:
- Size: 23.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39e19310807a62a141ab55d9194677702c4492e457d3b86c7d3edf4b084937a4 |
|
MD5 | c83e8401cbacdb9779beaaf8a9530a6f |
|
BLAKE2b-256 | 58316f0526279f5123a773c3798ed419b2461f6567479fd50e3ee8796c78cb7c |