Skip to content

Commit

Permalink
Disable optional validators at the top level (#29355)
Browse files Browse the repository at this point in the history
Disable optional validators at the top-level for both argument and returns validators. This results in undefined behavior, so we should disallow it. This will not break existing users because it doesn't fail at run time.

GitOrigin-RevId: 8d0bfa440b75928e133f38eeb8c1bab9c5f060fb
  • Loading branch information
jordanhunt22 authored and Convex, Inc. committed Sep 3, 2024
1 parent e7b6853 commit 533f070
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
29 changes: 29 additions & 0 deletions src/server/registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,35 @@ describe("argument and return value validators can be objects or validators", ()
return { arg: arg };
},
}),

// This is syntx that we no longer want to support when typechecking because they result in undefined behavior.
mutationNoOptionalValidators: mutation({
// @ts-expect-error Optional validators are not supported at the top level
args: v.optional(v.string()),
// @ts-expect-error Optional validators are not supported at the top level
returns: v.optional(v.string()),
handler: () => {
return "result";
},
}),
queryNoOptionalValidators: query({
// @ts-expect-error Optional validators are not supported at the top level
args: v.optional(v.string()),
// @ts-expect-error Optional validators are not supported at the top level
returns: v.optional(v.string()),
handler: () => {
return "result";
},
}),
actionNoOptionalValidators: action({
// @ts-expect-error Optional validators are not supported at the top level
args: v.optional(v.string()),
// @ts-expect-error Optional validators are not supported at the top level
returns: v.optional(v.string()),
handler: () => {
return "result";
},
}),
};
type API = ApiFromModules<{ module: typeof module }>;

Expand Down
35 changes: 25 additions & 10 deletions src/server/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,13 @@ export type MutationBuilder<
Visibility extends FunctionVisibility,
> = {
<
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
ArgsValidator extends
| PropertyValidators
| Validator<any, "required", any>
| void,
ReturnsValidator extends
| PropertyValidators
| Validator<any, any, any>
| Validator<any, "required", any>
| void,
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
OneOrZeroArgs extends
Expand Down Expand Up @@ -680,10 +683,13 @@ export type MutationBuilderWithTable<
Visibility extends FunctionVisibility,
> = {
<
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
ArgsValidator extends
| PropertyValidators
| Validator<any, "required", any>
| void,
ReturnsValidator extends
| PropertyValidators
| Validator<any, any, any>
| Validator<any, "required", any>
| void,
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
OneOrZeroArgs extends
Expand Down Expand Up @@ -770,10 +776,13 @@ export type QueryBuilder<
Visibility extends FunctionVisibility,
> = {
<
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
ArgsValidator extends
| PropertyValidators
| Validator<any, "required", any>
| void,
ReturnsValidator extends
| PropertyValidators
| Validator<any, any, any>
| Validator<any, "required", any>
| void,
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
OneOrZeroArgs extends
Expand Down Expand Up @@ -856,10 +865,13 @@ export type QueryBuilderWithTable<
Visibility extends FunctionVisibility,
> = {
<
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
ArgsValidator extends
| PropertyValidators
| Validator<any, "required", any>
| void,
ReturnsValidator extends
| PropertyValidators
| Validator<any, any, any>
| Validator<any, "required", any>
| void,
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
OneOrZeroArgs extends
Expand Down Expand Up @@ -942,10 +954,13 @@ export type ActionBuilder<
Visibility extends FunctionVisibility,
> = {
<
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
ArgsValidator extends
| PropertyValidators
| Validator<any, "required", any>
| void,
ReturnsValidator extends
| PropertyValidators
| Validator<any, any, any>
| Validator<any, "required", any>
| void,
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
OneOrZeroArgs extends
Expand Down

0 comments on commit 533f070

Please sign in to comment.