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
|