Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow frozen objects/arrays to be coerced #1151

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ export function record<K extends string, V>(
`Expected an object, but received: ${print(value)}`
)
},
coercer(value) {
return isNonArrayObject(value) ? { ...value } : value
},
})
}

Expand Down Expand Up @@ -473,6 +476,9 @@ export function tuple<A extends AnyStruct, B extends AnyStruct[]>(
`Expected an array, but received: ${print(value)}`
)
},
coercer(value) {
return Array.isArray(value) ? value.slice() : value
},
})
}

Expand Down
9 changes: 9 additions & 0 deletions test/validation/array/valid-frozen.ts
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/validation/object/valid-frozen.ts
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/validation/record/valid-frozen.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions test/validation/tuple/valid-frozen.ts
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/validation/type/valid-frozen.ts
Original file line number Diff line number Diff line change
@@ -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