mrvdot
mrvdot•3mo ago

Action Validator from Schema

Hi, I feel like I'm missing something obvious, but I've searched the docs and discord, and reviewed a bit of the source code as well, so now I'm here 🙂 Is there a way to derive an argument validator in an action from the schema itself? For example, I have the following table:
wins: defineTable({
userId: v.id("users"),
description: v.string(),
winDate: v.number(),
})
wins: defineTable({
userId: v.id("users"),
description: v.string(),
winDate: v.number(),
})
And an action that receives wins as an argument:
export const generatePreview = action({
args: {
wins: v.array(v.object({
description: v.string(),
winDate: v.number(),
})),
// ...
export const generatePreview = action({
args: {
wins: v.array(v.object({
description: v.string(),
winDate: v.number(),
})),
// ...
In the action I've only called out description and winDate because that's all this action cares about, and most of the other type-safe systems I use just enforce "does it have what you need" and don't worry about "does it have extra stuff" in it. However, Convex throws a validation error every time I try to just pass win entries directly to this because it also has _id, _creationTime, etc. So, my question is two-fold: 1. (Ideal) Is there a way to just use the schema itself for the arg validator, something like wins: v.array(validatorFromDoc(Doc<"wins">))? - I've tried a few forms of this, but since the data model contains only types, nothing in there can be used as a value when passing to the validator methods 2. (Workable) Is there a way to say "don't worry about extra properties"? Something like:
v.object({
field1: v.string(),
field2: v.number(),
// We don't use anything else, but also don't care if it's passed in
[key: string]: any
})
v.object({
field1: v.string(),
field2: v.number(),
// We don't use anything else, but also don't care if it's passed in
[key: string]: any
})
Thanks!
2 Replies
Convex Bot
Convex Bot•3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
mrvdot
mrvdotOP•3mo ago
Ah, for anyone else who is curious, there's helpful examples at https://stack.convex.dev/argument-validation-without-repetition to do this
Argument Validation without Repetition
A few more advanced techniques & helpers to further reduce duplication and accelerate your Convex workflow.

Did you find this page helpful?