Lazy Schema
Quick and simple schema validator.
Installation
pip install lazy-schema
How to Use
from lazy_schema import schema
from datetime import datetime
from bson import ObjectId
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]
}
SchemaPool
Just a collection where you can keep all your schemas.
from lazy_schema import SchemaPool
schemas = SchemaPool()
schemas.set(
"my_schema",
hello="world!",
number=123,
)
schemas.set(
"my_other_schema",
hello="there!",
number=456,
)
print(schemas.my_schema())
print(schemas.my_other_schema())
{
"hello": "world!",
"number": 123
}
{
"hello": "there!",
"number": 456
}
Loading from MongoDB with SchemaPool
You can load MongoDB documents with SchemaPool!
MongoDB Document
{
"_id": [ObjectId],
"__im_a_comment__": null,
"hello": "world!",
"number": 123
}
Script
from pymongo import MongoClient
from lazy_schema import SchemaPool
mongo = MongoClient(...)
collection = mongo.my_database.my_schemas
schemas = SchemaPool()
schema = schemas.pymongo(
collection,
query={
"_id": ObjectId(...),
},
)
print(schema())
Result
{
// `_id` is always excluded.
"hello": "world!",
"number": 123
}
You can also add it into the SchemaPool.
...
schema.add_to("my_schema", schemas)
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!"
}
Metadata
Release files for lazy-schema 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lazy_schema-0.4.0.tar.gz | 4.8 kB | Without provenance | Details for lazy_schema-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lazy_schema-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for lazy_schema-0.4.0-py3-none-any.whl |
Total release size: 11.3 kB
Release files / lazy_schema-0.4.0.tar.gz
| Download URL | lazy_schema-0.4.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f38b048cd16d76dc92441232814aa6e3c940a3029df178e836ff40ddd5dae10
|
|
BLAKE2b-256 checksum How to use checksums |
d59be1ded099ee4f20d533770b8f201ab8c1863468d18217074c2907e47edbb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.8.10 Linux/5.15.0-107-generic
|
Release files / lazy_schema-0.4.0-py3-none-any.whl
| Download URL | lazy_schema-0.4.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29714be5bc76d51e906a172e008605995522728081952137c621f91e8a90f7d1
|
|
BLAKE2b-256 checksum How to use checksums |
652f3e4784058eb6acf78bd74caaef11c8ecc04a3268828ba25642d683e833a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.8.10 Linux/5.15.0-107-generic
|
Security for lazy-schema 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.