ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.71k stars 46 forks source link

Ability to provide default values for params/queries #227

Closed UberMouse closed 2 years ago

UberMouse commented 2 years ago

This is something I have wanted to be able to do a few times now and doesn't seem possible. Could be solved via the Zod default function if all the params/query fields were parsed with the schema (I don't see that happening anywhere but maybe I missed it), that's what I have tried so far.

ecyrbe commented 2 years ago

Indeed this is by design, cf https://github.com/ecyrbe/zodios/discussions/225 Because the default behaviour is to do it on backend, not frontend. what could be done is an option to activate it, but would not be activated by default.

UberMouse commented 2 years ago

An option to activate it would be useful, as we are only using @zodios/core on the front-end so that's where I need default values. I've updated @zodios/core to try the transform option mentioned in that discussion but it doesn't work for this scenario as the schema isn't executed for undefined values.

ecyrbe commented 2 years ago

As a general advice, if you have optional parameters with defaults, you should let your backend set it. Indeed, default values can be considered business code and you usually want business code to be executed on your backend. That's why zodios has this behaviour by default.

Nevetheless, this option is now available on @zodios/core v10.7.0. see documentation

UberMouse commented 2 years ago

Thanks :tada: The specific endpoint I'm adding now has different responses depending on whether you include a query param or not so I'm making two separate endpoints, one with the query param forced to true and the other without it.