Enum function documentation
📖 Overview
The Enum feature allows you to define fields that can only take on a specific set of values. schema-dsl supports multiple enumeration types and syntax formats.
✨ Core Features
- ✅ String Enumeration - Limit the range of string values
- ✅ Boolean Enumeration - true/false Boolean values
- ✅ Number Enumeration - Numeric Value Limitation
- ✅ Integer Enumeration - Integer value limitation (no decimals allowed)
- ✅ Decimal Enumeration - supports decimal values
- ✅ Automatic Type Recognition - Intelligent recognition of enumeration types
- ✅ Multiple syntax - short and full form
- ✅ Required Support - Enum fields can be marked as required
- ✅ Chained API - supports.label() and.messages()
📝 Grammar format
basic grammar
Supported enumeration types
🚀 Quick start
1. String enumeration
2. Boolean enumeration
3. Number enumeration
📚 Detailed usage
Required enumeration
Explicitly specify the type
Chained API
enum in array
Enums in nested objects
🎯 Practical application scenarios
User management system
Order management
Content management
⚙️ Advanced features
default value
Custom error message
Use the 'enum' key uniformly (recommended)✅
All enumeration types use 'enum' to define error messages, which is the simplest and most direct:
Description:
- ✅ All enumeration types use the
'enum'key uniformly - ✅ Simple and easy to understand, no need to remember different types of key names
- ✅ Automatically infer the type, users only care about the error message content
Advanced usage: Customize messages by type (optional)
If you need to customize different error messages for different types of enumerations, you can use the type.enum format:
Priority: type.enum > enum > Global default
Suggestion: In 99% of scenarios, it is enough to use 'enum' directly ✅
Multi-language support
🔍 Type identification rules
Automatic recognition logic
Explicitly specify the type
❌ Error handling
Invalid enumeration value
type mismatch
Enumerations are type-validated automatically:
Description:
- Type errors are automatically validated by the schema (such as passing a string to a numeric enumeration)
- Enumeration range errors use
'enum'error message
📊 Performance
Excellent enumeration validation performance:
🔄 Compatibility
Compatible with older versions
Does not affect other types
📖 Related documents
Corresponding sample file
Example entry: enum.ts Description: Overrides the success/failure path for string, number, boolean, and array element enumerations and displays a custom enumeration error message.