1
1
import { toNestErrors , validateFieldsNatively } from '@hookform/resolvers' ;
2
- import { ArkErrors , Type } from 'arktype' ;
3
- import { FieldError , FieldErrors , Resolver } from 'react-hook-form' ;
2
+ import { StandardSchemaV1 } from '@standard-schema/spec' ;
3
+ import { getDotPath } from '@standard-schema/utils' ;
4
+ import { FieldError , FieldValues , Resolver } from 'react-hook-form' ;
4
5
5
- function parseErrorSchema ( arkErrors : ArkErrors ) : Record < string , FieldError > {
6
- const errors = [ ...arkErrors ] ;
7
- const fieldsErrors : Record < string , FieldError > = { } ;
6
+ function parseErrorSchema (
7
+ issues : readonly StandardSchemaV1 . Issue [ ] ,
8
+ validateAllFieldCriteria : boolean ,
9
+ ) {
10
+ const errors : Record < string , FieldError > = { } ;
8
11
9
- for ( ; errors . length ; ) {
10
- const error = errors [ 0 ] ;
11
- const _path = error . path . join ( '.' ) ;
12
+ for ( let i = 0 ; i < issues . length ; i ++ ) {
13
+ const error = issues [ i ] ;
14
+ const path = getDotPath ( error ) ;
12
15
13
- if ( ! fieldsErrors [ _path ] ) {
14
- fieldsErrors [ _path ] = { message : error . message , type : error . code } ;
15
- }
16
+ if ( path ) {
17
+ if ( ! errors [ path ] ) {
18
+ errors [ path ] = { message : error . message , type : '' } ;
19
+ }
20
+
21
+ if ( validateAllFieldCriteria ) {
22
+ const types = errors [ path ] . types || { } ;
16
23
17
- errors . shift ( ) ;
24
+ errors [ path ] . types = {
25
+ ...types ,
26
+ [ Object . keys ( types ) . length ] : error . message ,
27
+ } ;
28
+ }
29
+ }
18
30
}
19
31
20
- return fieldsErrors ;
32
+ return errors ;
21
33
}
22
34
35
+ export function arktypeResolver < Input extends FieldValues , Context , Output > (
36
+ schema : StandardSchemaV1 < Input , Output > ,
37
+ _schemaOptions ?: never ,
38
+ resolverOptions ?: {
39
+ raw ?: false ;
40
+ } ,
41
+ ) : Resolver < Input , Context , Output > ;
42
+
43
+ export function arktypeResolver < Input extends FieldValues , Context , Output > (
44
+ schema : StandardSchemaV1 < Input , Output > ,
45
+ _schemaOptions : never | undefined ,
46
+ resolverOptions : {
47
+ raw : true ;
48
+ } ,
49
+ ) : Resolver < Input , Context , Input > ;
50
+
23
51
/**
24
52
* Creates a resolver for react-hook-form using Arktype schema validation
25
53
* @param {Schema } schema - The Arktype schema to validate against
@@ -35,28 +63,36 @@ function parseErrorSchema(arkErrors: ArkErrors): Record<string, FieldError> {
35
63
* resolver: arktypeResolver(schema)
36
64
* });
37
65
*/
38
- export function arktypeResolver < Schema extends Type < any , any > > (
39
- schema : Schema ,
66
+ export function arktypeResolver < Input extends FieldValues , Context , Output > (
67
+ schema : StandardSchemaV1 < Input , Output > ,
40
68
_schemaOptions ?: never ,
41
69
resolverOptions : {
42
70
raw ?: boolean ;
43
71
} = { } ,
44
- ) : Resolver < Schema [ 'inferOut' ] > {
45
- return ( values , _ , options ) => {
46
- const out = schema ( values ) ;
72
+ ) : Resolver < Input , Context , Input | Output > {
73
+ return async ( values : Input , _ , options ) => {
74
+ let result = schema [ '~standard' ] . validate ( values ) ;
75
+ if ( result instanceof Promise ) {
76
+ result = await result ;
77
+ }
78
+
79
+ if ( result . issues ) {
80
+ const errors = parseErrorSchema (
81
+ result . issues ,
82
+ ! options . shouldUseNativeValidation && options . criteriaMode === 'all' ,
83
+ ) ;
47
84
48
- if ( out instanceof ArkErrors ) {
49
85
return {
50
86
values : { } ,
51
- errors : toNestErrors ( parseErrorSchema ( out ) , options ) ,
87
+ errors : toNestErrors ( errors , options ) ,
52
88
} ;
53
89
}
54
90
55
91
options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
56
92
57
93
return {
58
- errors : { } as FieldErrors ,
59
- values : resolverOptions . raw ? Object . assign ( { } , values ) : out ,
94
+ values : resolverOptions . raw ? Object . assign ( { } , values ) : result . value ,
95
+ errors : { } ,
60
96
} ;
61
97
} ;
62
98
}
0 commit comments