Skip to content
Discussion options

You must be logged in to vote

Is this not elegant?

from fastapi import Depends, FastAPI, Query
from fastapi.testclient import TestClient
from datetime import date
from pydantic import BaseModel
from typing import Optional, TypeVar, Type, Protocol


class AlignedDateRange(BaseModel):
    start: Optional[date]
    end: Optional[date]

    @classmethod
    def validate(cls, v):
        start, end = v.split(",")

        return cls(start=start or None, end=end or None)

    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def __modify_schema__(cls, field_schema):
        field_schema.update(
            pattern="YYYY-MM-DD,YYYY-MM-DD",
            examples=["2021-01-01,2021-02…

Replies: 12 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
Converted from issue

This discussion was converted from issue #3656 on February 28, 2023 16:36.