Git Product home page Git Product logo

dictify's Introduction

{ 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.

! New in V3.1.0 strict mode post validation

Get it


$ pip install dictify

Schema definition


Let's start with an example note data:

{
    "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
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.

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'


import json

note_dict = dict(note) # Convert to python built-in `dict`
note_json = json.dumps(note)  # Convert to JSON string

dictify's People

Contributors

nitipit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

slapaper

dictify's Issues

Partial Validation Option

Partial Validation Option can be used to validate partial fields of the model.

  • Only validate model with corresponded key.
  • Use by option flag partial=True
class Contact(Model):
    type = Field(require=True).instance(str)\
        .verify(lambda value: value in ['phone', 'email', 'address'])
    note = Field().instance(str)\
        .verify(lambda value: len(value) <= 250)
    value = Field(require=True).instance(str)\
        .verify(lambda value: len(value) <= 1000)

contact = new Contact({type: 'phone'}, partial=True)

Test for every built-in dict methods

Set and test all these methods behavior which come with dict instance.
Some should be disabled since it's not go along with library concept
clear()
get(<key>[, <default>])
items()
keys()
values()
pop(<key>[, <default>]) -> should be disabled
popitem() -> should be disabled
update(<obj>)

Document how to cast Model instance to plain `dict`

Use dict() or dictify.Model.Dict.dict To make dictify.Model object to dict object
for example:

user = User() # now user is `dictify.Model.Dict` instance.
user = dict(user) # user becomes `dict` instance.
# Or user `user.dict()`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.