-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Extend ValidateBody from typestack #755
Conversation
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', () => { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this 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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect 👍
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