1
1
import { toNestErrors , validateFieldsNatively } from '@hookform/resolvers' ;
2
- import { Static , StaticDecode , Type } from '@sinclair/typebox' ;
2
+ import { Static , StaticDecode , TObject , TProperties } from '@sinclair/typebox' ;
3
3
import { TypeCheck } from '@sinclair/typebox/compiler' ;
4
4
import { Value , type ValueError } from '@sinclair/typebox/value' ;
5
- import {
6
- FieldError ,
7
- FieldErrors ,
8
- FieldValues ,
9
- Resolver ,
10
- appendErrors ,
11
- } from 'react-hook-form' ;
5
+ import { FieldError , Resolver , appendErrors } from 'react-hook-form' ;
12
6
13
7
function parseErrorSchema (
14
8
_errors : ValueError [ ] ,
@@ -45,22 +39,6 @@ function parseErrorSchema(
45
39
return errors ;
46
40
}
47
41
48
- export function typeboxResolver < Input extends FieldValues , Context > (
49
- typecheck : TypeCheck < ReturnType < typeof Type . Object < Input > > > ,
50
- ) : Resolver <
51
- Static < ReturnType < typeof typecheck . Schema > > ,
52
- Context ,
53
- StaticDecode < ReturnType < typeof typecheck . Schema > >
54
- > ;
55
-
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 > ;
63
-
64
42
/**
65
43
* Creates a resolver for react-hook-form using Typebox schema validation
66
44
* @param {Schema | TypeCheck<Schema> } schema - The Typebox schema to validate against
@@ -77,10 +55,14 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
77
55
* resolver: typeboxResolver(schema)
78
56
* });
79
57
*/
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 ) => {
58
+ export function typeboxResolver <
59
+ Properties extends TProperties ,
60
+ Context ,
61
+ Schema extends TObject < Properties > ,
62
+ > (
63
+ schema : Schema | TypeCheck < Schema > ,
64
+ ) : Resolver < Static < Schema > , Context , StaticDecode < Schema > > {
65
+ return async ( values : Static < Schema > , _ , options ) => {
84
66
const errors = Array . from (
85
67
schema instanceof TypeCheck
86
68
? schema . Errors ( values )
@@ -91,7 +73,7 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
91
73
92
74
if ( ! errors . length ) {
93
75
return {
94
- errors : { } as FieldErrors ,
76
+ errors : { } ,
95
77
values,
96
78
} ;
97
79
}
0 commit comments