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

fix: handle symbols in constraintToString method with array #2026

Open
adrienboulle opened this issue Mar 29, 2023 · 2 comments · May be fixed by #2027
Open

fix: handle symbols in constraintToString method with array #2026

adrienboulle opened this issue Mar 29, 2023 · 2 comments · May be fixed by #2027
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@adrienboulle
Copy link
Contributor

adrienboulle commented Mar 29, 2023

Description

When implementing custom validators and using array of symbols as constraints, an error is raised when the error message is generated for a failed validation.

Minimal code-snippet showcasing the problem

const mySymbol = Symbol('mySymbol');

function IsSameType(property: unknown, validationOptions?: ValidationOptions) {
  return function (object: object, propertyName: string): void {
    registerDecorator({
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      constraints: [property],
      validator: {
        validate(value: any, args: ValidationArguments) {
          return args.constraints[0].some(t => typeof value === typeof t);
        },
        defaultMessage: buildMessage(
          eachPrefix + '$property must be of type ' + properties.map(property => typeof property).join(' or '),
          validationOptions
        ),
      },
    });
  };
}

class MyClass {
  @IsOneOfTypes([mySymbol])
  property: symbol;
}

const model = new MyClass();

// this will raise an error when generating the message
validator.validate(model)

#2027

Expected behavior
No error should be raised when generating a failed validation message.

Actual behavior
An error is raised when generating a failed validation message.

@adrienboulle adrienboulle added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Mar 29, 2023
@adrienboulle adrienboulle linked a pull request Mar 29, 2023 that will close this issue
6 tasks
@braaar
Copy link
Member

braaar commented Apr 9, 2024

We have yet to take a look at this issue. Please be patient.

If others are having the same problem, please chime in or use reactions to show that.

@braaar
Copy link
Member

braaar commented Apr 17, 2024

I can't get your reproduction to run on my end. This is what I have

import {
  ValidationArguments,
  ValidationOptions,
  buildMessage,
  registerDecorator,
} from "class-validator";
import * as validator from "class-validator";

const mySymbol = Symbol("mySymbol");

function IsSameType(property: unknown, validationOptions?: ValidationOptions) {
  return function (object: object, propertyName: string): void {
    registerDecorator({
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      constraints: [property],
      validator: {
        validate(value: any, args: ValidationArguments) {
          return args.constraints[0].some((t) => typeof value === typeof t);
        },
        defaultMessage: buildMessage(
          eachPrefix +
            "$property must be of type " +
            properties.map((property) => typeof property).join(" or "),
          validationOptions
        ),
      },
    });
  };
}

class MyClass {
  @IsOneOfTypes([mySymbol])
  property: symbol;
}

const model = new MyClass();

// this will raise an error when generating the message
validator.validate(model);

IsOneOfTypes appears to not be defined anywhere, and I'm seeing this type error in buildMessage.

Argument of type 'string' is not assignable to parameter of type '(eachPrefix: string, args?: ValidationArguments) => string'.

Could you correct this in your snippet so that I can see this issue for myself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

Successfully merging a pull request may close this issue.

2 participants