fabian-hiller / valibot

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

Modify variant schema options reference more precise #600

Closed chimame closed 4 months ago

chimame commented 4 months ago

Overview

The options specified when generating a variant schema are curried. So I wrote this fix.

Details

I don't think it's a good way to do it, but in my use case, if I want to make changes to the options below, it won't work without this fix. Since it works with other schemas, the options are specified differently only for variant schema.

const schema = variant('type', [
  object({ type: literal('a'), value: string() }),
  object({ type: literal('b'), value: number() }),
])
const convertSchema = {
  ...schema,
  options: type.options.map((option) =>
    pipe(
      unknown(),
      transform((output) => output.value == null ? { ...output, value: "" } : output),
      option,
    ),
}
safeParse(convertSchema, { type: 'a', value: null }) // <-- parse error
fabian-hiller commented 4 months ago

Great catch! Thank you!

fabian-hiller commented 4 months ago

v0.31.0-rc.3 is available