convex-js icon indicating copy to clipboard operation
convex-js copied to clipboard

Convex Validator Wrong Error Message

Open danielyogel opened this issue 2 months ago • 0 comments

Hey, thanks for Convex! it's just wonderful.

One thing I noticed related to Values and Validator, which took me some time to debug:

import { v } from 'convex/values';
import { validate } from 'convex-helpers/validators';
import { z } from 'zod';

const validator = v.union(
   v.object({ status: v.literal('initialized'), updatedAt: v.number() }),
   v.object({
      status: v.literal('loading'),
      work: v.optional(v.string())
   }),
   v.object({
      status: v.literal('complete'),
      content: v.string()
   }),
   v.object({ status: v.literal('error'), errorMessage: v.string() })
);

const zodValidator = z.discriminatedUnion('status', [
   z.object({ status: z.literal('initialized'), updatedAt: z.number() }),
   z.object({
      status: z.literal('loading'),
      work: z.string().optional()
   }),
   z.object({
      status: z.literal('complete'),
      content: z.string()
   }),
   z.object({ status: z.literal('error'), errorMessage: z.string() })
]);

const value = {
   status: 'complete',
   wrongField: 'a'
};

zodValidator.parse(value); // EXPECTED message:  { "expected": "string","code": "invalid_type","path": ["content"],"message": "Invalid input: expected string, received undefined"}

validate(validator, value, { throw: true }); // WRONG message: Validator error for status: Expected `error`, got `"complete"`

related issue

danielyogel avatar Sep 18 '25 13:09 danielyogel