Skip to content

Commit

Permalink
add validation for refine method
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalics committed Jan 21, 2024
1 parent 219c7b7 commit 29e2efd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ abstract class SchemaBuilder<
* Schema.parse([{ active: true, name: 'some 1' }, { active: true, name: 'some 2' }]) // throws Error
*/
refine(fn: (output: Output) => any) {
if (typeof fn !== 'function') {
throw new TypeError(`Cannot set for not a function for refine. Expect "function", Got: ${typeof fn}`, { cause: { fn, type: typeof fn } })
}
this.refineFns.push(fn)
return this
}
Expand Down
5 changes: 5 additions & 0 deletions tests/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,8 @@ test('refine should throws default error', () => {
expect(result.error).toBeInstanceOf(Error)
expect(result.error?.message).toBe('refine error')
})

test('refine should throws TypeError for not a function', () => {
// @ts-expect-error
expect(() => s.number().refine(false)).toThrow(TypeError)
})

0 comments on commit 29e2efd

Please sign in to comment.