iway1 / trpc-panel

MIT License
627 stars 43 forks source link

z.boolean().optional() sends input as `false` instead of `undefined` #72

Closed JoeKarow closed 10 months ago

JoeKarow commented 1 year ago

When defining the input schema, optional booleans are sent to tRPC with the value of false rather than undefined. This also happens to boolean fields inside of z.object({}).partial()

iway1 commented 1 year ago

Yeah we need a better way of dealing with optionals in general. Boolean fields default to "false" in the form state, and right now there's not really any way to set an type of field to be "undefined" in case the field is optional.

Some ideas:

  1. One easy fix could be to allow toggling between JSON and form input views, then the user could just input a json object directly similar to swagger.. this would cover all cases where the user is wanting to input some null or undefined.

  2. Also could add some toggle or something to let the dev toggle between "undefined" and "defined", but this could start making the UI get more noisy?

Would love any opinions on these approaches or others

JoeKarow commented 1 year ago

I think option 1 would be the most straightforward (with the bonus of null handling).

If you're looking for a quick fix on the booleans at this time, would switching to a true/false radio button set work? An optional field would just not have any initial value set. Or a true/false/undefined set for optionals?

iway1 commented 1 year ago

I just played around with the radio solution with a "true/false" field and having the default value be undefined, the dev can clear the form to get it set back to optional if needed. Seems to work pretty well

Going to go ahead and release that as a quick fix for the boolean issue, and we'll add a json entry option for other cases