11import { toNestErrors , validateFieldsNatively } from '@hookform/resolvers' ;
2- import type { ValidationIssue } from '@typeschema/core' ;
3- import { validate } from '@typeschema/main' ;
4- import { FieldError , FieldErrors , appendErrors } from 'react-hook-form' ;
5- import type { Resolver } from './types' ;
2+ import {
3+ FieldError ,
4+ FieldErrors ,
5+ FieldValues ,
6+ Resolver ,
7+ appendErrors ,
8+ } from 'react-hook-form' ;
9+ import { StandardSchemaV1 } from 'zod/lib/standard-schema' ;
610
711const parseErrorSchema = (
8- typeschemaErrors : ValidationIssue [ ] ,
12+ typeschemaErrors : readonly StandardSchemaV1 . Issue [ ] ,
913 validateAllFieldCriteria : boolean ,
1014) : FieldErrors => {
15+ const schemaErrors = Object . assign ( [ ] , typeschemaErrors ) ;
1116 const errors : Record < string , FieldError > = { } ;
1217
13- for ( ; typeschemaErrors . length ; ) {
18+ for ( ; schemaErrors . length ; ) {
1419 const error = typeschemaErrors [ 0 ] ;
1520
1621 if ( ! error . path ) {
@@ -37,12 +42,28 @@ const parseErrorSchema = (
3742 ) as FieldError ;
3843 }
3944
40- typeschemaErrors . shift ( ) ;
45+ schemaErrors . shift ( ) ;
4146 }
4247
4348 return errors ;
4449} ;
4550
51+ export function typeschemaResolver < Input extends FieldValues , Context , Output > (
52+ schema : StandardSchemaV1 < Input , Output > ,
53+ _schemaOptions ?: never ,
54+ resolverOptions ?: {
55+ raw ?: false ;
56+ } ,
57+ ) : Resolver < Input , Context , Output > ;
58+
59+ export function typeschemaResolver < Input extends FieldValues , Context , Output > (
60+ schema : StandardSchemaV1 < Input , Output > ,
61+ _schemaOptions : never | undefined ,
62+ resolverOptions : {
63+ raw : true ;
64+ } ,
65+ ) : Resolver < Input , Context , Input > ;
66+
4667/**
4768 * Creates a resolver for react-hook-form using TypeSchema validation
4869 * @param {any } schema - The TypeSchema to validate against
@@ -60,30 +81,36 @@ const parseErrorSchema = (
6081 * resolver: typeschemaResolver(schema)
6182 * });
6283 */
63- export const typeschemaResolver : Resolver =
64- ( schema , _ , resolverOptions = { } ) =>
65- async ( values , _ , options ) => {
66- const result = await validate ( schema , values ) ;
84+ export function typeschemaResolver < Input extends FieldValues , Context , Output > (
85+ schema : StandardSchemaV1 < Input , Output > ,
86+ _schemaOptions ?: never ,
87+ resolverOptions : {
88+ raw ?: boolean ;
89+ } = { } ,
90+ ) : Resolver < Input , Context , Output | Input > {
91+ return async ( values , _ , options ) => {
92+ let result = schema [ '~standard' ] . validate ( values ) ;
93+ if ( result instanceof Promise ) {
94+ result = await result ;
95+ }
6796
68- options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
97+ if ( result . issues ) {
98+ const errors = parseErrorSchema (
99+ result . issues ,
100+ ! options . shouldUseNativeValidation && options . criteriaMode === 'all' ,
101+ ) ;
69102
70- if ( result . success ) {
71103 return {
72- errors : { } as FieldErrors ,
73- values : resolverOptions . raw
74- ? Object . assign ( { } , values )
75- : ( result . data as any ) ,
104+ values : { } ,
105+ errors : toNestErrors ( errors , options ) ,
76106 } ;
77107 }
78108
109+ options . shouldUseNativeValidation && validateFieldsNatively ( { } , options ) ;
110+
79111 return {
80- values : { } ,
81- errors : toNestErrors (
82- parseErrorSchema (
83- result . issues ,
84- ! options . shouldUseNativeValidation && options . criteriaMode === 'all' ,
85- ) ,
86- options ,
87- ) ,
112+ values : resolverOptions . raw ? Object . assign ( { } , values ) : result . value ,
113+ errors : { } ,
88114 } ;
89115 } ;
116+ }
0 commit comments