RFC: Make object schemas nonstrict by default
See original GitHub issueZod’s policy of disallowing any unknown keys by default is confusing and probably not worth it. It also makes the inferred types incorrect:
const A = z.object({
a: z.string(),
});
const B = z.object({
b: z.string(),
});
const AB = z.intersection(A, B);
type AB = z.infer<typeof Teacher>;
// { a: string; b: string };
The actual inferred type here should be never because no data will properly pass validation.
If anyone has a reason why object schemas should be strict by default, speak now!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
1. User Information The userinfo subcomponent may consist of a user name and, optionally, scheme-specific information about how to gain authorization to access ......
Read more >Concise Binary Object Representation (CBOR) RFC 7049
Concise Binary Object Representation (CBOR) (RFC 7049, October 2013; ... These design goals make it different from earlier binary serializations such as ...
Read more >SJOT: Schemas for JSON Objects - Genivia
JSON schema is non-strict by default, meaning that all object properties are optional and any additional properties are permitted by default ...
Read more >OpenAPI Specification - Version 3.0.3 - Swagger
An OpenAPI document MAY be made up of a single document or be divided into ... Models are defined using the Schema Object,...
Read more >SJOT: Schemas for JSON Objects - GitHub
JSON schema is non-strict by default. By contrast, SJOT is strict by default since object properties are required by default. JSON schemas are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The primary thing I care about regarding this issue is that there’s a path to getting a clean object without erroneous keys. If you use zod to validate before a schemaless database, you might find out you have a bunch of unwanted fields if there aren’t good protections in place. Perhaps making this explicit like a
ZodSchema.parse(val, { clean: true })
which would allow for this.Even right now, this is a bit cumbersome because unused fields have to be manually removed. A value cleaner would make this nicer.
If object schemas become nonstrict by default (which generally makes sense to me), how would parsing work? I see some advantages in being able to give an object with extra keys, then parsing removes them.