Add minLength and maxLength to Validator

This commit is contained in:
Alice Gaudon 2020-04-25 09:33:15 +02:00
parent de777bc844
commit b401c9732c

View File

@ -86,6 +86,30 @@ export default class Validator<T> {
return this;
}
/**
* @param minLength included
*/
public minLength(minLength: number): Validator<T> {
this.addStep({
verifyStep: val => (<any>val).length >= minLength,
throw: () => new BadLengthValidationError(minLength, 1000000),
isFormat: true,
});
return this;
}
/**
* @param maxLength included
*/
public maxLength(maxLength: number): Validator<T> {
this.addStep({
verifyStep: val => (<any>val).length <= maxLength,
throw: () => new BadLengthValidationError(0, maxLength),
isFormat: true,
});
return this;
}
/**
* @param minLength included
* @param maxLength included