1- import {
2- AnyObjectSchema ,
3- ArraySchema ,
4- InferType ,
5- Schema ,
6- SchemaFieldDescription ,
7- ValidateOptions ,
8- ValidationError ,
9- } from 'yup' ;
1+ import { AnyObjectSchema , ArraySchema , InferType , Schema , ValidateOptions , ValidationError } from 'yup' ;
102import {
113 TypedSchema ,
124 TypedSchemaError ,
@@ -76,49 +68,40 @@ export function toTypedSchema<TSchema extends Schema, TOutput = InferType<TSchem
7668 } ,
7769 describe ( path ) {
7870 if ( ! path ) {
79- return getDescriptionFromYupDescription ( yupSchema . describe ( ) ) ;
71+ return getDescriptionFromYupSpec ( yupSchema . spec ) ;
8072 }
8173
82- const description = getDescriptionForPath ( path , yupSchema ) ;
74+ const description = getSpecForPath ( path , yupSchema ) ;
8375 if ( ! description ) {
8476 return {
8577 required : false ,
8678 exists : false ,
8779 } ;
8880 }
8981
90- return getDescriptionFromYupDescription ( description ) ;
82+ return getDescriptionFromYupSpec ( description ) ;
9183 } ,
9284 } ;
9385
9486 return schema ;
9587}
9688
97- function getDescriptionFromYupDescription ( desc : SchemaFieldDescription ) : TypedSchemaPathDescription {
98- if ( 'tests' in desc ) {
99- const required = desc ?. tests ?. some ( t => t . name === 'required' ) || false ;
100-
101- return {
102- required,
103- exists : true ,
104- } ;
105- }
106-
89+ function getDescriptionFromYupSpec ( spec : AnyObjectSchema [ 'spec' ] ) : TypedSchemaPathDescription {
10790 return {
108- required : false ,
109- exists : false ,
91+ required : ! spec . optional ,
92+ exists : true ,
11093 } ;
11194}
11295
113- function getDescriptionForPath ( path : string , schema : Schema ) : SchemaFieldDescription | null {
96+ function getSpecForPath ( path : string , schema : Schema ) : AnyObjectSchema [ 'spec' ] | null {
11497 if ( ! isObjectSchema ( schema ) ) {
11598 return null ;
11699 }
117100
118101 if ( isNotNestedPath ( path ) ) {
119102 const field = schema . fields [ cleanupNonNestedPath ( path ) ] ;
120103
121- return field ?. describe ( ) || null ;
104+ return ( field as AnyObjectSchema ) ?. spec || null ;
122105 }
123106
124107 const paths = ( path || '' ) . split ( / \. | \[ ( \d + ) \] / ) . filter ( Boolean ) ;
@@ -133,7 +116,7 @@ function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescrip
133116 }
134117
135118 if ( i === paths . length - 1 ) {
136- return currentSchema . describe ( ) ;
119+ return currentSchema . spec ;
137120 }
138121 }
139122
0 commit comments