How to generate json schema with typebox with inversify routes?

330 views Asked by At

I am using inversify express util with swagger-express-ts to add the route definitiion at route

@ApiOperationPost({
  description: 'Create A User',
  summary: 'Create a new user',
  parameters: {
    body: {
      description: 'Create New Expense',
      required: true,
      model: 'User'
    }
  },
  path: '/',
  responses: apiOperationsResponse()
})
@httpPost('/')
public async createUser(request: Request, response: Response, next: NextFunction) {
  const handler = await this._getUserController.executeHandler();

  return handler(request, response, next);
}

Earlier I was using the class dto with class-validator and passing the models as

@ApiModel({
  description: 'User description',
  name: 'User'
})
export class UserDto {
  @ApiModelProperty({
    description: 'user id'
  })
  @IsString()
  @IsOptional()
  userId?: string;
}

I have now changed the class validators with typebox and now need to pass the models to the API definitions,

Can't find a way to utilise the typebox types for the same as it can't find models.

I tried to pass the models in swagger midlleware

 app.use(
    swagger.express({
      definition: {
        info: {
          title: 'API Documentation',
          version: '1.0.0'
        },
        externalDocs: {
          url: `http://localhost:3007/`
        },
        basePath: '/api/v1/',
        models: {
          User: {
            description: 'some data',
            properties: {
              id: {
                required: true,
                type: 'integer'
              },
              name: {
                required: true,
                type: 'string'
              }
            }
          }
        }
      }
    })
  );

Tried this approach https://dev.to/thyphamdev/one-json-schema-rules-them-for-all-typescript-type-api-validation-openapi-doc-and-swagger-ui-3k3p

But the schema properties are different for swagger-express-ts

This works but I have to again type complete schema and not able to utilise the already created typebox schema and types.

Is there a way to use the typed models either in the middleware or directly in the swagger route definitions ?

0

There are 0 answers