From b401c9732c885a44ad2feac97ce681473fa7a840 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 25 Apr 2020 09:33:15 +0200 Subject: [PATCH] Add minLength and maxLength to Validator --- src/db/Validator.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/db/Validator.ts b/src/db/Validator.ts index e76ed9a..85ca6b8 100644 --- a/src/db/Validator.ts +++ b/src/db/Validator.ts @@ -86,6 +86,30 @@ export default class Validator { return this; } + /** + * @param minLength included + */ + public minLength(minLength: number): Validator { + this.addStep({ + verifyStep: val => (val).length >= minLength, + throw: () => new BadLengthValidationError(minLength, 1000000), + isFormat: true, + }); + return this; + } + + /** + * @param maxLength included + */ + public maxLength(maxLength: number): Validator { + this.addStep({ + verifyStep: val => (val).length <= maxLength, + throw: () => new BadLengthValidationError(0, maxLength), + isFormat: true, + }); + return this; + } + /** * @param minLength included * @param maxLength included