# Fix Zod Making All Schema Fields Optional in TypeScript

> Enable strict mode in tsconfig.json to fix Zod object schemas showing all fields as optional, or use the min(1) validation method as a workaround

**URL:** https://sentry.io/answers/why-does-zod-make-all-my-schema-fields-optional/

---

## The Problem

You are using [Zod](https://zod.dev/), 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:

```ts
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:

```ts
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+:

```json
{
  "compilerOptions": {
    "strict": true
  }
}
```

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

The [`strict`](https://www.typescriptlang.org/tsconfig/#strict) flag adds extra type checks. Setting it to `true` enables all the `strict` mode family options, which includes the [`strictNullChecks`](https://www.typescriptlang.org/tsconfig/#strictNullChecks) option.

Note that if you have `strictNullChecks` explicitly set to `false` in your `tsconfig.json` file, all properties in your Zod object schemas will be optional even if `strict` is set to `true`. The explicit `false` overrides the `strict` flag for that one check:

```json
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:

```ts
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`

---

*Source: [sentry.io/answers/why-does-zod-make-all-my-schema-fields-optional/](https://sentry.io/answers/why-does-zod-make-all-my-schema-fields-optional/)*
