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

Initial naive implementation for method arguments decorator validation #2432

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

PavelLoparev
Copy link

@PavelLoparev PavelLoparev commented Mar 12, 2024

Description

This PR introduces method's arguments validation.

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

#115

This is a naive implementation that can be extended if needed.

How it works:

  1. IsNotEmptyArgument argument decorator "registers" validation in the target object (just an array of strings like <methodName>:<parameterIndex>:<validatorName>)
  2. ValidateArguments 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 {
    @ValidateArguments
    async myMethod(@IsNotEmptyArgument test: string): Promise<void> {
        ...
    }
}

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

Successfully merging this pull request may close these issues.

None yet

1 participant