Strict object validation is handy for data exploration
I finally found a good use case for
z.object().strict() in Zod.
z.object() ignores unknown keys (= object keys not specified in a Zod schema) by default.
Unknown keys can be disallowed with Zod's
.strict() be useful?
From a GitHub comment by Kamil Trebunia:
For consuming APIs I would almost always pick
z.object, but for data exploration as I am doing now
z.strictObjectis necessary and I am glad I found about it.
So: if you are exploring what data a poorly documented API returns,
you can use
z.object().strict() (or the shorthand
to avoid missing any keys;
unspecified keys will make Zod throw an error,
so you can adjust the Zod schema accordingly.
.deepStrict() method in Zod,
but it's easy to search for
.object( and replace all occurrences with