fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.88k stars 181 forks source link

Shorthand for transforming `null` to `undefined` #709

Closed ktkk closed 1 month ago

ktkk commented 1 month ago

I have the following schema:

const InputSchema = object({
  input: pipe(nullish(pipe(number(), integer())), transform(x => x === null ? undefined : x)),
});

which I use on my backend to validate and coerce input from a Flutter app (Dart has no concept of undefined).

I was wondering if Valibot has built in functionality for this transform that I'm missing. If not, I think this would be a valuable addition.

ktkk commented 1 month ago

Nevermind, I overlooked the default parameter of nullish. My schema now looks like this:

const InputSchema = object({
  input: nullish(pipe(number(), integer()), undefined),
});