@ApiPropertyString
Provides comprehensive Swagger, validation, and transformation support for string DTO fields. Handles dozens of formats (email, URL, UUID, password, timezone, slug, etc.), arrays, regex patterns, and response serialization.
Signature
@ApiPropertyString(properties: TApiPropertyStringProperties)Key options
| Option | Description |
|---|---|
format | Required EApiPropertyStringType value (STRING, EMAIL, URL, PASSWORD, UUID, REGEXP, PHONE_NUMBER, etc.). Determines validators such as IsEmail, IsUrl, IsUUID. |
pattern | Stringified RegExp ("/^[a-z]+$/"). Used by Matches and validated against exampleValue. |
minLength / maxLength | Enforced with Length. |
isArray, minItems, maxItems, isUniqueItems | Enable string arrays with IsArray, ArrayMinSize, ArrayMaxSize, ArrayNotEmpty. |
isRequired, isNullable, isResponse, isExpose | Control optionality, Swagger metadata, and serialization. |
exampleValue | Single value or array; validated against the regex/length constraints. |
Behavior
- Emits
@ApiPropertywith the correct format, pattern, and example. Arrays automatically wrap the schema initems. - Adds format-specific validators pulled from
class-validator(e.g.,IsEmail,IsPhoneNumber,IsStrongPassword,IsJWT,IsHexColor). - Registers request validators (
Matches,Length,IsOptional, array validators) unlessisResponseistrue. - Applies
ApiResponseProperty,Expose, orExcludeon response DTOs.
Example
class UserDto {
@ApiPropertyString({
entity: () => UserEntity,
description: "Email address",
format: EApiPropertyStringType.EMAIL,
minLength: 5,
maxLength: 255,
pattern: "/^[\\w.-]+@([\\w-]+\\.)+[\\w-]{2,}$/",
isRequired: true,
})
email!: string;
@ApiPropertyString({
entity: () => UserEntity,
description: "Tags",
format: EApiPropertyStringType.SLUG,
isArray: true,
minItems: 1,
maxItems: 5,
})
tags!: Array<string>;
}Related resources
Last updated on