Skip to main content

cleaned

pypi: https://pypi.org/project/cleand/

(pypi's project name is not cleaned because it wasn't available then)

Overview

cleaned is a declarative data validator.

Examples

import json
import cleaned as cl


class Request(cl.Cleaned):
    username = cl.Str(pattern='^[a-zA-Z_]+$', blank=False, min_length=3)
    password = cl.Str(blank=False, min_length=8)
    age = cl.Int()


def register_user_api(request_json: str) -> ...:
    dirty_data = json.loads(request_json)
    cleaned_data = Request(**dirty_data)

    # username matches ^[a-zA-Z_]+$ and it has at least 3 characters
    username = cleaned_data.username

    # password is at least 8 characters
    password = cleaned_data.password

    # age is a int value
    age = cleaned_data.age

    # do something with the data
    print(username, password, age)
    ...


register_user_api(json.dumps({
    'username': 'user',
    'password': 'KJF83h9q3FAS',
    'age': '20',
}))


try:
    Request(username='invalid format', password='short')
except cl.ValidationError as e:
    print(e.nested['username'])
    # ('The value must match: ^[a-zA-Z_]+$', 'pattern')
    print(e.nested['password'])
    # ('The length of the value must be longer than or equal to 8.', 'min_length')
    print(e.nested['age'])
    # ('This field is required', 'required')

Static Typing

mypy can handle almost all cleaned values in the library.

import cleaned as cl
import enum


class Examples(cl.Cleaned):
    class NestedExample(cl.Cleaned):
        a = cl.Int()

    class EnumExample(enum.Enum):
        a = 1
        b = 2

    a = cl.Either(cl.Int(), cl.Str(blank=False))
    b = cl.Int().opt()
    c = cl.Dict(key=cl.Int(), value=cl.Float().opt()).opt()
    d = cl.Nested(NestedExample)
    e = cl.Enum(EnumExample)
    f = cl.List(cl.Nested(NestedExample))
    g = cl.List(cl.Nested(lambda: Examples))


ex = Examples()

reveal_type(ex.a)
# Revealed type is 'Union[builtins.int*, builtins.str*]'

reveal_type(ex.b)
# Revealed type is 'Union[builtins.int*, None]'

reveal_type(ex.c)
# Revealed type is 'Union[builtins.dict*[builtins.int*, Union[builtins.float*, None]], None]'

reveal_type(ex.d)
# Revealed type is 'Examples.NestedExample*'

reveal_type(ex.e)
# Revealed type is 'Examples.EnumExample*'

reveal_type(ex.f)
# Revealed type is 'builtins.list*[Examples.NestedExample*]'

reveal_type(ex.g)
# Revealed type is 'builtins.list*[hoge.Examples*]'

Metadata

Release files for cleand 1.3.1

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

Source distribution (sdist)

Source distribution for cleand 1.3.1
File Size Uploaded
cleand-1.3.1.tar.gz 13.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cleand 1.3.1
File Interpreter ABI Platform
cleand-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / cleand-1.3.1.tar.gz

Download URL cleand-1.3.1.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6864eb136d036e4c6c4e4014cf75b648b57367917d7d1b3c448165321e091f1f
BLAKE2b-256 checksum
How to use checksums
17283b6a0d9b36297daf8b11b61d589216356b601fe4d53693a259a7940195e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / cleand-1.3.1-py3-none-any.whl

Download URL cleand-1.3.1-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e077a7acb5eddfdc6202bc33b8d99976d1a8c4e94aab5c78d7aa758b3a88b1ff
BLAKE2b-256 checksum
How to use checksums
9a83ee8267039e74f354f392dd75956ddd3bf1d8abcf2ef94178a37265d8db2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

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