From 23787ae4b6a233ede97dd86401452f9047c12aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20M=C3=BCller?= Date: Fri, 3 Nov 2023 23:09:12 +0100 Subject: [PATCH] Allow frozen objects/arrays to be coerced (ianstormtaylor/superstruct#1151) * Update tuple coercer * Update record coercer * Add frozen objects/arrays tests --- src/structs/types.ts | 6 ++++++ test/validation/array/valid-frozen.ts | 9 +++++++++ test/validation/object/valid-frozen.ts | 18 ++++++++++++++++++ test/validation/record/valid-frozen.ts | 15 +++++++++++++++ test/validation/tuple/valid-frozen.ts | 9 +++++++++ test/validation/type/valid-frozen.ts | 18 ++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 test/validation/array/valid-frozen.ts create mode 100644 test/validation/object/valid-frozen.ts create mode 100644 test/validation/record/valid-frozen.ts create mode 100644 test/validation/tuple/valid-frozen.ts create mode 100644 test/validation/type/valid-frozen.ts diff --git a/src/structs/types.ts b/src/structs/types.ts index 8bcc0ef3..c4571185 100644 --- a/src/structs/types.ts +++ b/src/structs/types.ts @@ -366,6 +366,9 @@ export function record( isObject(value) || `Expected an object, but received: ${print(value)}` ) }, + coercer(value) { + return isObject(value) ? { ...value } : value + }, }) } @@ -453,6 +456,9 @@ export function tuple( `Expected an array, but received: ${print(value)}` ) }, + coercer(value) { + return Array.isArray(value) ? value.slice() : value + }, }) } diff --git a/test/validation/array/valid-frozen.ts b/test/validation/array/valid-frozen.ts new file mode 100644 index 00000000..814291fe --- /dev/null +++ b/test/validation/array/valid-frozen.ts @@ -0,0 +1,9 @@ +import { array, number } from '../../../src' + +export const Struct = array(number()) + +export const data = Object.freeze([1, 2, 3]) + +export const output = [1, 2, 3] + +export const create = true diff --git a/test/validation/object/valid-frozen.ts b/test/validation/object/valid-frozen.ts new file mode 100644 index 00000000..462de9f1 --- /dev/null +++ b/test/validation/object/valid-frozen.ts @@ -0,0 +1,18 @@ +import { object, string, number } from '../../../src' + +export const Struct = object({ + name: string(), + age: number(), +}) + +export const data = Object.freeze({ + name: 'john', + age: 42, +}) + +export const output = { + name: 'john', + age: 42, +} + +export const create = true diff --git a/test/validation/record/valid-frozen.ts b/test/validation/record/valid-frozen.ts new file mode 100644 index 00000000..1bb5bd83 --- /dev/null +++ b/test/validation/record/valid-frozen.ts @@ -0,0 +1,15 @@ +import { record, string, number } from '../../../src' + +export const Struct = record(string(), number()) + +export const data = Object.freeze({ + a: 1, + b: 2, +}) + +export const output = { + a: 1, + b: 2, +} + +export const create = true diff --git a/test/validation/tuple/valid-frozen.ts b/test/validation/tuple/valid-frozen.ts new file mode 100644 index 00000000..b084af38 --- /dev/null +++ b/test/validation/tuple/valid-frozen.ts @@ -0,0 +1,9 @@ +import { tuple, string, number } from '../../../src' + +export const Struct = tuple([string(), number()]) + +export const data = Object.freeze(['A', 1]) + +export const output = ['A', 1] + +export const create = true diff --git a/test/validation/type/valid-frozen.ts b/test/validation/type/valid-frozen.ts new file mode 100644 index 00000000..cdb08dbc --- /dev/null +++ b/test/validation/type/valid-frozen.ts @@ -0,0 +1,18 @@ +import { type, string, number } from '../../../src' + +export const Struct = type({ + name: string(), + age: number(), +}) + +export const data = Object.freeze({ + name: 'john', + age: 42, +}) + +export const output = { + name: 'john', + age: 42, +} + +export const create = true