Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

PavelLoparev
Copy link
Contributor

@PavelLoparev PavelLoparev commented Mar 7, 2024

Arguments decorator-based validation approach:

  1. IsNotEmpty argument decorator "registers" validation in the target object (just an array of strings like <methodName>:<parameterIndex>:<validatorName>)
  2. 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:

export class MyClass {
    @Validate
    async myMethod(@IsNotEmpty test: string): Promise<void> {
        ...
    }
}

As I get it I can't use class-validator lib because it's only for class property validations, not method arguments.

Comment on lines +14 to +15
@Validate
async getProjectDetails(@IsNotEmpty projectId: string): Promise<ProjectDto> {
Copy link
Contributor Author

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)

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Comment on lines +1 to +11
export function IsNotEmpty(
target: any,
methodName: string,
parameterIndex: number
) {
if (!target?.validations) {
target.validations = [];
}

target.validations.push(`${methodName}:${parameterIndex}:${IsNotEmpty.name}`);
}
Copy link
Contributor Author

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)

Comment on lines +13 to +21
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`);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actual validation logic

Comment on lines +28 to +41
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;
}
}
}
Copy link
Contributor Author

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

@PavelLoparev
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants