1
1
import { Repository } from 'typeorm' ;
2
- import { z , ZodObject } from 'zod' ;
2
+ import { z , ZodObject , ZodUnion } from 'zod' ;
3
3
import { QueryField } from 'json-shared-type' ;
4
4
5
5
import {
@@ -45,6 +45,7 @@ import {
45
45
46
46
import {
47
47
PatchShape ,
48
+ PatchShapeDefault ,
48
49
zodPatchRelationshipsSchema ,
49
50
} from './zod-input-patch-schema' ;
50
51
@@ -93,7 +94,12 @@ export type PostData<E extends Entity> = z.infer<ZodInputPostSchema<E>>['data'];
93
94
94
95
export type ZodInputPatchSchema < E extends Entity > = ZodObject <
95
96
{
96
- data : ZodObject < PatchShape < E > , 'strict' > ;
97
+ data : ZodUnion <
98
+ [
99
+ ZodObject < PatchShapeDefault < E > , 'strict' > ,
100
+ ZodObject < PatchShape < E > , 'strict' >
101
+ ]
102
+ > ;
97
103
} ,
98
104
'strict'
99
105
> ;
@@ -236,7 +242,8 @@ export const zodInputPatchSchema = <E extends Entity>(
236
242
{ } as FieldWithType < E >
237
243
) ;
238
244
const typeName = camelToKebab ( getEntityName ( repository . target ) ) ;
239
- const postShape : PatchShape < E > = {
245
+
246
+ const patchShapeDefault : PatchShapeDefault < E > = {
240
247
id : zodIdSchema ( primaryType ) ,
241
248
type : zodTypeSchema ( typeName ) ,
242
249
attributes : zodAttributesSchema ( fieldWithType ) ,
@@ -247,9 +254,25 @@ export const zodInputPatchSchema = <E extends Entity>(
247
254
) . optional ( ) ,
248
255
} ;
249
256
257
+ const patchShape : PatchShape < E > = {
258
+ id : zodIdSchema ( primaryType ) ,
259
+ type : zodTypeSchema ( typeName ) ,
260
+ attributes : zodAttributesSchema ( fieldWithType )
261
+ . optional ( )
262
+ . default ( { } as any ) ,
263
+ relationships : zodPatchRelationshipsSchema (
264
+ relationArrayProps ,
265
+ relationPopsName ,
266
+ primaryColumnType
267
+ ) ,
268
+ } ;
269
+
250
270
return z
251
271
. object ( {
252
- data : z . object ( postShape ) . strict ( ) ,
272
+ data : z . union ( [
273
+ z . object ( patchShapeDefault ) . strict ( ) ,
274
+ z . object ( patchShape ) . strict ( ) ,
275
+ ] ) ,
253
276
} )
254
277
. strict ( ) ;
255
278
} ;
0 commit comments