How to generate simple schema using Swagger in NestJS?

102 views Asked by At

According to docs, in NestJS it is possible to generate schemas like this: enter image description here

export class NodeId {
  @ApiProperty({
    minimum: 40,
    maximum: 40,
    readOnly: true,
  })
  id: string;
}

export class Position {
  @ApiProperty({ readOnly: true, example: 0 })
  x: number;
  @ApiProperty({ readOnly: true, example: 0 })
  y: number;
}

export class Node {
  @ApiProperty()
  nodeId: NodeId;
  @ApiProperty()
  position: Position;
}

In yaml NodeId schema looks like this:

NodeId:
  type: object
  properties:
    id:
      type: string
      minimum: 40
      maximum: 40
      readOnly: true
  required:
    - id

Though, my ultimate goal is being able to produce simple schema for entities like Ids, which has to have a specific structure and could be reusable: enter image description here

This can be achieved in swagger itself (by editing yaml file manually) like this:

NodeId:
  type: string
  minimum: 40
  maximum: 40
  readOnly: true

I'm seeking for the same in NestJS. Sadly, there was no such example in NestJS docs. I'm wondering whether it is possible to generate this kind of schema by swagger module means.

0

There are 0 answers