Skip to content

Commit

Permalink
Fix incorrectly passing validation for arrays in object(), type(), an…
Browse files Browse the repository at this point in the history
…d record() (#1252)

* Add isNonArrayObject util

* Fix incorrectly passing validation when arrays are passed to object()

* Fix incorrectly passing validation when arrays are passed to record()

* Fix incorrectly passing validation when arrays are passed to type()
  • Loading branch information
arturmuller authored Jun 30, 2024
1 parent 8232269 commit fdf3e62
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
print,
run,
isObject,
isNonArrayObject,
AnyStruct,
InferStructTuple,
UnionToIntersection,
Expand Down Expand Up @@ -316,11 +317,12 @@ export function object<S extends ObjectSchema>(schema?: S): any {
},
validator(value) {
return (
isObject(value) || `Expected an object, but received: ${print(value)}`
isNonArrayObject(value) ||
`Expected an object, but received: ${print(value)}`
)
},
coercer(value, ctx) {
if (!isObject(value) || Array.isArray(value)) {
if (!isNonArrayObject(value)) {
return value
}

Expand Down Expand Up @@ -380,7 +382,8 @@ export function record<K extends string, V>(
},
validator(value) {
return (
isObject(value) || `Expected an object, but received: ${print(value)}`
isNonArrayObject(value) ||
`Expected an object, but received: ${print(value)}`
)
},
})
Expand Down Expand Up @@ -496,11 +499,12 @@ export function type<S extends ObjectSchema>(
},
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
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function isObject(x: unknown): x is object {
return typeof x === 'object' && x != null
}

/**
* Check if a value is a non-array object.
*/

export function isNonArrayObject(x: unknown): x is object {
return isObject(x) && !Array.isArray(x)
}

/**
* Check if a value is a plain object.
*/
Expand Down
15 changes: 15 additions & 0 deletions test/validation/object/invalid-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { object } from '../../../src'

export const Struct = object()

export const data = []

export const failures = [
{
value: [],
type: 'object',
refinement: undefined,
path: [],
branch: [data],
},
]
15 changes: 15 additions & 0 deletions test/validation/record/invalid-array.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 = []

export const failures = [
{
value: [],
type: 'record',
refinement: undefined,
path: [],
branch: [data],
},
]
15 changes: 15 additions & 0 deletions test/validation/type/invalid-array.ts
Original file line number Diff line number Diff line change
@@ -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],
},
]

0 comments on commit fdf3e62

Please sign in to comment.