Skip to main content

Validate-it

Build Status Coverage Status PyPI version Downloads

About

Ultimate data validation tool built on top of the typing module

Features:

  • validation by type hints
  • validation on __init__: SomeModel(**kwargs)
  • validation on __setattr__: some_instance.some_field = value
  • built-in options for types:
    • min_value, max_value (based on < and >)
    • min_length, max_length, size (based on len())
  • cast for incoming value and outgoing value: Options(parser=int, serializer=str)
  • alias for incoming keys and rename for outgoing keys: d: int = Options(alias='dyn', rename='dynamic')
  • validation by list allowed values: Options(allow=[1, 2, 3])
  • validation by custom list of validators: Options(validators=[is_odd, is_even])
  • auto pack nested values: data: List[SomeModel] = Options(auto_pack=True, packer=SomeModel)
  • all this options can be callable: Options(min_value=dynamic_min_value)

Installation

With pip:

pip install validate-it

Supported fields

import re
from datetime import datetime
from typing import Dict, List, Union, Optional
from validate_it import schema, Options


class IsNotEmailError(Exception):
    pass


def is_email(name, key, value, root):
    if not re.match(r"[^@]+@[^@]+\.[^@]+", value):
        raise IsNotEmailError(f"{key}: is not email")

    return value

@schema
class Example:
    # required fields
    field_a: datetime
    field_b: float
    
    # required fields with defaults
    field_c: str = "unknown"
    field_d: int = 9
    
    # required fields with nested types
    field_e: Dict[int, str]
    field_f: List[int]
    
    # optional fields
    field_g: Optional[int]
    field_h: Union[int, None] # equivalent of Optional[int]
    
    # with some validators:
    fields_i: int = Options(default=0, max_value=100, min_value=100)
    fields_j: str = Options(size=10)
    fields_k: str = Options(min_length=10, max_length=20)
    fields_l: List[str] = Options(size=5)
    fields_m: str = Options(validators=[is_email])
    fields_n: int = Options(allowed=[1, 2, 3])
    
    # with search (input) alias:
    fields_o: int = Options(alias="field_n")
    
    # with rename (output) alias:
    fields_p: int = Options(rename="field_q")
    
    # with serializer used in #to_dict(), outgoing value is str type
    fields_q: int = Options(serializer=str)
    
    # with parser used in #from_dict() or direct setattr, incoming value will be parsed as int
    fields_r: int = Options(parser=int)

Validation example

from typing import List
from validate_it import *


@schema
class Simple:
    a: int
    b: int


simple = Simple(a=1, b=2)
simple.a = 2
simple.b = 3

try:
    simple.a = 'not int'
except TypeError:
    print("Wrong type")

@schema
class Owner:
    first_name: str
    last_name: str


@schema
class Characteristics:
    cc: float = Options(min_value=0.0)
    hp: int = Options(min_value=0)


@schema
class Car:
    name: str = Options(min_length=2, max_length=20)
    owners: List[Owner] = Options(auto_pack=True, packer=pack_value)
    characteristics: Characteristics = Options(default=lambda: {"cc": 0.0, "hp": 0}, auto_pack=True, packer=pack_value)
    convert: bool = Options(parser=bool)


_data = {
    "name": "Shelby GT500",
    "owners": [
        {
            "first_name": "Randall",
            "last_name": "Raines",
        }
    ],
    "characteristics": {
        "cc": 4.7,
        "hp": 306
    },
    "unknown_field": 10,
    "convert": 1 
}

_expected = {
    "name": "Shelby GT500",
    "owners": [
        {
            "first_name": "Randall",
            "last_name": "Raines",
        }
    ],
    "characteristics": {
        "cc": 4.7,
        "hp": 306
    },
    "convert": "1"
}

car = Car(**_data)
assert to_dict(car) == _expected

Dataclass example

from validate_it import *
from dataclasses import dataclass


@schema
@dataclass
class Simple:
    a: int
    b: int


simple = Simple(a=1, b=2)
simple.a = 2
simple.b = 3

try:
    simple.a = 'not int'
except TypeError:
    print("Wrong type")

Simple mapping example

from validate_it import *


@schema
class User:
    first_name: str = Options(alias="f")
    last_name: str = Options(alias="l")

_in_data = {
    "f": "John",
    "l": "Connor"
}

user = User(**_in_data)

assert to_dict(user) == {"first_name": "John", "last_name": "Connor"}

Nested mapping example

from validate_it import *
from accordion import compress


@schema
class Player:
    nickname: str = Options(alias="info.nickname")
    intelligence: int = Options(alias="characteristics/0")
    dexterity: int = Options(alias="characteristics/1")
    strength: int = Options(alias="characteristics/2")
    vitality: int = Options(alias="characteristics/3")

_in_data = {
    "info": {
        "nickname": "Killer777",
    },
    "characteristics": [
        7,
        55,
        11,
        44
    ]
}

player = Player(**compress(_in_data))

assert to_dict(player) == {
    "nickname": "Killer777", 
    "intelligence": 7, 
    "dexterity": 55, 
    "strength": 11, 
    "vitality": 44
}

and back:

from validate_it import *
from accordion import expand


@schema
class Player:
    nickname: str = Options(rename="info.nickname")
    intelligence: int = Options(rename="characteristics/0")
    dexterity: int = Options(rename="characteristics/1")
    strength: int = Options(rename="characteristics/2")
    vitality: int = Options(rename="characteristics/3")

_in_data = {
    "nickname": "Killer777", 
    "intelligence": 7, 
    "dexterity": 55, 
    "strength": 11, 
    "vitality": 44
}

player = Player(**_in_data)

assert expand(to_dict(player)) == {
    "info": {
        "nickname": "Killer777",
    },
    "characteristics": [
        7,
        55,
        11,
        44
    ]
}

Requirements

Tested with python3.6, python3.7, pypy3.6-7.0.0

Metadata

Release files for validate-it 0.11.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for validate-it 0.11.2
File Size Uploaded
validate-it-0.11.2.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for validate-it 0.11.2
File Interpreter ABI Platform
validate_it-0.11.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.5 kB

Release files / validate-it-0.11.2.tar.gz

Download URL validate-it-0.11.2.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
255cff5298d0526c4d782a31452265ff3a760efbe38ac595a46892c9efec53d6
BLAKE2b-256 checksum
How to use checksums
945bce68a0274e9bd2b5d733c5c4df367e964cd9f9a08a8451c1a2949449fd8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.3 CPython/3.8.6 Darwin/18.7.0

Release files / validate_it-0.11.2-py3-none-any.whl

Download URL validate_it-0.11.2-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af41438ba432ec270f479bcdacc57ad762eef39c294d8b1785edf4ee1d23e23b
BLAKE2b-256 checksum
How to use checksums
6d66c454b90c450c5d8311e8323f843b5ca5a7ca8f670d089e83b12e9c1d0096
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.3 CPython/3.8.6 Darwin/18.7.0

Release history Release notifications | RSS feed

This release

0.11.2 This release

2 release files

0.11.1

2 release files

0.10.1

1 release file

0.10.0

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.0

1 release file

0.8.6

1 release file

0.8.5

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.5

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.9

1 release file

0.3.8

1 release file

0.3.7

1 release file

0.3.6

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.12

1 release file

0.2.11

1 release file

0.2.10

1 release file

0.2.9

1 release file

0.2.8

1 release file

0.2.7

1 release file

0.2.6

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.0

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page