1
1
import { toNestErrors , validateFieldsNatively } from '@hookform/resolvers' ;
2
- import { Static , StaticDecode , Type } from '@sinclair/typebox' ;
2
+ import { Static , StaticDecode , TObject } from '@sinclair/typebox' ;
3
3
import { TypeCheck } from '@sinclair/typebox/compiler' ;
4
4
import { Value , type ValueError } from '@sinclair/typebox/value' ;
5
5
import {
6
6
FieldError ,
7
- FieldErrors ,
8
7
FieldValues ,
9
8
Resolver ,
10
9
appendErrors ,
@@ -45,21 +44,19 @@ function parseErrorSchema(
45
44
return errors ;
46
45
}
47
46
48
- export function typeboxResolver < Input extends FieldValues , Context > (
49
- typecheck : TypeCheck < ReturnType < typeof Type . Object < Input > > > ,
50
- ) : Resolver <
51
- Static < ReturnType < typeof typecheck . Schema > > ,
47
+ export function typeboxResolver <
48
+ Input extends FieldValues ,
52
49
Context ,
53
- StaticDecode < ReturnType < typeof typecheck . Schema > >
54
- > ;
50
+ Schema extends TObject < Input > = TObject < Input > ,
51
+ > (
52
+ typecheck : TypeCheck < Schema > ,
53
+ ) : Resolver < Static < Schema > , Context , StaticDecode < Schema > > ;
55
54
56
- export function typeboxResolver < Input extends FieldValues , Context > (
57
- schema : ReturnType < typeof Type . Object < Input > > ,
58
- ) : Resolver < Static < typeof schema > , Context , StaticDecode < typeof schema > > ;
59
-
60
- export function typeboxResolver < Input extends FieldValues , Context , Output > (
61
- schema : ReturnType < typeof Type . Object < Input > > ,
62
- ) : Resolver < Static < typeof schema > , Context , Output > ;
55
+ export function typeboxResolver <
56
+ Input extends FieldValues ,
57
+ Context ,
58
+ Schema extends TObject < Input > = TObject < Input > ,
59
+ > ( schema : Schema ) : Resolver < Static < Schema > , Context , StaticDecode < Schema > > ;
63
60
64
61
/**
65
62
* Creates a resolver for react-hook-form using Typebox schema validation
@@ -77,10 +74,14 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
77
74
* resolver: typeboxResolver(schema)
78
75
* });
79
76
*/
80
- export function typeboxResolver < Input extends FieldValues , Context , Output > (
81
- schema : ReturnType < typeof Type . Object < Input > > ,
82
- ) : Resolver < Static < typeof schema > , Context , Output | Static < typeof schema > > {
83
- return async ( values : Static < typeof schema > , _ , options ) => {
77
+ export function typeboxResolver <
78
+ Input extends FieldValues ,
79
+ Context ,
80
+ Schema extends TObject < Input > ,
81
+ > (
82
+ schema : Schema | TypeCheck < Schema > ,
83
+ ) : Resolver < Static < Schema > , Context , StaticDecode < Schema > > {
84
+ return async ( values : Static < Schema > , _ , options ) => {
84
85
const errors = Array . from (
85
86
schema instanceof TypeCheck
86
87
? schema . Errors ( values )
@@ -91,7 +92,7 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
91
92
92
93
if ( ! errors . length ) {
93
94
return {
94
- errors : { } as FieldErrors ,
95
+ errors : { } ,
95
96
values,
96
97
} ;
97
98
}
0 commit comments