Skip to content

Commit

Permalink
Fix incorrectly passing validation when arrays are passed to type()
Browse files Browse the repository at this point in the history
  • Loading branch information
arturmuller committed Jun 30, 2024
1 parent e9c0341 commit cd2ee1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,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
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 cd2ee1f

Please sign in to comment.