mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-07 20:37:44 +00:00
fix(organization): prevent empty name and slug in create/update (#5100)
This commit is contained in:
@@ -101,6 +101,31 @@ describe("organization", async (it) => {
|
|||||||
expect(existingSlug.error?.status).toBe(400);
|
expect(existingSlug.error?.status).toBe(400);
|
||||||
expect(existingSlug.error?.message).toBe("slug is taken");
|
expect(existingSlug.error?.message).toBe("slug is taken");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should prevent creating organization with empty slug", async () => {
|
||||||
|
const { headers } = await signInWithTestUser();
|
||||||
|
const organization = await client.organization.create({
|
||||||
|
name: "test-empty-slug",
|
||||||
|
slug: "",
|
||||||
|
fetchOptions: {
|
||||||
|
headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(organization.error?.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should prevent creating organization with empty name", async () => {
|
||||||
|
const { headers } = await signInWithTestUser();
|
||||||
|
const organization = await client.organization.create({
|
||||||
|
name: "",
|
||||||
|
slug: "test-empty-name",
|
||||||
|
fetchOptions: {
|
||||||
|
headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(organization.error?.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
it("should create organization directly in the server without cookie", async () => {
|
it("should create organization directly in the server without cookie", async () => {
|
||||||
const session = await client.getSession({
|
const session = await client.getSession({
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
@@ -160,6 +185,34 @@ describe("organization", async (it) => {
|
|||||||
expect(organization.data?.metadata?.test).toBe("test2");
|
expect(organization.data?.metadata?.test).toBe("test2");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should prevent updating organization to empty slug", async () => {
|
||||||
|
const { headers } = await signInWithTestUser();
|
||||||
|
const organization = await client.organization.update({
|
||||||
|
organizationId,
|
||||||
|
data: {
|
||||||
|
slug: "",
|
||||||
|
},
|
||||||
|
fetchOptions: {
|
||||||
|
headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(organization.error?.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should prevent updating organization to empty name", async () => {
|
||||||
|
const { headers } = await signInWithTestUser();
|
||||||
|
const organization = await client.organization.update({
|
||||||
|
organizationId,
|
||||||
|
data: {
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
fetchOptions: {
|
||||||
|
headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(organization.error?.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
it("should allow activating organization and set session", async () => {
|
it("should allow activating organization and set session", async () => {
|
||||||
const organization = await client.organization.setActive({
|
const organization = await client.organization.setActive({
|
||||||
organizationId,
|
organizationId,
|
||||||
|
|||||||
@@ -29,10 +29,10 @@ export const createOrganization = <O extends OrganizationOptions>(
|
|||||||
isClientSide: true,
|
isClientSide: true,
|
||||||
});
|
});
|
||||||
const baseSchema = z.object({
|
const baseSchema = z.object({
|
||||||
name: z.string().meta({
|
name: z.string().min(1).meta({
|
||||||
description: "The name of the organization",
|
description: "The name of the organization",
|
||||||
}),
|
}),
|
||||||
slug: z.string().meta({
|
slug: z.string().min(1).meta({
|
||||||
description: "The slug of the organization",
|
description: "The slug of the organization",
|
||||||
}),
|
}),
|
||||||
userId: z.coerce
|
userId: z.coerce
|
||||||
@@ -386,12 +386,14 @@ export const updateOrganization = <O extends OrganizationOptions>(
|
|||||||
...additionalFieldsSchema.shape,
|
...additionalFieldsSchema.shape,
|
||||||
name: z
|
name: z
|
||||||
.string()
|
.string()
|
||||||
|
.min(1)
|
||||||
.meta({
|
.meta({
|
||||||
description: "The name of the organization",
|
description: "The name of the organization",
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
slug: z
|
slug: z
|
||||||
.string()
|
.string()
|
||||||
|
.min(1)
|
||||||
.meta({
|
.meta({
|
||||||
description: "The slug of the organization",
|
description: "The slug of the organization",
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user