Skip to content

fix: handle symbols in constraintToString method with array #2026

Open
@adrienboulle

Description

@adrienboulle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions