From 876a6f0be1d5ceb939943f8cb4bee584567799e5 Mon Sep 17 00:00:00 2001 From: rajanpanth Date: Sun, 9 Aug 2026 11:35:06 +0545 Subject: [PATCH] fix(Length): don't reference unset max/min constraint in error message --- src/decorator/string/Length.ts | 6 +++++- .../validation-functions-and-decorators.spec.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/decorator/string/Length.ts b/src/decorator/string/Length.ts index 04427bdfb7..c56fe44fed 100644 --- a/src/decorator/string/Length.ts +++ b/src/decorator/string/Length.ts @@ -26,7 +26,11 @@ export function Length(min: number, max?: number, validationOptions?: Validation defaultMessage: buildMessage((eachPrefix, args) => { const isMinLength = args?.constraints[0] !== null && args?.constraints[0] !== undefined; const isMaxLength = args?.constraints[1] !== null && args?.constraints[1] !== undefined; - if (isMinLength && (!args.value || args.value.length < args?.constraints[0])) { + if (isMinLength && !isMaxLength) { + return eachPrefix + '$property must be longer than or equal to $constraint1 characters'; + } else if (isMaxLength && !isMinLength) { + return eachPrefix + '$property must be shorter than or equal to $constraint2 characters'; + } else if (isMinLength && (!args.value || args.value.length < args?.constraints[0])) { return eachPrefix + '$property must be longer than or equal to $constraint1 characters'; } else if (isMaxLength && args.value.length > args?.constraints[1]) { return eachPrefix + '$property must be shorter than or equal to $constraint2 characters'; diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 4c266f02ee..b59f7b39ff 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -4267,6 +4267,19 @@ describe('Length', () => { const message = 'someProperty must be shorter than or equal to ' + constraintToString(constraint2) + ' characters'; return checkReturnedError(new MyClass(), ['aaaa', 'azzazza'], validationType, message); }); + + describe('when only min is specified and the value is not a string', () => { + class MinOnlyClass { + @Length(constraint1) + someProperty: any; + } + + it('should not reference the unset max constraint in the error message', () => { + const validationType = 'isLength'; + const message = 'someProperty must be longer than or equal to ' + constraintToString(constraint1) + ' characters'; + return checkReturnedError(new MinOnlyClass(), [123, true, { foo: 'bar' }], validationType, message); + }); + }); }); describe('MinLength', () => {