11import { 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' ;
45
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 > = { } ;
811
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 ) ;
1215
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 || { } ;
1623
17- errors . shift ( ) ;
24+ errors [ path ] . types = {
25+ ...types ,
26+ [ Object . keys ( types ) . length ] : error . message ,
27+ } ;
28+ }
29+ }
1830 }
1931
20- return fieldsErrors ;
32+ return errors ;
2133}
2234
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+
2351/**
2452 * Creates a resolver for react-hook-form using Arktype schema validation
2553 * @param {Schema } schema - The Arktype schema to validate against
@@ -35,28 +63,36 @@ function parseErrorSchema(arkErrors: ArkErrors): Record<string, FieldError> {
3563 * resolver: arktypeResolver(schema)
3664 * });
3765 */
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 > ,
4068 _schemaOptions ?: never ,
4169 resolverOptions : {
4270 raw ?: boolean ;
4371 } = { } ,
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+ ) ;
4784
48- if ( out instanceof ArkErrors ) {
4985 return {
5086 values : { } ,
51- errors : toNestErrors ( parseErrorSchema ( out ) , options ) ,
87+ errors : toNestErrors ( errors , options ) ,
5288 } ;
5389 }
5490
5591 options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
5692
5793 return {
58- errors : { } as FieldErrors ,
59- values : resolverOptions . raw ? Object . assign ( { } , values ) : out ,
94+ values : resolverOptions . raw ? Object . assign ( { } , values ) : result . value ,
95+ errors : { } ,
6096 } ;
6197 } ;
6298}
0 commit comments