Skip to content

Commit e79de74

Browse files
Benjvvpfforres
andauthored
Add validation for community name length (#95)
Agregue validaciones zod de .min y .max solamente para el nombre de la comunidad :) --------- Co-authored-by: Felipe Torres <felipe.torressepulveda@gmail.com>
1 parent 9800937 commit e79de74

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/datasources/db/communities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ export const communityRelations = relations(communitySchema, ({ many }) => ({
2727
}));
2828

2929
export const selectCommunitySchema = createSelectSchema(communitySchema);
30-
export const insertCommunitySchema = createInsertSchema(communitySchema);
30+
export const insertCommunitySchema = createInsertSchema(communitySchema, {
31+
name: (schema) => schema.name.min(2).max(64),
32+
});

src/tests/community/communityCreate/communityCreate.test.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "~/tests/__fixtures";
88
import { clearDatabase } from "~/tests/__fixtures/databaseHelper";
99
import { CommnunityStatus } from "~/generated/types";
10-
import { faker } from "@faker-js/faker";
10+
import { fa, faker } from "@faker-js/faker";
1111
import {
1212
CreateCommunity,
1313
CreateCommunityMutation,
@@ -74,7 +74,7 @@ describe("Community", () => {
7474
});
7575
it("If community slug already exist", async () => {
7676
await insertCommunity({
77-
name: "a",
77+
name: "aas",
7878
slug: "c",
7979
});
8080
const fakeData = {
@@ -95,5 +95,53 @@ describe("Community", () => {
9595
assert.equal(response.errors?.length, 1);
9696
assert.equal(response.errors?.[0]?.message, "This slug already exist");
9797
});
98+
it("If community name shorter than 2 characters", async () => {
99+
const fakeData = {
100+
name: "s",
101+
slug: "c",
102+
description: faker.lorem.paragraph(3),
103+
};
104+
const response = await executeGraphqlOperationAsSuperAdmin<
105+
CreateCommunityMutation,
106+
CreateCommunityMutationVariables
107+
>({
108+
document: CreateCommunity,
109+
variables: {
110+
input: fakeData,
111+
},
112+
});
113+
const msg = JSON.parse(response.errors?.[0]?.message || "") as {
114+
message: string;
115+
}[];
116+
assert.equal(response.errors?.length, 1);
117+
assert.equal(
118+
msg[0].message,
119+
"String must contain at least 2 character(s)",
120+
);
121+
});
122+
it("If community name shorter than 65 characters", async () => {
123+
const fakeData = {
124+
name: faker.lorem.words(65),
125+
slug: "c",
126+
description: faker.lorem.paragraph(3),
127+
};
128+
const response = await executeGraphqlOperationAsSuperAdmin<
129+
CreateCommunityMutation,
130+
CreateCommunityMutationVariables
131+
>({
132+
document: CreateCommunity,
133+
variables: {
134+
input: fakeData,
135+
},
136+
});
137+
const msg = JSON.parse(response.errors?.[0]?.message || "") as {
138+
message: string;
139+
}[];
140+
assert.equal(response.errors?.length, 1);
141+
assert.equal(
142+
msg[0].message,
143+
"String must contain at most 64 character(s)",
144+
);
145+
});
98146
});
99147
});

0 commit comments

Comments
 (0)