Skip to content

fix: handle symbols in constraintToString method with array #2027

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

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
689b9b1
fix: handle symbols in constraintToString method with array
adrienboulle Mar 29, 2023
5a5cf41
Merge branch 'develop' into fix/constraintToString
adrienboulle Apr 4, 2023
7e7e6f3
Merge branch 'develop' into fix/constraintToString
adrienboulle Apr 12, 2023
d1cabcb
Merge branch 'develop' into fix/constraintToString
adrienboulle May 4, 2023
8692cc5
Merge branch 'develop' into fix/constraintToString
adrienboulle May 15, 2023
5c28eda
Merge branch 'develop' into fix/constraintToString
adrienboulle Nov 11, 2023
4743c73
Merge branch 'develop' into fix/constraintToString
adrienboulle Nov 13, 2023
66469c2
Merge branch 'develop' into fix/constraintToString
adrienboulle Nov 25, 2023
63db161
Merge branch 'develop' into fix/constraintToString
adrienboulle Nov 30, 2023
d13ea82
Merge branch 'develop' into fix/constraintToString
adrienboulle Jan 22, 2024
149451b
Merge branch 'develop' into fix/constraintToString
adrienboulle Jan 25, 2024
32e40a4
Merge branch 'develop' into fix/constraintToString
adrienboulle Feb 5, 2024
df15c06
Merge branch 'develop' into fix/constraintToString
adrienboulle Feb 10, 2024
4d223a0
Merge branch 'develop' into fix/constraintToString
adrienboulle Feb 21, 2024
2dd311d
Merge branch 'develop' into fix/constraintToString
adrienboulle Mar 13, 2024
232cbaa
Merge branch 'develop' into fix/constraintToString
adrienboulle Apr 15, 2024
dfde868
Merge branch 'develop' into fix/constraintToString
adrienboulle May 12, 2024
832c06a
Merge branch 'develop' into fix/constraintToString
adrienboulle Sep 29, 2024
34984fb
Merge branch 'develop' into fix/constraintToString
braaar Jan 17, 2025
688eab2
Merge branch 'typestack:develop' into fix/constraintToString
adrienboulle Jun 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/validation/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValidationArguments } from './ValidationArguments';
*/
export function constraintToString(constraint: unknown): string {
if (Array.isArray(constraint)) {
return constraint.join(', ');
return constraint.map(c => constraintToString(c)).join(', ');
}

if (typeof constraint === 'symbol') {
Expand Down
39 changes: 37 additions & 2 deletions test/functional/custom-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,52 @@ describe('decorator with symbol constraint', () => {
};
}

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

class MyClass1 {
@IsSameType(mySymbol)
property: symbol;
}

class MyClass2 {
@IsOneOfTypes([mySymbol, 'str'])
property: symbol;
}

it('if property is not a symbol then it should fail', () => {
expect.assertions(2);
const model = new MyClass();
const model = new MyClass1();
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].constraints.customValidation).toEqual('property must be of type symbol');
});
});

it('if properties is not an array of symbols then it should fail', () => {
expect.assertions(2);
const model = new MyClass2();
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].constraints.customValidation).toEqual('property must be of type symbol or string');
});
});
});