Sentry Answers>JavaScript>

Why does Zod make all my schema fields optional?

Why does Zod make all my schema fields optional?

Matthew C.

The Problem

You are using Zod, the TypeScript-first schema validation library, in a project that uses TypeScript and wondering why Zod makes schema fields optional.

For example, you are using it to validate data structure from a login form. You’ve created an object schema for the expected structure of the user input data:

Click to Copy
const UserSchema = z.object({ username: z.string(), password: z.string(), }); type User = z.infer<typeof UserSchema>;

You expect all of the fields to be required, but when you look at the TypeScript type of the UserSchema, you see that the object properties are optional:

Click to Copy
type User = { username?: string; password?: string; };

This is unexpected as Zod makes all properties in an object schema required by default.

The Solution

Enable strict mode in your project’s tsconfig.json file and make sure that you are using TypeScript version 4.5+:

Click to Copy
{ "compilerOptions": { "strict": true } }

Enabling strict mode is a best practice for all TypeScript projects.

The strict flag adds extra type checks. Setting it to true enables all the strict mode family options, which includes the strictNullChecks option.

Note that if you have strictNullChecks set to true in your tsconfig.json file, all properties in your Zod object schemas will be optional even if strict is set to true. You can turn off strictNullChecks by setting it to false:

Click to Copy
strictNullChecks: false,

This turns off the strictNullChecks strict mode family check, but not the other strict mode family options. When strictNullChecks is set to false, null and undefined are ignored by TypeScript, leading to unexpected errors at runtime.

It’s best to set it to true as it gives null and undefined their own distinct types, which can help you find and prevent bugs: you’ll get a type error if you try to use null or undefined where a value is expected.

If you can’t change the strict mode or strictNullChecks flags, you can use the Zod min() validation method for each object value to make sure a value is added:

Click to Copy
const UserSchema = z.object({ username: z.string().min(1), password: z.string().min(1) });

You may have strict mode set to false for various reasons such as:

  • Your project has a legacy codebase with JavaScript code or non-strict TypeScript code.
  • Your project is migrating from a JavaScript codebase to a TypeScript codebase.
  • Your project is a prototype. Reduced type-checking can increase development speed.
  • Your project uses third-party libraries without type definitions.

To enable strict mode in your project, gradually turn it on by adding each strict mode family option one by one:

  • strictNullChecks
  • strictFunctionTypes
  • strictBindCallApply
  • strictPropertyInitialization
  • noImplicitAny
  • noImplicitThis
  • alwaysStrict
  • YoutubeHow Sentry.io saved me from disaster
  • ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns
  • SentryJavascript Error Monitoring & Tracing
  • ResourcesJavaScript Frontend Error Monitoring 101
  • Syntax.fmListen to the Syntax Podcast
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2025 • Sentry is a registered Trademark of Functional Software, Inc.