🎨 Body Parser error

This commit is contained in:
Flam3rboy
2021-02-02 00:51:00 +01:00
parent 7998b39757
commit 8505d9279b
14 changed files with 143 additions and 36 deletions
+17 -3
View File
@@ -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}` }),
},
});
}
}
+3 -3
View File
@@ -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