strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.01k stars 533 forks source link

How to validate input model field? #2148

Open web5gogogo opened 2 years ago

web5gogogo commented 2 years ago
@strawberry.input
class OrderParameters():
    offerer: str
    zone: str
    order_type: int = strawberry.field(
        description="The order type, support: {}".format(OrderType.dict())
    )
    start_time: int
    end_time: int
    salt: int
    offer: List[OfferItem]
    consideration: List[ConsiderationItem]
    zone_hash: str
    total_original_consideration_items: int
    conduit_key: str
    counter: int

I have many input fields, and each field needs to be verified separately?

Upvote & Fund

Fund with Polar

ThirVondukr commented 2 years ago

Hello, what kind of validation are we talking about? If it's simple validation that doesn't have to interact with your data storage/db you can use pydantic, otherwise you should check them in your code.

humphrey commented 2 years ago

Probably not the most elegant solution, but I had existing Django ModelForms, so I wrote my own helper function that generates a base strawberry input from a ModelForm. I then use that form in my mutation just like I would in a Django view function.