Skip to main content
Version: 1.12.1

Optional Field Control

Configure how optional Prisma fields are validated using different Zod patterns.

Use Cases

API with Null Values

When your API accepts explicit null values alongside undefined/omitted fields:

generator zod {
provider = "prisma-zod-generator"
optionalFieldBehavior = "nullish" # default
}

model User {
id Int @id
name String?
bio String?
}

Generated schema accepts all these patterns:

// All valid
{ id: 1, name: "John", bio: "Developer" }
{ id: 1, name: null, bio: undefined }
{ id: 1 } // name and bio omitted

Strict No-Null API

When you want to reject explicit null values:

generator zod {
provider = "prisma-zod-generator"
optionalFieldBehavior = "optional"
}

Generated validation:

// Valid
{ id: 1, name: "John" }
{ id: 1 } // name omitted

// Invalid - null rejected
{ id: 1, name: null } // ❌ Validation error

Always-Present Fields

When optional fields must always be included in requests:

generator zod {
provider = "prisma-zod-generator"
optionalFieldBehavior = "nullable"
}

Generated validation:

// Valid
{ id: 1, name: "John", bio: "Developer" }
{ id: 1, name: null, bio: null }

// Invalid - fields must be present
{ id: 1 } // ❌ Missing name and bio

Configuration Options

Generator Block

generator zod {
provider = "prisma-zod-generator"
optionalFieldBehavior = "nullish" | "optional" | "nullable"
}

JSON Config

{
"optionalFieldBehavior": "nullish"
}

Comparison Table

BehaviorZod OutputAccepts undefinedAccepts nullAllows Omitted
nullish.nullish()
optional.optional()
nullable.nullable()

Migration Example

Changing from the legacy .optional().nullable() pattern:

Before:

// Legacy behavior (equivalent to nullish)
name: z.string().optional().nullable()

After with explicit configuration:

// With optionalFieldBehavior = "nullish"
name: z.string().nullish()

// With optionalFieldBehavior = "optional"
name: z.string().optional()

// With optionalFieldBehavior = "nullable"
name: z.string().nullable()

Type Safety

All behaviors maintain compatibility with Prisma types:

import { Prisma } from '@prisma/client';

// Prisma type: { name?: string | null }
const data: Prisma.UserCreateInput = {
id: 1,
name: null // Prisma allows null
};

// All optionalFieldBehavior settings accept this
UserCreateInputSchema.parse(data); // ✅ Always works