Skip to content

Custom type with Annotated and pydantic #313

@kondera

Description

@kondera

Hey,

I've been loving kor so far (especially how it returns dicts with raw, errrors and validated data).
But is there some way to create your own "types" with validators with pydantic 2 e.g.:

PhoneNumber = Annotated[
    str,
    AfterValidator(lambda num: ''.join(x for x in num if x.isdigit())),
    Field(
        description="Phone number in E.164 format",
        examples=["505505505"],
        min_length=9,
        max_length=9,
    ),
]

It would be quite useful for extraction tasks.
For now I've been kinda creating 2 models - 1 for kor which just gets strings, and 2nd to create more strict that validates the previous model, which is not ideal, but gets the job done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions