panda - how would i go about validating a datab...

how would i go about validating a database column that is typed as JSON and can have any amount of values in it:
// These work
{
"foo": 1,
"bar": 3
}
{
"foo": 1
}
// This does not work
{
"bang": "1233"
}
// These work
{
"foo": 1,
"bar": 3
}
{
"foo": 1
}
// This does not work
{
"bang": "1233"
}
Solution:
they keys will always be strings and the values will always be numbers, so im assuming i would just do z.record(z.number)?
Jump to solution
4 Replies
Scott Trinh
Scott Trinh•7mo ago
If it's truly dynamic, you can use z.any or z.unknown to type that property. Or if you know roughly that it'll be a JSON object, but don't know the shape, maybe z.record(z.unknown) (Which is Record<string, unknown>) is a slightly easier type to deal with downstream, but that'll break if it ends up being other valid JSON values like an array, null, or a bare JSON primitive like number or string, etc.
Solution
𓆩panda𓆪
they keys will always be strings and the values will always be numbers, so im assuming i would just do z.record(z.number)?
Scott Trinh
Scott Trinh•7mo ago
ahh, yep!
𓆩panda𓆪
awesome, thanks so much!
Want results from more Discord servers?
Add your server
More Posts