FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.9k stars 140 forks source link

Is there a way to ValidatePathParam on a whole subcontroller? #1268

Closed ConorCorp closed 3 months ago

ConorCorp commented 3 months ago

Hey gang, Is there a way for me to ValidatePathParam in the VersionController? or just class wide in the ApproverController?

Basically, it would make a lot of sense if the versionId could be validated in the VersionController, and not in subcontrollers.

export class VersionController {
  subControllers = [controller('/:versionId/approver', ApproverController)];

 // ...other routes using /:versionId
}

export class ApproverController {
  @Get()
  @ValidatePathParam('versionId', { type: 'string', format: 'uuid' }) // <---- Make this class wide or in parent controller
  async getDocumentVersionApprovers(ctx: Context) {}
}
LoicPoullain commented 3 months ago

Hey @ConorCorp

You cannot move the hook to VersionController but you can decorate the ApproverController class with it. In this way, it will apply for all methods of the sub-controller:

export class VersionController {
  subControllers = [controller('/:versionId/approver', ApproverController)];

 // ...other routes using /:versionId
}

@ValidatePathParam('versionId', { type: 'string', format: 'uuid' })
export class ApproverController {
  @Get()
  async getDocumentVersionApprovers(ctx: Context) {}
}
ConorCorp commented 3 months ago

Ahhh that's lovely, thanks Loic. Keep up the good work!