question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Make object schemas nonstrict by default

See original GitHub issue

Zod’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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
chrbalacommented, Aug 9, 2020

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.

2reactions
chrbalacommented, Aug 1, 2020

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.

const Obj = z.object({
  key: z.string(),
});

Obj.parse({key: 'value', key2: 'value2'})  // could return { key: 'value' }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found