valibot icon indicating copy to clipboard operation
valibot copied to clipboard

Are there any similarities about zod `.superRefine`?

Open dhythm opened this issue 10 months ago • 1 comments

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?

dhythm avatar Apr 24 '24 18:04 dhythm

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 avatar Apr 25 '24 02:04 fabian-hiller

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

fabian-hiller avatar Jun 25 '24 13:06 fabian-hiller