Skip to content

Optional

Create an optional schema with optional(schema) or .optional().

use zod_rs::prelude::*;
use serde_json::json;
// Function form
let schema = optional(string());
assert!(schema.safe_parse(&json!(null)).is_ok());
assert!(schema.safe_parse(&json!("hello")).is_ok());
// Method chaining
let schema = string().optional();
assert!(schema.safe_parse(&json!(null)).is_ok());

Optional schemas accept null in addition to the wrapped type. Non-null values are validated against the inner schema.

let schema = optional(string().email());
assert!(schema.safe_parse(&json!(null)).is_ok());
assert!(schema.safe_parse(&json!("user@example.com")).is_ok());
assert!(schema.safe_parse(&json!("not-email")).is_err());

Use optional_field on object schemas for optional fields:

let schema = object()
.field("name", string())
.optional_field("bio", string());

Option<T> fields are automatically treated as optional:

#[derive(ZodSchema)]
struct UserProfile {
name: String,
bio: Option<String>, // automatically optional
avatar: Option<String>, // automatically optional
}