@@ -10,7 +10,7 @@ export class StrapiSchemaGenerator {
1010 this . strict = strict ;
1111 }
1212
13- private generateAttributeSchema ( attribute : StrapiAttribute ) : z . ZodType < unknown > {
13+ private generateAttributeSchema ( attribute : StrapiAttribute , pupulatedRelations : Array < string > = [ ] ) : z . ZodType < unknown > | null {
1414 const ref = this ;
1515 switch ( attribute . type ) {
1616 case "string" :
@@ -90,24 +90,30 @@ export class StrapiSchemaGenerator {
9090 if ( ! attribute . relation )
9191 throw new Error ( "Relation type requires relation target" ) ;
9292 const targetType = ref . contentTypes . find (
93- ( contentType ) => contentType . apiID === attribute . relationTargetModel ,
93+ ( contentType ) =>
94+ contentType . apiID === attribute . target ||
95+ contentType . uid === attribute . target ,
9496 ) ;
97+
9598 if ( ! targetType )
9699 throw new Error ( `Target type ${ attribute . relation } not found` ) ;
97100
98- const relationSchema = ref . generateContentTypeSchema ( targetType . schema ) ;
101+ if ( attribute . targetAttribute && pupulatedRelations . includes ( attribute . targetAttribute ) ) {
102+ return null ;
103+ }
104+
105+ const relationSchema = ref . generateContentTypeSchema (
106+ targetType . schema ,
107+ attribute . targetAttribute ? [ ...pupulatedRelations , attribute . targetAttribute ] : pupulatedRelations ,
108+ ) ;
99109
100110 switch ( attribute . relation ) {
101111 case "oneToOne" :
102112 case "manyToOne" :
103- return z . object ( {
104- data : relationSchema . nullable ( ) ,
105- } ) ;
113+ return relationSchema ;
106114 case "oneToMany" :
107115 case "manyToMany" :
108- return z . object ( {
109- data : z . array ( relationSchema ) ,
110- } ) ;
116+ return z . array ( relationSchema ) ;
111117 default :
112118 throw new Error (
113119 `Unsupported relation type: ${ attribute . relationType } ` ,
@@ -131,23 +137,27 @@ export class StrapiSchemaGenerator {
131137
132138 private generateContentTypeSchema (
133139 contentTypeSchema : StrapiContentType [ "schema" ] ,
140+ pupulatedRelations ?: Array < string >
134141 ) : z . ZodObject < any > {
135142 const ref = this ;
136143 const shape : Record < string , z . ZodTypeAny > = Object . entries (
137144 contentTypeSchema . attributes ,
138145 ) . reduce (
139146 ( acc , [ key , attribute ] ) => {
140147 try {
141- const schema = ref . generateAttributeSchema ( attribute ) ;
142- return {
143- ...acc ,
144- [ key ] :
145- ref . strict && attribute . required
146- ? schema
147- : schema . nullable ( ) . optional ( ) ,
148- } ;
148+ const schema = ref . generateAttributeSchema ( attribute , pupulatedRelations ) ;
149+ if ( schema ) {
150+ return {
151+ ...acc ,
152+ [ key ] :
153+ ref . strict && attribute . required
154+ ? schema
155+ : schema . nullable ( ) . optional ( ) ,
156+ } ;
157+ }
158+ return acc ;
149159 } catch ( error ) {
150- console . error ( "Error generating attribute schema" , error ) ;
160+ console . warn ( "Error generating attribute schema" , error ) ;
151161 return acc ;
152162 }
153163 } ,
0 commit comments