From cd2ee1fe7f62c25486ab9c38e0493b9e2a74310a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20M=C3=BCller?= Date: Sun, 30 Jun 2024 22:27:37 +0200 Subject: [PATCH] Fix incorrectly passing validation when arrays are passed to type() --- src/structs/types.ts | 5 +++-- test/validation/type/invalid-array.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/validation/type/invalid-array.ts diff --git a/src/structs/types.ts b/src/structs/types.ts index 0587f78a..c1cfccfa 100644 --- a/src/structs/types.ts +++ b/src/structs/types.ts @@ -499,11 +499,12 @@ export function type( }, validator(value) { return ( - isObject(value) || `Expected an object, but received: ${print(value)}` + isNonArrayObject(value) || + `Expected an object, but received: ${print(value)}` ) }, coercer(value) { - return isObject(value) ? { ...value } : value + return isNonArrayObject(value) ? { ...value } : value }, }) } diff --git a/test/validation/type/invalid-array.ts b/test/validation/type/invalid-array.ts new file mode 100644 index 00000000..a0ec3972 --- /dev/null +++ b/test/validation/type/invalid-array.ts @@ -0,0 +1,15 @@ +import { type } from '../../../src' + +export const Struct = type({}) + +export const data = [] + +export const failures = [ + { + value: [], + type: 'type', + refinement: undefined, + path: [], + branch: [data], + }, +]