Maximum allowed length (default: 255)
Configured optional text validation schema
// Optional fields with different length limits
const MetadataSchema = z.object({
title: TextFieldSchema(1, 100), // Required
subtitle: OptionalTextFieldSchema(200), // Optional, max 200 chars
description: OptionalTextFieldSchema(1000), // Optional, max 1000 chars
notes: OptionalTextFieldSchema(500) // Optional, max 500 chars
});
// User profile with optional information
const UserProfileSchema = z.object({
username: UsernameSchema, // Required
displayName: OptionalTextFieldSchema(100), // Optional display name
bio: OptionalTextFieldSchema(500), // Optional biography
website: UrlSchema.optional(), // Optional website
location: OptionalTextFieldSchema(100) // Optional location
});
// Content with optional metadata
const LessonSchema = z.object({
title: TextFieldSchema(1, 200), // Required title
content: TextFieldSchema(50, 5000), // Required content
summary: OptionalTextFieldSchema(300), // Optional summary
teacherNotes: OptionalTextFieldSchema(1000) // Optional teacher notes
});
Optional text field validation schema factory function
Factory function that creates optional text field validation schemas with configurable maximum length constraints. This function is designed for optional fields that don't require content but should be validated when provided, maintaining data quality while supporting flexible data entry.
The optional nature allows fields to be omitted entirely while still enforcing length limits when values are provided. This pattern is common for profile information, metadata fields, and supplementary content that enhances but doesn't define the core data structure.