object but not array

how do we declare a schema that only accepts plain json object like {} or {some: "key"} but not []?
5 Replies
TaQuanMinhLong
TaQuanMinhLongOP2mo ago
this still accepts
TaQuanMinhLong
TaQuanMinhLongOP2mo ago
No description
TizzySaurus
TizzySaurus2mo ago
I guess
const Thing = type("Record<string, unknown>")
const Thing = type("Record<string, unknown>")
Changing unknown to be whatever type you want all the values to adhere to (unknown will allow anything, so if that's what you want then you can just leave) There's also type.Record("string", "unknown"), so you can use that if you prefer the syntax
TaQuanMinhLong
TaQuanMinhLongOP2mo ago
Thanks, I'll give that a shot
ssalbdivad
ssalbdivad2mo ago
I would probably just write a narrow to check that it's not an array: https://arktype.io/docs/expressions#narrow What you want is essentially a negated type and typescript and can't be expressed well natively for the same reason with types. A custom predicate is probably the way to go.
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime

Did you find this page helpful?