Add validation to org slug

This commit is contained in:
Tuan Dang
2024-02-07 09:39:06 -08:00
parent fc7015de83
commit 9011394c34
3 changed files with 23 additions and 5 deletions
@@ -88,7 +88,11 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
params: z.object({ organizationId: z.string().trim() }), params: z.object({ organizationId: z.string().trim() }),
body: z.object({ body: z.object({
name: z.string().trim().optional(), name: z.string().trim().optional(),
slug: z.string().trim().optional(), slug: z
.string()
.trim()
.regex(/^[a-zA-Z0-9-]+$/, "Name must only contain alphanumeric characters or hyphens")
.optional(),
authEnforced: z.boolean().optional() authEnforced: z.boolean().optional()
}), }),
response: { response: {
+13 -3
View File
@@ -121,11 +121,17 @@ export const orgServiceFactory = ({
/* /*
* Update organization details * Update organization details
* */ * */
const updateOrg = async ({ actor, actorId, actorOrgScope, orgId, data }: TUpdateOrgDTO) => { const updateOrg = async ({
actor,
actorId,
actorOrgScope,
orgId,
data: { name, slug, authEnforced }
}: TUpdateOrgDTO) => {
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorOrgScope); const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorOrgScope);
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Edit, OrgPermissionSubjects.Settings); ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Edit, OrgPermissionSubjects.Settings);
if (data.authEnforced) { if (authEnforced) {
const samlCfg = await samlConfigDAL.findEnforceableSamlCfg(orgId); const samlCfg = await samlConfigDAL.findEnforceableSamlCfg(orgId);
if (!samlCfg) if (!samlCfg)
throw new BadRequestError({ throw new BadRequestError({
@@ -134,7 +140,11 @@ export const orgServiceFactory = ({
}); });
} }
const org = await orgDAL.updateById(orgId, data); const org = await orgDAL.updateById(orgId, {
name,
slug: slug ? slugify(slug) : slug,
authEnforced
});
if (!org) throw new BadRequestError({ name: "Org not found", message: "Organization not found" }); if (!org) throw new BadRequestError({ name: "Org not found", message: "Organization not found" });
return org; return org;
}; };
@@ -10,7 +10,11 @@ import { OrgPermissionActions, OrgPermissionSubjects, useOrganization } from "@a
import { useUpdateOrg } from "@app/hooks/api"; import { useUpdateOrg } from "@app/hooks/api";
const formSchema = yup.object({ const formSchema = yup.object({
slug: yup.string().required().label("Project Slug") slug: yup
.string()
.matches(/^[a-zA-Z0-9-]+$/, "Name must only contain alphanumeric characters or hyphens")
.required()
.label("Project Slug")
}); });
type FormData = yup.InferType<typeof formSchema>; type FormData = yup.InferType<typeof formSchema>;