Make Excessive Object Properties Fail on Zod Validation

Excessive properties refer to any additional attributes that are not explicitly listed in an object schema. When creating an object based on a schema, it is important to ensure that only the defined properties are included.

In TypeScript, if an object is created with an extra property that isn't defined in the schema, TypeScript will not raise an error unless the type is explicitly annotated. When the type is annotated, TypeScript will issue an error indicating that the extra property is not allowed.

Zod provides a strict utility to enforce similar checks. This feature ensures that only specified properties are allowed in an object schema. When unnecessary properties are added at both the top level and within nested schemas, Zod will throw errors indicating unrecognized keys.

Adding an unrecognized property to a Zod schema results in an error message that specifies which keys are not recognized. Applying strict checks on nested schemas ensures that all levels of the data structure are validated correctly.

Share with a coworker

Transcript

[00:00] Sometimes we want to make sure that there are no excessive properties, which means that if we create an object of a certain type or schema, then the only allowed properties are the ones that are explicitly listed on the type, on the schema, or the interface or whatever we've got over here. So let's see how this works in TypeScript. Here we've got the booking schema which is inferred from our schema and we've got the properties with their corresponding types. Everything is as expected and we've got the example booking object. Now if I uncomment the unnecessary property that is not listed anywhere here, then TypeScript is not going to yell since there is no relation between this variable and the type itself.

[00:41] However, if I annotate the booking schema over here, then TypeScript starts yelling. Object literal may only specify known properties and unnecessary property does not exist in type and here comes the type literal. Same way if I uncomment this one over here then TypeScript is going to yell at the top level and here we've got the top level of our object with the very similar message. So this is how TypeScript excessive attribute check works And this works only for literals. So if you would run a function call over here and put the function call, or if you were using an existing variable, this error would not be thrown.

[01:27] So this is thrown only in terms of literals. So just putting the value here in place. So let's first run this file against ts-node and we will see that there is a compilation fail. So let's remove this one as well. Let's run it back again.

[01:44] So Now we can take a look how this works in Zod. So when we have an object schema, we can use the strict utility, which is doing a very, very similar thing. So let's see how this is going to work with ts-node. But first, I would need to remove this annotation over here so that I can add the unnecessary properties without making TypeScript compilation fail because that would not even execute the file and not execute Zod. So let's run this file.

[02:17] And what we can see is that there is no error since the Unnecessary property is not being put on this Level of object but it's on the guest detail schema. So let's also add the unnecessary property on the top level schema and let's run the file we would see that there is going to be an Unrecognized keys error code unrecognized keys in object unnecessary property Now we can also apply the strict on the nested schema, so on the guest detail schema. So again, this is the Z object, let's put the strict over here and let's rerun it so we can see that there are two errors with unrecognized keys. So essentially strict over a Zod object schema.