Skip to content

Commit

Permalink
Update CLI validator to match server for v.record() (#29872)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: cacdb389c52e7f97226e2266975b97774eeae641
  • Loading branch information
sujayakar authored and Convex, Inc. committed Sep 16, 2024
1 parent f85ff59 commit f798b18
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
45 changes: 31 additions & 14 deletions src/cli/codegen_templates/component_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,41 @@ export async function resolveFunctionReference(
// The server sends down `udfType` capitalized.
const udfType = analyzedFunction.udfType.toLowerCase();

const argsValidator = parseValidator(analyzedFunction.args);
let argsType = "any";
if (argsValidator) {
if (argsValidator.type === "object" || argsValidator.type === "any") {
argsType = validatorToType(argsValidator);
} else {
return await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: `Invalid function args: ${analyzedFunction.args}`,
});
try {
const argsValidator = parseValidator(analyzedFunction.args);
if (argsValidator) {
if (argsValidator.type === "object" || argsValidator.type === "any") {
argsType = validatorToType(argsValidator);
} else {
// eslint-disable-next-line no-restricted-syntax
throw new Error(
`Unexpected argument validator type: ${argsValidator.type}`,
);
}
}
} catch (e) {
return await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: `Invalid function args: ${analyzedFunction.args}`,
errForSentry: e,
});
}

const returnsValidator = parseValidator(analyzedFunction.returns);
let returnsType = "any";
if (returnsValidator) {
returnsType = validatorToType(returnsValidator);
try {
const returnsValidator = parseValidator(analyzedFunction.returns);
if (returnsValidator) {
returnsType = validatorToType(returnsValidator);
}
} catch (e) {
return await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: `Invalid function returns: ${analyzedFunction.returns}`,
errForSentry: e,
});
}

return `FunctionReference<"${udfType}", "${visibility}", ${argsType}, ${returnsType}>`;
Expand Down Expand Up @@ -444,7 +461,7 @@ function validatorToType(validator: ConvexValidator): string {
} else if (validator.type === "array") {
return `Array<${validatorToType(validator.value)}>`;
} else if (validator.type === "record") {
return `Record<${validatorToType(validator.keys)}, ${validatorToType(validator.values)}>`;
return `Record<${validatorToType(validator.keys)}, ${validatorToType(validator.values.fieldType)}>`;
} else if (validator.type === "union") {
return validator.value.map(validatorToType).join(" | ");
} else if (validator.type === "object") {
Expand Down
11 changes: 9 additions & 2 deletions src/cli/lib/deployApi/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const baseConvexValidator = z.discriminatedUnion("type", [
export type ConvexValidator =
| z.infer<typeof baseConvexValidator>
| { type: "array"; value: ConvexValidator }
| { type: "record"; keys: ConvexValidator; values: ConvexValidator }
| {
type: "record";
keys: ConvexValidator;
values: { fieldType: ConvexValidator; optional: false };
}
| { type: "union"; value: ConvexValidator[] }
| {
type: "object";
Expand All @@ -28,7 +32,10 @@ export const convexValidator: z.ZodType<ConvexValidator> = z.lazy(() =>
looseObject({
type: z.literal("record"),
keys: convexValidator,
values: convexValidator,
values: z.object({
fieldType: convexValidator,
optional: z.literal(false),
}),
}),
looseObject({
type: z.literal("union"),
Expand Down

0 comments on commit f798b18

Please sign in to comment.