From f6484223eccbf89875cc85241104ccf3733521bb Mon Sep 17 00:00:00 2001 From: Alex H Date: Wed, 15 May 2024 15:36:58 +0200 Subject: [PATCH] fix(json-api-nestjs): Resource Relationship not allowing data key. fix validation for allow data props for relation on post and patch method Closes: 87 --- .../relationships.spec.ts | 37 +++++++++++++++++-- .../zod-input-patch-schema/relationships.ts | 12 +++++- .../relationships.spec.ts | 34 ++++++++++++++++- .../zod-input-post-schema/relationships.ts | 13 ++++++- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.spec.ts b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.spec.ts index ea02cc4f..d04c1f23 100644 --- a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.spec.ts +++ b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.spec.ts @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => { }; let relationshipsSchema: ZodPatchRelationshipsSchema; - type RelationshipsSchema = z.infer>; beforeAll(() => { relationshipsSchema = zodPatchRelationshipsSchema( relationArrayProps, @@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => { }); it('Should be ok', () => { - const check: RelationshipsSchema = { + const check = { comments: [ { type: 'comments', @@ -72,12 +71,44 @@ describe('zodRelationshipsSchema', () => { }, ], }; - const check2: RelationshipsSchema = { + const check2 = { comments: [], manager: null, }; + const check3 = { + comments: { + data: [ + { + type: 'comments', + id: '1', + }, + ], + }, + userGroup: { + data: { + type: 'user-groups', + id: '1', + }, + }, + manager: { + data: { + type: 'users', + id: '1', + }, + }, + notes: { + data: [ + { + type: 'notes', + id: 'id', + }, + ], + }, + }; + expect(relationshipsSchema.parse(check)).toEqual(check); expect(relationshipsSchema.parse(check2)).toEqual(check2); + expect(relationshipsSchema.parse(check3)).toEqual(check); }); it('should be not ok', () => { diff --git a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.ts b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.ts index 89f43e94..4e9e48dd 100644 --- a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.ts +++ b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-patch-schema/relationships.ts @@ -45,7 +45,17 @@ export const zodPatchRelationshipsSchema = ( ).optional(); return { ...acum, - [props]: dataItem, + [props]: z.union([ + dataItem, + z + .object({ data: dataItem }) + .strict() + .refine(nonEmptyObject()) + .transform((i) => { + const { data } = i; + return data; + }), + ]), }; }, {} as ShapeRelationships diff --git a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.spec.ts b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.spec.ts index 81d11c33..54604aba 100644 --- a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.spec.ts +++ b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.spec.ts @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => { }; let relationshipsSchema: ZodRelationshipsSchema; - type RelationshipsSchema = z.infer>; beforeAll(() => { relationshipsSchema = zodRelationshipsSchema( relationArrayProps, @@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => { }); it('Should be ok', () => { - const check: RelationshipsSchema = { + const check = { comments: [ { type: 'comments', @@ -72,7 +71,38 @@ describe('zodRelationshipsSchema', () => { }, ], }; + const check2 = { + comments: { + data: [ + { + type: 'comments', + id: '1', + }, + ], + }, + userGroup: { + data: { + type: 'user-groups', + id: '1', + }, + }, + manager: { + data: { + type: 'users', + id: '1', + }, + }, + notes: { + data: [ + { + type: 'notes', + id: 'id', + }, + ], + }, + }; expect(relationshipsSchema.parse(check)).toEqual(check); + expect(relationshipsSchema.parse(check2)).toEqual(check); }); it('should be not ok', () => { diff --git a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.ts b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.ts index 524e2fed..3cad63e6 100644 --- a/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.ts +++ b/libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/relationships.ts @@ -47,10 +47,21 @@ export const zodRelationshipsSchema = ( ).optional(); return { ...acum, - [props]: dataItem, + [props]: z.union([ + dataItem, + z + .object({ data: dataItem }) + .strict() + .refine(nonEmptyObject()) + .transform((i) => { + const { data } = i; + return data; + }), + ]), }; }, {} as ShapeRelationships ); + return z.object(shape).strict().refine(nonEmptyObject()); };