Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/decorator/string/Length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
13 changes: 13 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down