Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ValidateBody from typestack #755

Merged

Conversation

AleksandrSl
Copy link
Contributor

@AleksandrSl AleksandrSl commented Jul 9, 2020

Add ability to provide "factory function" for clas against which body will be validated.

Issue

#752

Solution and steps

It turned out to be a little bit harder and flaky than ValidateBody from core. There is no robust difference between function and class (it's just a constructor function). The only way I found, use only arrow functions in this decorator if you want it to be generic, cause they cannot be used as constructors and don't have prototype. (https://stackoverflow.com/a/45759444/6540091).
Maybe it's worth adding separate decorator for this case, though I didn't came up with meaningfull name for it.

P.S. I didn't update the docs, cause I'm not sure of the solution.

Checklist

  • Add/update/check docs (code comments and docs/ folder).
  • Add/update/check tests.
  • Update/check the cli generators.

Add ability to provide "factory function" for cls against which body will be validated.
deepStrictEqual(actual2.body, {
message: 'The request body should be a valid JSON object or array.'
});
context('given class as argument', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I didn't change the tests, just generalized them a little bit to run same tests in separate context (to avoid duplication)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 👍

cls: Class | ((controller: any) => Class),
options: ValidateBodyOptions = {}
): HookDecorator {
return Hook(async function(this: any, ctx) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to function cause I want proper this from the call site

@AleksandrSl
Copy link
Contributor Author

Seems that failed test isn't connected to changes
image

@LoicPoullain LoicPoullain added this to Work In Progress in Issue tracking via automation Jul 14, 2020
@LoicPoullain LoicPoullain self-requested a review July 14, 2020 17:01
Copy link
Member

@LoicPoullain LoicPoullain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR really well-written. 👍

I took the liberty to add the documentation.

Nice trick on the prototype. It works well and I don't see a case where one would need to use a function () {} in ValidateBody.

deepStrictEqual(actual2.body, {
message: 'The request body should be a valid JSON object or array.'
});
context('given class as argument', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 👍

@LoicPoullain LoicPoullain changed the base branch from master to v1-10-0 July 16, 2020 14:03
@LoicPoullain LoicPoullain changed the base branch from v1-10-0 to master July 16, 2020 14:24
@LoicPoullain LoicPoullain changed the base branch from master to v1-10-0 July 16, 2020 14:24
@LoicPoullain LoicPoullain merged commit 54cc9bb into FoalTS:v1-10-0 Jul 16, 2020
Issue tracking automation moved this from Work In Progress to Done / Closed This Release Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Issue tracking
  
Done / Closed This Release
Development

Successfully merging this pull request may close these issues.

None yet

2 participants