Skip to main content

A lightweight model validator for modern projects.

Project description


A lightweight model validator for modern projects.


pip install bike

First Pedals

Lets define a simple model to represent a person.

import bike

class Person:
    name: str
    height: float
    weight: float

p1 = Person(
    name='Patrick Love', 

A Person instance can be created passing the attributes. Also can be instantiated by a dict.


data = {
    'name': 'Patrick Love',
    'height': 75,
    'weight': 180

p1 = Person(**data)

Nested Models

We can create and mananger more complex structs by nested models. It's simply by using a model as field type of other model.

import bike

class Address:
    address_1: str
    address_2: str = ''
    city: str
    state: str
    code: str
    country: str

class Phone:
    country_code: str
    local_code: str
    number: str

class Person:
    name: str
    address: Address
    phones: list[Phone]
payload = {
    'name': 'Aline Mark',
    'address': {
        'address_1': '239 W 45th St',
        'address_2': '',
        'city': 'New York',
        'state': 'NY',
        'code': '10036',
        'country': 'EUA'
    'phones': [
            'country_code': '+1',
            'local_code': '010',
            'number': '134354325'
            'country_code': '+2',
            'local_code': '011',
            'number': '134565436'
p1 = Person(**payload)
# +1
# 011
# NY

Parsing to Json and Dict

We can parse data to dict or json by using .dict() or json() methods respectively.

import bike

class Make:
    name: str
    country: str

class Car:
    name: str
    make: Make

m1 = Make(name='Nissan', country='JP')
c1 = Car(name='Leaf', make=m1)

c1_dict = c1.dict()
# JP

c1_json = c1.json()
# {"name": "Leaf", "make": {"name": "Nissan", "country": "JP"}}

Validating Fields

Fields can be validated adding the validator annotation.

import bike

class Character:
    name: str
    health: float = 1.0

    def name_validator(cls, val):
        return val.title()
    def health_validator(cls, val):
        if val < 0: 
            return 0.0
        elif val > 1:
            return 1.0
        return val
c1 = Character(name='ninki walker', health=2.0)
# Ninki Walker
# 1.0

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bike-0.3.6.tar.gz (5.9 kB view hashes)

Uploaded source

Built Distribution

bike-0.3.6-py3-none-any.whl (6.9 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page