Return value validation question?
If i wanna use the return value validation in a function. And say that function returns an array of documents.
How may i define the return value, as the doc. I have a
myValidator
exported from a validators.ts
file that i can use like returns: v.array(videoValidator)
But that ends up erroring, since what im returning is the result of a .collect()
so basically what ends up coming through is system fields aswell.
I don't see anything regarding validating returned objects containing system fields in the docs.9 Replies
Any reason not to add the system fields to your validator, or make a variant that has the system fields?
I think that's what you want here, unless you don't want the system fields in your return value.
Eg., add
{ _id: v.id('tableName'), _creationTime: v.number() }
to the validator
It would be cool to have a v.doc('tableName')
validator thoughI want to polish this up and add it to convex-helpers, but here's an example of what you're talking about (not super battle tested, but it seems to work for one of my apps)
oohh that's awesome
could have a
docWithoutSystemFields
counterpart too
love thisThat's real nice! thanks for sharing! Indeed a better V haha
yeah quickly started missing something like that, when i began using the return validation earlier today
You could just pull this out from the code above and use it in your validators without replacing
v
:
So you could do:
That is great!
Big thanks to @sshader, I didn't realize the schema came with validators before reading her betterV implementation
Yeah totally. Really nice to learn that it does 👍
I didn't realize the schema came with validatorsYeah this was part of 1.13 (or at least making them easier to access) since we figured folks would want return value validators for
Doc<"foo">
.
Note that this betterV
can only be used in argument validators / return value validators, and not schema definitions themselves (or else we'd have a circular dependency). The reason v
is less powerful in some ways is because it's designed to work in both schema definitions + validators.