No project description provided
Project description
Lazy Schema
Quick and simple schema validator.
Installation
pip install lazy-schema
How to Use
from lazy_schema import schema
from datetime import datetime
exampleSchema = schema(
stringField="Hello World!",
numberField=123,
booleanField=True,
lambdaField=lambda: datetime.utcnow(),
)
document = exampleSchema(
stringField="Hi World!",
)
print(document)
{
"stringField": "Hi World!",
"numberField": 123,
"booleanField": True,
"lambdaField": [datetime Object]
}
Loading JSON Files
{
"stringField": "Hi World!",
"numberField": 123,
"booleanField": true
}
from lazy_schema import schema
exampleSchema = schema(
"path/to/file.json",
lambdaField=lambda: datetime.utcnow(),
)
document = exampleSchema(
stringField="Hi World!",
)
print(document)
{
"stringField": "Hi World!",
"numberField": 123,
"booleanField": True,
"lambdaField": [datetime Object]
}
Special Keywords
Field names that are enclosed with double underscores _
are special keywords.
They are not included in the schema.
These can be used when creating a schema,
exampleSchema = schema(
__discrete__=True,
...
)
inside a schema validation,
document = exampleSchema(
__discrete__=True,
...
)
and inside a JSON file.
{
"__discrete__": true,
...
}
__discrete__
Excludes fields with a None
default value.
from lazy_schema import schema
exampleSchema = schema(
__discrete__=True,
fieldA="Hello World!",
fieldB=None,
fieldC="Hi World!",
)
document = exampleSchema(
fieldA=None,
)
print(document)
{
"fieldA": None,
"fieldC": "Hi World!"
}
__no_default__
Default values are not written.
from lazy_schema import schema
exampleSchema = schema(
__no_default__=True,
fieldA="Hello World!",
fieldB=None,
fieldC="Hi World!",
)
document = exampleSchema(
fieldA="Hi There!",
)
print(document)
{
"fieldA": "Hi There!"
}
__no_null__
None
values will never be written.
from lazy_schema import schema
exampleSchema = schema(
__no_null__=True,
fieldA="Hello World!",
fieldB=None,
fieldC="Hi World!",
)
document = exampleSchema(
fieldA=None,
)
print(document)
{
"fieldC": "Hi World!"
}
Comments
Any field names enclosed with double underscores __name__
will never be written.
from lazy_schema import schema
exampleSchema = schema(
__comment__="fieldA should not be None!",
fieldA="Hello World!",
__yet_another_comment__="fieldB is always None...",
fieldB=None,
__fieldC__="fieldC should always have 'World' in it!",
fieldC="Hi World!",
)
document = exampleSchema(
fieldA=None,
)
print(document)
{
"fieldA": "Hello World!",
"fieldB": None,
"fieldC": "Hi World!"
}
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
lazy_schema-0.1.0.tar.gz
(2.1 kB
view hashes)
Built Distribution
Close
Hashes for lazy_schema-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4be45c81c373aea99f4392463c7847ae49d9356bfe8ed8a2fceb11496ad14936 |
|
MD5 | df1bf9dcc4b387a5ed07ab5557b24813 |
|
BLAKE2b-256 | cdc155d1a46d929bb2c332de1a7d342e2fd78415a33cf54af3302e0a87b157e5 |