-
Notifications
You must be signed in to change notification settings - Fork 10
CON-1508: Decorator based arguments validation approach #105
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
Conversation
@Validate | ||
async getProjectDetails(@IsNotEmpty projectId: string): Promise<ProjectDto> { |
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.
@dimitrystd it's just an example of how it can be used (SUBM api class is in private sdk)
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.
Looks interesting. Just two concerns:
- It only works with simple types. If we pass an object of
Parameters
class then we can't validate it. Or need to write more code.
Using!
and strict compilation can give us similar results. - We implement just another validation package.
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.
We would need to refactor here and there to support strictNullChecks
mode.
export function IsNotEmpty( | ||
target: any, | ||
methodName: string, | ||
parameterIndex: number | ||
) { | ||
if (!target?.validations) { | ||
target.validations = []; | ||
} | ||
|
||
target.validations.push(`${methodName}:${parameterIndex}:${IsNotEmpty.name}`); | ||
} |
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.
Registers validation for argument (it's not possible to get value of the argument in an argument decorator which is weird)
function IsNotEmptyValidate( | ||
methodName: string, | ||
argumentIndex: number, | ||
argumentValue: any | ||
) { | ||
if (argumentValue === '' || argumentValue === null || argumentValue === undefined) { | ||
throw new Error(`Invalid empty argument at index ${argumentIndex} with "${argumentValue}" value in "${methodName}" method`); | ||
} | ||
} |
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.
Actual validation logic
for (const validation of target.validations) { | ||
const [methodName, argumentIndex, validatorName] = validation.split(":"); | ||
|
||
if (method.name === methodName) { | ||
switch (validatorName) { | ||
case IsNotEmpty.name: | ||
const argumentValue = args[argumentIndex]; | ||
|
||
IsNotEmptyValidate(methodName, argumentIndex, argumentValue); | ||
|
||
break; | ||
} | ||
} | ||
} |
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.
Walk through registered validations and execute them
We decided to move this approach directly to the class-validator lib (typestack/class-validator#2432). If it will be accepted there we will get back to this ticket. |
Arguments decorator-based validation approach:
IsNotEmpty
argument decorator "registers" validation in the target object (just an array of strings like<methodName>:<parameterIndex>:<validatorName>
)Validation
method decorator reads all registered validations and executes them.This scheme with 2 decorators instead of just one argument decorator is only needed because argument level decorator can't access actual argument value (it's weird)
Usage example:
As I get it I can't use
class-validator
lib because it's only for class property validations, not method arguments.