File tree Expand file tree Collapse file tree 4 files changed +89
-7
lines changed
libs/json-api/json-api-nestjs/src/lib/helper/zod Expand file tree Collapse file tree 4 files changed +89
-7
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => {
4040 } ;
4141
4242 let relationshipsSchema : ZodPatchRelationshipsSchema < Users > ;
43- type RelationshipsSchema = z . infer < ZodPatchRelationshipsSchema < Users > > ;
4443 beforeAll ( ( ) => {
4544 relationshipsSchema = zodPatchRelationshipsSchema (
4645 relationArrayProps ,
@@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => {
5049 } ) ;
5150
5251 it ( 'Should be ok' , ( ) => {
53- const check : RelationshipsSchema = {
52+ const check = {
5453 comments : [
5554 {
5655 type : 'comments' ,
@@ -72,12 +71,44 @@ describe('zodRelationshipsSchema', () => {
7271 } ,
7372 ] ,
7473 } ;
75- const check2 : RelationshipsSchema = {
74+ const check2 = {
7675 comments : [ ] ,
7776 manager : null ,
7877 } ;
78+ const check3 = {
79+ comments : {
80+ data : [
81+ {
82+ type : 'comments' ,
83+ id : '1' ,
84+ } ,
85+ ] ,
86+ } ,
87+ userGroup : {
88+ data : {
89+ type : 'user-groups' ,
90+ id : '1' ,
91+ } ,
92+ } ,
93+ manager : {
94+ data : {
95+ type : 'users' ,
96+ id : '1' ,
97+ } ,
98+ } ,
99+ notes : {
100+ data : [
101+ {
102+ type : 'notes' ,
103+ id : 'id' ,
104+ } ,
105+ ] ,
106+ } ,
107+ } ;
108+
79109 expect ( relationshipsSchema . parse ( check ) ) . toEqual ( check ) ;
80110 expect ( relationshipsSchema . parse ( check2 ) ) . toEqual ( check2 ) ;
111+ expect ( relationshipsSchema . parse ( check3 ) ) . toEqual ( check ) ;
81112 } ) ;
82113
83114 it ( 'should be not ok' , ( ) => {
Original file line number Diff line number Diff line change @@ -45,7 +45,17 @@ export const zodPatchRelationshipsSchema = <E extends Entity>(
4545 ) . optional ( ) ;
4646 return {
4747 ...acum ,
48- [ props ] : dataItem ,
48+ [ props ] : z . union ( [
49+ dataItem ,
50+ z
51+ . object ( { data : dataItem } )
52+ . strict ( )
53+ . refine ( nonEmptyObject ( ) )
54+ . transform ( ( i ) => {
55+ const { data } = i ;
56+ return data ;
57+ } ) ,
58+ ] ) ,
4959 } ;
5060 } ,
5161 { } as ShapeRelationships < E >
Original file line number Diff line number Diff line change @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => {
4040 } ;
4141
4242 let relationshipsSchema : ZodRelationshipsSchema < Users > ;
43- type RelationshipsSchema = z . infer < ZodRelationshipsSchema < Users > > ;
4443 beforeAll ( ( ) => {
4544 relationshipsSchema = zodRelationshipsSchema (
4645 relationArrayProps ,
@@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => {
5049 } ) ;
5150
5251 it ( 'Should be ok' , ( ) => {
53- const check : RelationshipsSchema = {
52+ const check = {
5453 comments : [
5554 {
5655 type : 'comments' ,
@@ -72,7 +71,38 @@ describe('zodRelationshipsSchema', () => {
7271 } ,
7372 ] ,
7473 } ;
74+ const check2 = {
75+ comments : {
76+ data : [
77+ {
78+ type : 'comments' ,
79+ id : '1' ,
80+ } ,
81+ ] ,
82+ } ,
83+ userGroup : {
84+ data : {
85+ type : 'user-groups' ,
86+ id : '1' ,
87+ } ,
88+ } ,
89+ manager : {
90+ data : {
91+ type : 'users' ,
92+ id : '1' ,
93+ } ,
94+ } ,
95+ notes : {
96+ data : [
97+ {
98+ type : 'notes' ,
99+ id : 'id' ,
100+ } ,
101+ ] ,
102+ } ,
103+ } ;
75104 expect ( relationshipsSchema . parse ( check ) ) . toEqual ( check ) ;
105+ expect ( relationshipsSchema . parse ( check2 ) ) . toEqual ( check ) ;
76106 } ) ;
77107
78108 it ( 'should be not ok' , ( ) => {
Original file line number Diff line number Diff line change @@ -47,10 +47,21 @@ export const zodRelationshipsSchema = <E extends Entity>(
4747 ) . optional ( ) ;
4848 return {
4949 ...acum ,
50- [ props ] : dataItem ,
50+ [ props ] : z . union ( [
51+ dataItem ,
52+ z
53+ . object ( { data : dataItem } )
54+ . strict ( )
55+ . refine ( nonEmptyObject ( ) )
56+ . transform ( ( i ) => {
57+ const { data } = i ;
58+ return data ;
59+ } ) ,
60+ ] ) ,
5161 } ;
5262 } ,
5363 { } as ShapeRelationships < E >
5464 ) ;
65+
5566 return z . object ( shape ) . strict ( ) . refine ( nonEmptyObject ( ) ) ;
5667} ;
You can’t perform that action at this time.
0 commit comments