Skip to content

Convex Validator Wrong Error Message #85

@danielyogel

Description

@danielyogel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions