Multi-type support design instructions
📖 Quick navigation
- Single Type Validation (this document)
- Union type validation - Use
types:syntax to do true cross-type joint validation
🎯 Design principles
schema-dsl implements multi-type support through the type-independent Builder mode.
core design
📊 Type support matrix
🔧 Implementation mechanism
1. Type analysis (DslBuilder constructor)
2. Method adaptation (method internal check type)
💡 String extended multi-type support
The String extension only supports string types, which is a design decision:
Why is it designed this way?
- Type Safety: Avoid calling string methods on numeric types
- Clear semantics:
'number:18-120'itself expresses constraints - Simplicity first: 80% of complex verifications are strings, focus on optimizing string experience
🎨 Recommended usage of various types
String type (supports chaining)
Numeric type (pure DSL)
Boolean type (pure DSL)
Date type (pure DSL)
Enumeration types (pure DSL)
Array types (pure DSL)
🚀 Expand new types
The current version gives priority to extending types by exposing runtime APIs, rather than requiring business parties to modify the internal DslAdapter / ErrorCodes source code.
Recommended entrance
Usage
📋 Type method compatibility matrix
Conditional validation: Use s.match() or s.if() static method.
Description:
- ✅ Fully supported
- ❌ Not supported (will be ignored or warned)
🎯 Best Practices
1. Choose expression according to type
2. String extension is only used for strings
3. Use custom for complex validation
💡 Summary
The multi-type support of schema-dsl adopts type-independent Builder + method intelligent adaptation design:
- Unified Entry: All types pass DslBuilder
- Type Aware: Methods internally check type compatibility
- Simple first: String extension focuses on strings (80% of complex scenarios)
- Progressive enhancement: Use DSL for simple, chained for complex, and custom for special
Design philosophy: Make the most common scenarios (string validation) the simplest, and keep the DSL simple for other types.
Corresponding sample file
Example entry: multi-type-support.ts Description: The recommended way to use an object to cover string, number, Boolean, date, array and enumeration fields at the same time, as well as the corresponding success/failure validation path.