Skip to content

DTO Schema

buildFieldSchemaFromDto(dtoClass) reads Swagger metadata from DTO classes and returns a FieldSchema.

ts
class UserDTO {
  @ApiProperty()
  id!: string;

  @ApiProperty({ type: () => ProfileDTO })
  profile!: ProfileDTO;
}

const schema = buildFieldSchemaFromDto(UserDTO);

Swagger-decorated nested DTO classes become relation fields. Recursive DTO references are cut off with an empty nested schema to avoid infinite traversal.

Metadata Requirements

DTO schema generation depends on Swagger metadata:

  • Only properties decorated with @ApiProperty or compatible Swagger metadata are included.
  • DTO classes without Swagger property metadata produce an empty schema.
  • Nested DTO classes become relation schema nodes when their type metadata points to another Swagger-decorated DTO.
  • Arrays of DTOs are supported when Swagger exposes the array item type, for example with @ApiProperty({ type: () => [BookDTO] }).
  • Recursive references are cut off with an empty nested relation schema.

Use manual schemas with relation when the public response shape differs from the Swagger DTO metadata.

getDtoFields

ts
const fields = getDtoFields(UserDTO);

getDtoFields returns the Swagger-decorated property names for a DTO class. DTOs without Swagger property metadata return an empty array.

Manual Relations

ts
const schema = {
  id: true,
  profile: relation({
    firstName: true,
  }),
};

Use relation when a schema is not generated from DTO metadata.

Released under the MIT License.