Validation Decorators
Define TypeScript input validation constraints for DTOs with AxilJS semantic decorators and generate consistent API validation metadata.
Validation Decorators
AxilJS validation decorators declare input constraints directly on DTO properties.
They describe the expected shape and constraints of incoming data as semantic metadata. A validation consumer can use this metadata to validate request payloads at runtime, while an OpenAPI consumer can use the same declarations to generate API schemas.
String Validators
Use string decorators to declare type, length, format, and pattern constraints.
These decorators allow a DTO to express common string requirements such as minimum length, maximum length, email format, URL format, UUID format, and regular-expression constraints.
Number Validators
Use numeric decorators to define numeric types and value boundaries.
For example, tMin(0) prevents values below zero, while tMax(10000) defines an upper boundary.
Type Validators
AxilJS also provides decorators for booleans, enumerations, optional properties, arrays, and nested DTOs.
These declarations make the expected DTO structure explicit and provide metadata that validation and schema-generation consumers can interpret.
Custom Validators
Use tCustom when a validation rule does not fit the built-in validators.
Custom validators let you express application-specific constraints while keeping the validation rule attached to the DTO property.
Full DTO Example
The decorators can be composed to describe a complete request payload.
This approach keeps validation requirements close to the data contract rather than scattering them across request handlers or infrastructure-specific validation configuration.
Runtime Validation and API Schemas
AxilJS uses the same decorator metadata for multiple consumers.
A validation consumer can read the declarations at runtime and validate incoming request bodies. An OpenAPI generator can consume the same metadata to produce API schemas.
This provides a single source of truth for DTO validation and API documentation:
The important distinction is that the decorators declare constraints; consumers determine how those constraints are enforced or represented.
Reference
| Decorator | Purpose |
|---|---|
tString | Declare a string constraint |
tNumber | Declare a numeric constraint |
tBoolean | Declare a boolean constraint |
tEmail | Validate email-formatted strings |
tUrl | Validate URL-formatted strings |
tUuid | Validate UUID-formatted strings |
tEnum | Restrict a value to declared options |
tPattern | Apply a regular-expression constraint |
tMin | Define a minimum numeric value |
tMax | Define a maximum numeric value |
tMinLength | Define a minimum string length |
tMaxLength | Define a maximum string length |
tOptional | Mark a property as optional |
tArray | Declare an array value |
tNested | Declare a nested DTO |
tCustom | Define a custom validation rule |
AxilJS validation decorators turn DTO constraints into reusable semantic metadata, allowing runtime validation and API schema generation to operate from the same declarations.