aladov3.13.0

Docs/DTOs

@documentProperty

Adds OpenAPI/Swagger metadata to a property.

A field's schema comes from its validator (3.9.22): a validator built with validate describes its rules, so a field validated by validate.string().email().required().build() is documented as { type: 'string', format: 'email' } and listed as required, with no @documentProperty. @documentProperty adds description and example, and its schema (optional since 3.9.22) puts its keys on top of the derived ones.

TypeScript
import { documentProperty, validate, validateProperty } from 'alado';

class UserDto {
  @validateProperty({
    handler: validate.string().min(2).max(50).required().build(),
    error: { statusCode: 400, message: 'Invalid name' },
  })
  @documentProperty({
    description: 'The user display name',
    example: 'John Doe',
  })
  name: string = 'John Doe';
}

name is documented as { type: 'string', minLength: 2, maxLength: 50, description: 'The user display name', example: 'John Doe' } and listed in the body's required.

From validator rules to OpenAPI

Validator rule OpenAPI
string(), boolean(), array(), object() type
number() / integer(), bigint() type: 'number' / 'integer'
min(n), max(n), length(n) minLength / maxLength on a string, minimum / maximum on a number
positive(), negative() minimum: 0 / maximum: 0 with exclusiveMinimum / exclusiveMaximum: true
email(), uuid(), url() format: 'email' / 'uuid' / 'uri'
regex(p), objectId() pattern (further patterns in allOf)
enum(...) enum (with null when nullable())
nullable() nullable: true
items(v), minItems(n), maxItems(n), unique() items (from v), minItems, maxItems, uniqueItems
shape(s) properties, required, additionalProperties: false unless allowUnknownKeys
minKeys(n), maxKeys(n) minProperties, maxProperties
or(...keys), xor(...keys) anyOf / oneOf of required groups, in allOf

min() and max() are read for the type of the chain, whatever order it is written in. Rules OpenAPI cannot express add nothing: custom(), when(), match(), trim(), and a regex() with the i, m or s flag. A handler that is not built with validate gives an empty schema (any value; a path parameter is documented as { type: 'string' }): describe such a field with @documentProperty({ schema }). Before 3.9.21 a header or query field without @documentProperty had no schema (an invalid document), and such a body field was left out.

PropertyDefinitionSchema

The keys schema accepts:

TypeScript
interface PropertyDefinitionSchema {
  type?: string; // 'string' | 'number' | 'boolean' | 'object' | 'array'
  format?: string; // 'email' | 'uuid' | 'date-time' | ...
  default?: any;
  nullable?: boolean;
  enum?: any[];
  minimum?: number;
  maximum?: number;
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  items?: PropertyDefinitionSchema; // for arrays
  properties?: Record<string, PropertyDefinitionSchema>; // for objects
  oneOf?: PropertyDefinitionSchema[];
  anyOf?: PropertyDefinitionSchema[];
  allOf?: PropertyDefinitionSchema[];
}