-
Notifications
You must be signed in to change notification settings - Fork 64
Open
Description
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"`
Metadata
Metadata
Assignees
Labels
No labels