diff --git a/src/decorator/string/Length.ts b/src/decorator/string/Length.ts index 04427bdfb..c56fe44fe 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 4c266f02e..b59f7b39f 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', () => {