Skip to content

Commit

Permalink
fix(json-api-nestjs): Resource Relationship not allowing data key.
Browse files Browse the repository at this point in the history
fix validation for allow data props for relation on post and patch method

Closes: 87
  • Loading branch information
klerick committed May 15, 2024
1 parent 6197b52 commit f648422
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => {
};

let relationshipsSchema: ZodPatchRelationshipsSchema<Users>;
type RelationshipsSchema = z.infer<ZodPatchRelationshipsSchema<Users>>;
beforeAll(() => {
relationshipsSchema = zodPatchRelationshipsSchema(
relationArrayProps,
Expand All @@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => {
});

it('Should be ok', () => {
const check: RelationshipsSchema = {
const check = {
comments: [
{
type: 'comments',
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export const zodPatchRelationshipsSchema = <E extends Entity>(
).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<E>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('zodRelationshipsSchema', () => {
};

let relationshipsSchema: ZodRelationshipsSchema<Users>;
type RelationshipsSchema = z.infer<ZodRelationshipsSchema<Users>>;
beforeAll(() => {
relationshipsSchema = zodRelationshipsSchema(
relationArrayProps,
Expand All @@ -50,7 +49,7 @@ describe('zodRelationshipsSchema', () => {
});

it('Should be ok', () => {
const check: RelationshipsSchema = {
const check = {
comments: [
{
type: 'comments',
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,21 @@ export const zodRelationshipsSchema = <E extends Entity>(
).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<E>
);

return z.object(shape).strict().refine(nonEmptyObject());
};

0 comments on commit f648422

Please sign in to comment.