https://github.com/keenlycode/dictify
Dictify : Documents schema and data validation
https://github.com/keenlycode/dictify
Last synced: 3 months ago
JSON representation
Dictify : Documents schema and data validation
- Host: GitHub
- URL: https://github.com/keenlycode/dictify
- Owner: keenlycode
- License: mit
- Created: 2018-08-18T09:47:13.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2023-08-21T18:04:46.000Z (almost 3 years ago)
- Last Synced: 2025-04-15T19:53:58.969Z (about 1 year ago)
- Language: Python
- Homepage: https://keenlycode.github.io/dictify/
- Size: 7.56 MB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
{ Dictify }
Documents schema and data validation
{ dictify } is a python library to define data schema and validation with simple and flexible syntax for documents data type such as **JSON** and **Python** `dict` object.
## Get it
---
```shell
$ pip install dictify
```
## Schema definition
---
Let's start with an example note data:
```json
{
"title": "Dictify",
"content": "dictify is easy",
"timestamp": "2021-06-13T05:13:45.326869"
}
```
The schema condition should be like:
**title**
1. Required field
2. Must be `str` instance
3. Length is <= 300
**content**
1. Must be `str` instance
**timestamp**
1. Required field
2. Default to datetime on creation in ISO format string
3. Must be a valid ISO datetime string
```python
from datetime import datetime
from dictify import Model, Field
class Note(Model):
title = Field(required=True)\
.instance(str)\
.verify(lambda value: len(value) <= 300) # [1]
content = Field().instance(str)
timestamp = Field(
required=True,
default=lambda: datetime.utcnow().isoformat())\
.verify(lambda value: datetime.fromisoformat(value))
```
> [1] Field validations can be chained.
## Data assignment and validation
---
After schema definition, now we can use it to create `Model` instance with required data.
```python
note = Note({'title': 'Dictify', 'content': 'dictify is easy'})
# `note` can be used like a dict object.
note.update({
"content": "Updated content",
})
note["content"] = "Updated again"
# Code below will raise `Model.Error`.
note.update({'title': 0})
note['title'] = 0
```
> Note : Use `try..except` to catch errors if needed.
## Convert data to native 'dict' or 'JSON'
---
```python
import json
note_dict = dict(note) # Convert to python built-in `dict`
note_json = json.dumps(note) # Convert to JSON string
```