mrvdotM
Convex Communityโ€ข8mo agoโ€ข
2 replies
mrvdot

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(),
})


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(),
    })),
    // ...


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
})


Thanks!
Was this page helpful?