11import { PartialDeep } from 'type-fest' ;
22import { cleanupNonNestedPath , isNotNestedPath , type TypedSchema , type TypedSchemaError } from 'vee-validate' ;
33import {
4- Output ,
5- Input ,
4+ InferOutput ,
5+ InferInput ,
66 BaseSchema ,
77 BaseSchemaAsync ,
88 safeParseAsync ,
99 safeParse ,
10- SchemaIssue ,
10+ BaseIssue ,
1111 getDefault ,
1212 optional ,
1313 ArraySchema ,
1414 ObjectSchema ,
15+ ErrorMessage ,
16+ ArrayIssue ,
17+ ObjectEntries ,
18+ LooseObjectIssue ,
19+ ObjectIssue ,
20+ ObjectWithRestSchema ,
21+ ObjectWithRestIssue ,
22+ StrictObjectIssue ,
23+ StrictObjectSchema ,
24+ LooseObjectSchema ,
25+ getDotPath ,
1526} from 'valibot' ;
1627import { isIndex , isObject , merge , normalizeFormPath } from '../../shared' ;
1728
1829export function toTypedSchema <
19- TSchema extends BaseSchema | BaseSchemaAsync ,
20- TOutput = Output < TSchema > ,
21- TInput = PartialDeep < Input < TSchema > > ,
22- > ( valibotSchema : TSchema ) : TypedSchema < TInput , TOutput > {
30+ TSchema extends
31+ | BaseSchema < unknown , unknown , BaseIssue < unknown > >
32+ | BaseSchemaAsync < unknown , unknown , BaseIssue < unknown > > ,
33+ TInferOutput = InferOutput < TSchema > ,
34+ TInferInput = PartialDeep < InferInput < TSchema > > ,
35+ > ( valibotSchema : TSchema ) : TypedSchema < TInferInput , TInferOutput > {
2336 const schema : TypedSchema = {
2437 __type : 'VVTypedSchema' ,
2538 async parse ( value ) {
@@ -92,10 +105,10 @@ export function toTypedSchema<
92105 return schema ;
93106}
94107
95- function processIssues ( issues : SchemaIssue [ ] , errors : Record < string , TypedSchemaError > ) : void {
108+ function processIssues ( issues : BaseIssue < unknown > [ ] , errors : Record < string , TypedSchemaError > ) : void {
96109 issues . forEach ( issue => {
97- const path = normalizeFormPath ( ( issue . path || [ ] ) . map ( p => p . key ) . join ( '.' ) ) ;
98- if ( issue . issues ?. length ) {
110+ const path = normalizeFormPath ( getDotPath ( issue ) || '' ) ;
111+ if ( issue . issues ) {
99112 processIssues (
100113 issue . issues . flatMap ( ue => ue . issues || [ ] ) ,
101114 errors ,
@@ -114,7 +127,10 @@ function processIssues(issues: SchemaIssue[], errors: Record<string, TypedSchema
114127 } ) ;
115128}
116129
117- function getSchemaForPath ( path : string , schema : any ) : BaseSchema | null {
130+ function getSchemaForPath (
131+ path : string ,
132+ schema : BaseSchema < unknown , unknown , BaseIssue < unknown > > | BaseSchemaAsync < unknown , unknown , BaseIssue < unknown > > ,
133+ ) : BaseSchema < unknown , unknown , BaseIssue < unknown > > | null {
118134 if ( ! isObjectSchema ( schema ) ) {
119135 return null ;
120136 }
@@ -125,7 +141,7 @@ function getSchemaForPath(path: string, schema: any): BaseSchema | null {
125141
126142 const paths = ( path || '' ) . split ( / \. | \[ ( \d + ) \] / ) . filter ( Boolean ) ;
127143
128- let currentSchema : BaseSchema = schema ;
144+ let currentSchema : BaseSchema < unknown , unknown , BaseIssue < unknown > > = schema ;
129145 for ( let i = 0 ; i <= paths . length ; i ++ ) {
130146 const p = paths [ i ] ;
131147 if ( ! p || ! currentSchema ) {
@@ -145,14 +161,28 @@ function getSchemaForPath(path: string, schema: any): BaseSchema | null {
145161 return null ;
146162}
147163
148- function queryOptional ( schema : BaseSchema | BaseSchemaAsync ) : boolean {
149- return ( schema as any ) . type === 'optional' ;
164+ function queryOptional (
165+ schema : BaseSchema < unknown , unknown , BaseIssue < unknown > > | BaseSchemaAsync < unknown , unknown , BaseIssue < unknown > > ,
166+ ) : boolean {
167+ return schema . type === 'optional' ;
150168}
151169
152- function isArraySchema ( schema : unknown ) : schema is ArraySchema < any > {
153- return isObject ( schema ) && schema . type === 'array' ;
170+ function isArraySchema (
171+ schema : unknown ,
172+ ) : schema is ArraySchema < BaseSchema < unknown , unknown , BaseIssue < unknown > > , ErrorMessage < ArrayIssue > | undefined > {
173+ return isObject ( schema ) && 'item' in schema ;
154174}
155175
156- function isObjectSchema ( schema : unknown ) : schema is ObjectSchema < any > {
157- return isObject ( schema ) && schema . type === 'object' ;
176+ function isObjectSchema (
177+ schema : unknown ,
178+ ) : schema is
179+ | LooseObjectSchema < ObjectEntries , ErrorMessage < LooseObjectIssue > | undefined >
180+ | ObjectSchema < ObjectEntries , ErrorMessage < ObjectIssue > | undefined >
181+ | ObjectWithRestSchema <
182+ ObjectEntries ,
183+ BaseSchema < unknown , unknown , BaseIssue < unknown > > ,
184+ ErrorMessage < ObjectWithRestIssue > | undefined
185+ >
186+ | StrictObjectSchema < ObjectEntries , ErrorMessage < StrictObjectIssue > | undefined > {
187+ return isObject ( schema ) && 'entries' in schema ;
158188}
0 commit comments