fabian-hiller / valibot

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

Are there any similarities about zod `.superRefine`? #546

Closed dhythm closed 4 months ago

dhythm commented 6 months ago

Hi, folks! I'm just considering to migrate validator from zod to valibot. Then, I met an issue to prevent it.

zod provides superRefine method and it is useful like the following case;

somethingSchema.superRefine((val, ctx) => {
  if (!val.length) {
    ctx.addIssue({
     code: z.ZodIssueCode.custom
      message: "not inputted"
    })
  }
  const overLength = val.length - MAX_LENGTH
  if (overLength > 0) {
    ctx.addIssue({
     code: z.ZodIssueCode.custom
      message: `${overLength} characters exceeded from max length`
    })
  }
})

I guess pipeline with custom cannot cover this use case;

  string([
    custom(input => !!input.length, "not inputted"),
    custom(input => input.length - MAX_LENGTH <= 0, `${input.length - MAX_LENGTH} characters exceeded from max length`) // cannot get `input.length`
  ])

Are there any solution about this in valibot?

fabian-hiller commented 6 months ago

We are currently rewriting Valibot. This will most likely be possible in the future. I expect a new release in about 3 or 4 weeks. You can follow the progress in #502.

fabian-hiller commented 4 months ago

We now support rawCheck and rawTransform. Both APIs are similar to superRefine in Zod.