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
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
So it is specifically the keys, okay. That makes sense.
vs
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
@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)?
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. 😅
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.
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.Does any of this allow for some improvements in convex helpers?