Skip to content

fix(json-api-nestjs): Resource Relationship not allowing data key. #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
};