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

Third decorator gets called twice #39574

Closed
fireflysemantics opened this issue Jul 12, 2020 · 2 comments
Closed

Third decorator gets called twice #39574

fireflysemantics opened this issue Jul 12, 2020 · 2 comments

Comments

@fireflysemantics
Copy link

fireflysemantics commented Jul 12, 2020

Here's a stackblitz demo:

https://stackblitz.com/edit/typescript-fs-validator-typescript-issue-reported?file=index.ts

class Person {
    @IsValueIn(['PETER', 'JAMES'])
    @IsAlpha()
    @IsDefined()
    public name:string;
}

When this demo is run it logs the following:

Error in /turbo_modules/@fireflysemantics/validator@3.0.10/bundles/fireflysemantics-validator.umd.js (194:21)
The ValidationContainer
already contains context with signature IsValueIn_Person_name.

This is because the runtime decorator @IsValueIn(['PETER', 'JAMES']) is called twice.

I've added logging statements to the @fireflysemantics/validator library such that this can be seen more clearly:

https://github.com/fireflysemantics/validator/blob/master/projects/validator/src/lib/ValidationContainer.ts#L61

That is the function that gets called by the decorator.

  /**
   * @param target Add a ValidationContext instance.
   * @throws Error if attempting to add a ValidationContext with a signature that duplicates that of an instance already contained.
   * 
   * If an exception thrown it indicates that a duplicate class definition exist
   * in the runtime.  In other words the same class definition is loaded more
   * than once because it exists in multiple files.
   * 
   */
  public static addValidationContext(target: ValidationContext): void {

    const key: string = getPropertyKey(
      target.target.name,
      target.propertyName
    );

    console.log("The property key is: ", key)
    console.log("The target signature is: ", target.getSignature())

Expected behavior:
These statements should only execute one time for each decorator:

    console.log("The property key is: ", key)
    console.log("The target signature is: ", target.getSignature())

Actual behavior:

However they get called twice for the @IsValueIn(['PETER', 'JAMES']) decorator.

Try commenting out @IsValueIn(['PETER', 'JAMES']) and you will see there are no exceptions.

Playground Link:

https://stackblitz.com/edit/typescript-fs-validator-typescript-issue-reported?file=index.ts

@fireflysemantics fireflysemantics changed the title Decorator gets called twice Third decorator gets called twice Jul 12, 2020
@fireflysemantics
Copy link
Author

Also asked about this on SO just in case it produces additional insight.

https://stackoverflow.com/questions/62866171/typescript-decorator-gets-called-twice

@fireflysemantics
Copy link
Author

Closing - Error in the decorator. See the SO Issue if you need an explanation:

https://stackoverflow.com/questions/62866171/typescript-decorator-gets-called-twice

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

No branches or pull requests

1 participant