[Python] Pydantic nestled list type with sub-list minimum length

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I want to create a Pydantic class wrapping a list with string sub-lists that have to be at least of length two.

    For example, the following are valid:

    • [] empty list with no sublists is valid
    • [["a", "b"], ["c", "d", "e"] is valid because each sub-list is of at least length 2

    The following are not valid:

    • [[]] an empty sub-list is not valid
    • [["a"], ["b", "c"] one of the sub-lists has a length less than two which is invalid

    How do I do this in Pydantic? I don't think I can nest conlist? Can I use typing.Annotated to accomplish this?

    Continue reading...

Compartilhe esta Página