Validation
Navios Commander uses Zod schemas to validate command options. This ensures type safety and provides helpful error messages when options are invalid.
Basic Validation
Define a Zod schema and pass it to the @Command decorator:
String Validation
Basic String
String with Constraints
String with Custom Validation
Number Validation
Basic Number
Number with Constraints
Number with Precision
Boolean Validation
Array Validation
Basic Array
Array with Constraints
Array of Objects
Object Validation
Nested Objects
Optional Objects
Optional and Default Values
Optional Fields
Default Values
Optional with Default
Union Types
String Union
Type Union
Enum Validation
Date Validation
Custom Validation
refine()
Add custom validation logic:
superRefine()
For complex validation:
Option Descriptions with Meta
Zod v4 supports .meta() to add metadata to schema fields. Navios Commander uses this to display descriptions in help output:
Running export --help will display:
Meta Placement
The .meta() call should be placed last in the chain for best results:
Transform and Preprocess
Transform
Transform values during validation:
Preprocess
Preprocess values before validation:
Error Messages
Zod provides helpful error messages automatically:
Custom Error Messages
Complex Examples
User Creation
Database Migration
File Processing
Validation Errors
When validation fails, Zod throws a ZodError with detailed information:
The application automatically handles validation errors and displays helpful messages:
Best Practices
1. Always Define Schemas
2. Use Descriptive Error Messages
3. Provide Defaults for Optional Fields
4. Use Enums for Limited Choices
Next Steps
- Learn about commands in detail
- Explore dependency injection in commands
- Check out Zod documentation for more validation options