-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix system udf missing arg validators bug (#23749)
The first change is to move `convex/server` to `convex/_system/server` so it gets covered by [this](https://github.com/get-convex/convex/blob/1486eb4f6429ed120a57c0ffd742e0cccd0fb202/crates/isolate/build.rs#L106), alternatively we could broaden that. The second change is adding arg validators to a couple `mutationGeneric`s that were legitimately missing them. Confusingly, we were getting this error at import time so it looked like they were coming from other functions in the same file. GitOrigin-RevId: 24bce4fbc0a8ed6207b310fed7ffb99830809ca0
- Loading branch information
Showing
13 changed files
with
154 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
npm-packages/system-udfs/convex/_system/frontend/addDocument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
npm-packages/system-udfs/convex/_system/frontend/clearTablePage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
npm-packages/system-udfs/convex/_system/frontend/createTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
npm-packages/system-udfs/convex/_system/frontend/deleteDocuments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
npm-packages/system-udfs/convex/_system/frontend/patchDocumentsFields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
npm-packages/system-udfs/convex/_system/frontend/replaceDocument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Validation is not required for REPL functions | ||
// eslint-disable-next-line no-restricted-imports | ||
import { query, internalQuery } from "../../_generated/server"; | ||
|
||
export { query, internalQuery }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Argument-validated versions of wrappers for use in system UDFs necessary | ||
// because system UDFs are not analyzed. | ||
|
||
import { Validator } from "convex/values"; | ||
// This is where the alternatives are defined | ||
import { | ||
// eslint-disable-next-line no-restricted-imports | ||
query as baseQuery, | ||
// eslint-disable-next-line no-restricted-imports | ||
mutation as baseMutation, | ||
// eslint-disable-next-line no-restricted-imports | ||
action as baseAction, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalQuery as baseInternalQuery, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalMutation as baseInternalMutation, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalAction as baseInternalAction, | ||
} from "../_generated/server"; | ||
import { | ||
// eslint-disable-next-line no-restricted-imports | ||
queryGeneric as baseQueryGeneric, | ||
// eslint-disable-next-line no-restricted-imports | ||
mutationGeneric as baseMutationGeneric, | ||
// eslint-disable-next-line no-restricted-imports | ||
actionGeneric as baseActionGeneric, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalQueryGeneric as baseInternalQueryGeneric, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalMutationGeneric as baseInternalMutationGeneric, | ||
// eslint-disable-next-line no-restricted-imports | ||
internalActionGeneric as baseInternalActionGeneric, | ||
} from "convex/server"; | ||
|
||
import { DefaultFunctionArgs } from "convex/server"; | ||
import { performOp } from "../syscall"; | ||
|
||
type FunctionDefinition = { | ||
args: Record<string, Validator<any, boolean>>; | ||
handler: (ctx: any, args: DefaultFunctionArgs) => any; | ||
}; | ||
|
||
type WrappedFunctionDefinition = { | ||
args: Record<string, Validator<any, boolean>>; | ||
handler: (ctx: any, args: DefaultFunctionArgs) => any; | ||
exportArgs(): string; | ||
}; | ||
|
||
type Wrapper = (def: FunctionDefinition) => WrappedFunctionDefinition; | ||
|
||
function withArgsValidated<T>(wrapper: T): T { | ||
return ((functionDefinition: FunctionDefinition) => { | ||
if (!("args" in functionDefinition)) { | ||
throw new Error("args validator required for system udf"); | ||
} | ||
const wrap: Wrapper = wrapper as Wrapper; | ||
const func = wrap({ | ||
args: functionDefinition.args, | ||
handler: () => {}, | ||
}); | ||
const argsValidatorJson = func.exportArgs(); | ||
return wrap({ | ||
args: functionDefinition.args, | ||
handler: async (ctx: any, args: any) => { | ||
const result = await performOp("validateArgs", argsValidatorJson, args); | ||
if (!result.valid) { | ||
throw new Error(result.message); | ||
} | ||
return functionDefinition.handler(ctx, args); | ||
}, | ||
}); | ||
}) as T; | ||
} | ||
|
||
export const queryGeneric = withArgsValidated(baseQueryGeneric); | ||
export const mutationGeneric = withArgsValidated(baseMutationGeneric); | ||
export const actionGeneric = withArgsValidated(baseActionGeneric); | ||
export const internalQueryGeneric = withArgsValidated(baseInternalQueryGeneric); | ||
export const internalMutationGeneric = withArgsValidated( | ||
baseInternalMutationGeneric, | ||
); | ||
export const internalActionGeneric = withArgsValidated( | ||
baseInternalActionGeneric, | ||
); | ||
|
||
// Specific to this schema. | ||
export const query = withArgsValidated(baseQuery); | ||
export const mutation = withArgsValidated(baseMutation); | ||
export const action = withArgsValidated(baseAction); | ||
export const internalQuery = withArgsValidated(baseInternalQuery); | ||
export const internalMutation = withArgsValidated(baseInternalMutation); | ||
export const internalAction = withArgsValidated(baseInternalAction); |