From c0fd1d1f2fe49efb26756f9c15d6bf31b9e80414 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 5 Apr 2021 21:24:36 +0200 Subject: :bug: fix body parser --- src/util/instanceOf.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/util/instanceOf.ts') diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts index 4f30bd46..eb2894cd 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts @@ -114,8 +114,6 @@ export function instanceOf( } if (typeof type === "object") { - if (typeof value !== "object") throw new FieldError("BASE_TYPE_OBJECT", req.t("common:field.BASE_TYPE_OBJECT")); - if (Array.isArray(type)) { if (!Array.isArray(value)) throw new FieldError("BASE_TYPE_ARRAY", req.t("common:field.BASE_TYPE_ARRAY")); if (!type.length) return true; // type array didn't specify any type @@ -154,6 +152,8 @@ export function instanceOf( throw new FieldError("BASE_TYPE_CLASS", req.t("common:field.BASE_TYPE_CLASS", { type })); } + if (typeof value !== "object") throw new FieldError("BASE_TYPE_OBJECT", req.t("common:field.BASE_TYPE_OBJECT")); + const diff = Object.keys(value).missing( Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x)) ); -- cgit 1.5.1 From 4f67c2c86e1a418c66ef5bab9712b2dc289d83fc Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 6 Apr 2021 04:05:46 +0200 Subject: :art: proper error message body parser --- src/util/instanceOf.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/util/instanceOf.ts') diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts index eb2894cd..869f56d4 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts @@ -148,8 +148,11 @@ export function instanceOf( }) ); } - if (value instanceof type) return true; - throw new FieldError("BASE_TYPE_CLASS", req.t("common:field.BASE_TYPE_CLASS", { type })); + try { + if (value instanceof type) return true; + } catch (error) { + throw new FieldError("BASE_TYPE_CLASS", req.t("common:field.BASE_TYPE_CLASS", { type })); + } } if (typeof value !== "object") throw new FieldError("BASE_TYPE_OBJECT", req.t("common:field.BASE_TYPE_OBJECT")); -- cgit 1.5.1 From 79894e6f27cf1ac116705db9b87e38456c16934a Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 6 Apr 2021 17:12:05 +0200 Subject: :bug: fix body parser empty error object --- src/util/instanceOf.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/util/instanceOf.ts') diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts index 869f56d4..b4a231ba 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts @@ -121,7 +121,8 @@ export function instanceOf( return ( value.every((val, i) => { errors[i] = {}; - return ( + + if ( instanceOf(type[0], val, { path: `${path}[${i}]`, optional, @@ -129,7 +130,12 @@ export function instanceOf( req, ref: { key: i, obj: value }, }) === true - ); + ) { + delete errors[i]; + return true; + } + + return false; }) || errors ); } else if (type?.constructor?.name != "Object") { @@ -170,7 +176,7 @@ export function instanceOf( if (OPTIONAL) newKey = newKey.slice(OPTIONAL_PREFIX.length); errors[newKey] = {}; - return ( + if ( instanceOf(type[key], value[newKey], { path: `${path}.${newKey}`, optional: OPTIONAL, @@ -178,7 +184,12 @@ export function instanceOf( req, ref: { key: newKey, obj: value }, }) === true - ); + ) { + delete errors[newKey]; + return true; + } + + return false; }) || errors ); } else if (typeof type === "number" || typeof type === "string" || typeof type === "boolean") { -- cgit 1.5.1