diff --git a/src/structs/types.ts b/src/structs/types.ts index c1cfccfa..3eeb2bf4 100644 --- a/src/structs/types.ts +++ b/src/structs/types.ts @@ -386,6 +386,9 @@ export function record( `Expected an object, but received: ${print(value)}` ) }, + coercer(value) { + return isNonArrayObject(value) ? { ...value } : value + }, }) } @@ -473,6 +476,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