erquhart
erquhart•6mo ago

Can someone help me understand the

Can someone help me understand the importance of the record validator? The benefit over the object validator is that it adds the ability to specify the types of keys, is that about right?
7 Replies
ari
ari•6mo ago
You might have some keyed data you want to store that is typed, but the keys aren't all known in advance. contrived example: mapping employee names to their favorite fruits
erquhart
erquhartOP•6mo ago
So it is specifically the keys, okay. That makes sense.
ampp
ampp•6mo ago
ownerTable: v.union(v.literal('users'), v.literal('members')),
ownerId: v.union(v.id('users'), v.id('members')),
ownerTable: v.union(v.literal('users'), v.literal('members')),
ownerId: v.union(v.id('users'), v.id('members')),
vs

owner: v.record(vOwnerTable, vOwnerId),

owner: v.record(vOwnerTable, vOwnerId),
This would be our main use. just cant use literal strings on the key?... Im still playing with stuff, our tables are currently Enums that resolve to strings
Matt Luo
Matt Luo•6mo ago
@ampp , would you say that a common real world use case is creating a separate table just for full text searching? My question doesn't map perfectly to your example, but if you are searching by the fullName of members, could you have a separate table that has a searchIndex on v.record(vOwnerTable)?
ampp
ampp•6mo ago
Yeah i haven't even thought about other use cases. I'm still "new" to typescript and records and spent zero time understanding records. I'm trying to sort out the typescript issues and how i'm going to refactor this. Assuming there is stuff i don't know i don't know. 😅
Argument of type 'VUnion<EntityEnum, any, "required", any>' is not assignable to parameter of type 'Validator<string, "required", any>'.
Type 'VUnion<EntityEnum, any, "required", any>' is not assignable to type 'VUnion<string, Validator<any, "required", any>[], "required", any>'.
Argument of type 'VUnion<EntityEnum, any, "required", any>' is not assignable to parameter of type 'Validator<string, "required", any>'.
Type 'VUnion<EntityEnum, any, "required", any>' is not assignable to type 'VUnion<string, Validator<any, "required", any>[], "required", any>'.
im passing it traditonal enums in a array through literals(), are unions just not able to be used for keys? i keep forgetting literals() always is a string even if its a id or a number, i'm curious if there is a way to strip the literals down to just a string and still have some benefits of the literals.
sshader
sshader•6mo ago
I'd personally use record a lot more for arg validators / return value validators vs. schema (although totally valid to use for schema). I often want to pass around mappings (e.g. Record<Id<"player">, { name: string, cards: Doc<"card"> }>>). IMO the main value here is that I can still get the validation + types for the values instead of having to use v.any() You can totally model everything with just objects or arrays of objects, but v.record enables some convenient JS patterns of passing around objects with arbitrary keys.
ampp
ampp•6mo ago
Does any of this allow for some improvements in convex helpers?

Did you find this page helpful?