Skip to content

feat: BeforeValidators casting a simple string to {"type": "thing"} #48

@gselzer

Description

@gselzer

one "nice to have" (but definitely not mandatory) feature here is a BeforeValidator that casts a simple string to {"type": "thing"}

def _str_to_type_dict(obj: Any) -> Any:
    if isinstance(obj, str):
        return {"type": obj}
    return obj

AnyController = Annotated[..., BeforeValidator(_str_to_type_dict), Field(discriminator="type")]

then you can

Camera(controller="orbit")

but again, not mandatory, and anyone familiar with pydantic also knows they can do

Camera(controller={"type": "orbit"})

Originally posted by @tlambert03 in #42 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions