Skip to Content

@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

OptionDescription
formatRequired EApiPropertyStringType value (STRING, EMAIL, URL, PASSWORD, UUID, REGEXP, PHONE_NUMBER, etc.). Determines validators such as IsEmail, IsUrl, IsUUID.
patternStringified RegExp ("/^[a-z]+$/"). Used by Matches and validated against exampleValue.
minLength / maxLengthEnforced with Length.
isArray, minItems, maxItems, isUniqueItemsEnable string arrays with IsArray, ArrayMinSize, ArrayMaxSize, ArrayNotEmpty.
isRequired, isNullable, isResponse, isExposeControl optionality, Swagger metadata, and serialization.
exampleValueSingle value or array; validated against the regex/length constraints.

Behavior

  • Emits @ApiProperty with the correct format, pattern, and example. Arrays automatically wrap the schema in items.
  • Adds format-specific validators pulled from class-validator (e.g., IsEmail, IsPhoneNumber, IsStrongPassword, IsJWT, IsHexColor).
  • Registers request validators (Matches, Length, IsOptional, array validators) unless isResponse is true.
  • Applies ApiResponseProperty, Expose, or Exclude on 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>; }
Last updated on