How to use typesystem - 10 common examples

To help you get started, we’ve selected a few typesystem examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / typesystem / tests / test_base.py View on Github external
def test_validation_error_repr():
    result = Example.validate_or_error({"a": "a"})
    assert (
        repr(result.error)
        == "ValidationError([Message(text='This field is required.', code='required', index=['b'])])"
    )

    result = typesystem.String(max_length=10).validate_or_error("a" * 100)
    assert (
        repr(result.error)
        == "ValidationError(text='Must have no more than 10 characters.', code='max_length')"
    )
github encode / typesystem / tests / test_schemas.py View on Github external
import decimal
import uuid

import pytest

import typesystem
import typesystem.formats


class Person(typesystem.Schema):
    name = typesystem.String(max_length=100, allow_blank=False)
    age = typesystem.Integer()


class Product(typesystem.Schema):
    name = typesystem.String(max_length=100, allow_blank=False)
    rating = typesystem.Integer(default=None)


def test_required():
    class Example(typesystem.Schema):
        field = typesystem.Integer()

    value, error = Example.validate_or_error({})
    assert dict(error) == {"field": "This field is required."}

    class Example(typesystem.Schema):
        field = typesystem.Integer(allow_null=True)

    value, error = Example.validate_or_error({})
    assert dict(value) == {"field": None}
github encode / typesystem / tests / test_schemas.py View on Github external
def test_schema_uuid_serialization():
    class User(typesystem.Schema):
        id = typesystem.String(format="uuid")
        username = typesystem.String()

    item = User(id="b769df4a-18ec-480f-89ef-8ea961a82269", username="tom")

    assert item.id == uuid.UUID("b769df4a-18ec-480f-89ef-8ea961a82269")
    assert item["id"] == "b769df4a-18ec-480f-89ef-8ea961a82269"
github encode / typesystem / tests / test_definitions.py View on Github external
class ExampleE(typesystem.Schema, definitions=definitions):
        field_on_e = typesystem.Integer()
        example_f = typesystem.Array(items=[typesystem.Reference("ExampleF")])

    class ExampleF(typesystem.Schema, definitions=definitions):
        field_on_f = typesystem.Integer()

    value = ExampleE.validate(
        {"field_on_e": "123", "example_f": [{"field_on_f": "456"}]}
    )
    assert value == ExampleE(field_on_e=123, example_f=[ExampleF(field_on_f=456)])

    class ExampleG(typesystem.Schema, definitions=definitions):
        field_on_g = typesystem.Integer()
        example_h = typesystem.Object(
            properties={"h": typesystem.Reference("ExampleH")}
        )

    class ExampleH(typesystem.Schema, definitions=definitions):
        field_on_h = typesystem.Integer()

    value = ExampleG.validate(
        {"field_on_g": "123", "example_h": {"h": {"field_on_h": "456"}}}
    )
    assert value == ExampleG(field_on_g=123, example_h={"h": ExampleH(field_on_h=456)})
github encode / typesystem / tests / tokenize / test_tokenize_json.py View on Github external
def test_tokenize_list():
    token = tokenize_json("[true, false, null]")
    expected = ListToken(
        [ScalarToken(True, 1, 4), ScalarToken(False, 7, 11), ScalarToken(None, 14, 17)],
        0,
        18,
    )
    assert token == expected
    assert token.value == [True, False, None]
    assert token.lookup([0]).value is True
    assert token.lookup([0]).string == "true"
    assert token.lookup([0]).start.char_index == 1
    assert token.lookup([0]).end.char_index == 4
github encode / typesystem / tests / tokenize / test_tokenize_yaml.py View on Github external
def test_tokenize_list():
    token = tokenize_yaml(YAML_LIST)
    expected = ListToken(
        [
            ScalarToken(True, 3, 6),
            ScalarToken(False, 10, 14),
            ScalarToken(None, 18, 21),
        ],
        1,
        22,
    )
    assert token == expected
github encode / typesystem / tests / tokenize / test_tokenize_json.py View on Github external
def test_tokenize_floats():
    token = tokenize_json("[100.0, 1.0E+2, 1E+2]")
    expected = ListToken(
        [
            ScalarToken(100.0, 1, 5),
            ScalarToken(100.0, 8, 13),
            ScalarToken(100.0, 16, 19),
        ],
        0,
        20,
    )
    assert token == expected
    assert token.value == [100.0, 1.0e2, 1e2]
    assert token.lookup([0]).value == 100.0
    assert token.lookup([0]).string == "100.0"
    assert token.lookup([0]).start.char_index == 1
    assert token.lookup([0]).end.char_index == 5
github encode / typesystem / tests / tokenize / test_tokenize_yaml.py View on Github external
def test_tokenize_floats():
    token = tokenize_yaml(YAML_FLOATS)
    expected = ListToken([ScalarToken(100.0, 3, 7), ScalarToken(100.0, 11, 16)], 1, 17)
    assert token == expected
github encode / typesystem / tests / test_json_schema.py View on Github external
def test_schema_to_json_schema():
    class BookingSchema(typesystem.Schema):
        start_date = typesystem.Date(title="Start date")
        end_date = typesystem.Date(title="End date")
        room = typesystem.Choice(
            title="Room type",
            choices=[
                ("double", "Double room"),
                ("twin", "Twin room"),
                ("single", "Single room"),
            ],
        )
        include_breakfast = typesystem.Boolean(title="Include breakfast", default=False)

    schema = to_json_schema(BookingSchema)

    assert schema == {
        "type": "object",
github rsinger86 / drf-typed-views / test_project / testapp / views.py View on Github external
"""


class BagOptions(str, Enum):
    paper = "paper"
    plastic = "plastic"


class SuperUser(BaseModel):
    id: int
    name = "John Doe"
    signup_ts: datetime = None
    friends: List[int] = []


class Booking(typesystem.Schema):
    start_date = typesystem.Date()
    end_date = typesystem.Date()
    room = typesystem.Choice(
        choices=[
            ("double", "Double room"),
            ("twin", "Twin room"),
            ("single", "Single room"),
        ]
    )
    include_breakfast = typesystem.Boolean(title="Include breakfast", default=False)


@typed_api_view(["POST"])
def create_user(user: SuperUser):
    return Response(dict(user))