summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-02-02 00:51:00 +0100
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-02-02 00:51:00 +0100
commit8505d9279b0855990619008b38094d17a0be2aeb (patch)
tree9ddd83d513297b005e2546b999a68e6a2f0a0a78 /src/util
parent:sparkles: [Route] Register (diff)
downloadserver-8505d9279b0855990619008b38094d17a0be2aeb.tar.xz
:art: Body Parser error
Diffstat (limited to 'src/util')
-rw-r--r--src/util/String.ts20
-rw-r--r--src/util/instanceOf.ts6
2 files changed, 20 insertions, 6 deletions
diff --git a/src/util/String.ts b/src/util/String.ts

index e7f014eb..fa93f1b7 100644 --- a/src/util/String.ts +++ b/src/util/String.ts
@@ -1,6 +1,20 @@ -export const WHITE_SPACE = /\s\s+/g; +import { Request } from "express"; +import { FieldError, FieldErrors } from "./instanceOf"; + +export const DOUBLE_WHITE_SPACE = /\s\s+/g; export const SPECIAL_CHAR = /[@#`:\r\n\t\f\v\p{C}]/gu; -export function trim(str: string) { - return str.replace(SPECIAL_CHAR, "").replace(WHITE_SPACE, " ").trim(); +export function trimSpecial(str: string) { + return str.replace(SPECIAL_CHAR, "").replace(DOUBLE_WHITE_SPACE, " ").trim(); +} + +export function checkLength(str: string, min: number, max: number, key: string, req: Request) { + if (str.length < min || str.length > max) { + throw FieldErrors({ + [key]: { + code: "BASE_TYPE_BAD_LENGTH", + message: req.t("common:field.BASE_TYPE_BAD_LENGTH", { length: `${min} - ${max}` }), + }, + }); + } } diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts
index 341374b7..5035e4c9 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts
@@ -95,7 +95,7 @@ export function instanceOf( return true; throw new FieldError("BASE_TYPE_CHOICES", t("common:field.BASE_TYPE_CHOICES", { types: type.types })); case Email: - if ((<Email>type).check()) return true; + if (new Email(value).check()) return true; throw new FieldError("EMAIL_TYPE_INVALID_EMAIL", t("common:field.EMAIL_TYPE_INVALID_EMAIL")); case Date: value = new Date(value); @@ -143,13 +143,13 @@ export function instanceOf( let newKey = key; const OPTIONAL = key.startsWith(OPTIONAL_PREFIX); if (OPTIONAL) newKey = newKey.slice(OPTIONAL_PREFIX.length); - errors[key] = {}; + errors[newKey] = {}; return ( instanceOf(type[key], value[newKey], { path: `${path}.${newKey}`, optional: OPTIONAL, - errors: errors[key], + errors: errors[newKey], t, ref: { key: newKey, obj: value }, }) === true