fix: handle skip and slash in environment slug

This commit is contained in:
Salman
2024-03-15 20:21:27 +05:30
parent 3ae27e088f
commit ee2b8a594a
5 changed files with 27 additions and 14 deletions
@@ -22,7 +22,10 @@ export const registerProjectEnvRouter = async (server: FastifyZodProvider) => {
}), }),
body: z.object({ body: z.object({
name: z.string().trim(), name: z.string().trim(),
slug: z.string().trim() slug: z
.string()
.regex(/^[^./]*$/g)
.trim()
}), }),
response: { response: {
200: z.object({ 200: z.object({
@@ -77,7 +80,11 @@ export const registerProjectEnvRouter = async (server: FastifyZodProvider) => {
id: z.string().trim() id: z.string().trim()
}), }),
body: z.object({ body: z.object({
slug: z.string().trim().optional(), slug: z
.string()
.regex(/^[^./]*$/g)
.trim()
.optional(),
name: z.string().trim().optional(), name: z.string().trim().optional(),
position: z.number().optional() position: z.number().optional()
}), }),
@@ -17,6 +17,7 @@ type Props = {
}; };
// TODO: Migrate to better form management and validation. Preferable react-hook-form + yup // TODO: Migrate to better form management and validation. Preferable react-hook-form + yup
// We should remove this?
/** /**
* The dialog modal for when the user wants to create a new workspace * The dialog modal for when the user wants to create a new workspace
* @param {*} param0 * @param {*} param0
@@ -115,7 +115,6 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
}); });
useEffect(() => { useEffect(() => {
let currentEnvironment = propEnvironment; let currentEnvironment = propEnvironment;
let currentSecretPath = propSecretPath || "/"; let currentSecretPath = propSecretPath || "/";
@@ -134,7 +133,13 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
currentSecretPath = `/${folderPaths?.join("/")}` || "/"; currentSecretPath = `/${folderPaths?.join("/")}` || "/";
} }
if (!currentEnvironment || !decryptFileKey || !currentSecretPath || !currentWorkspace || !referenceKey) { if (
!currentEnvironment ||
!decryptFileKey ||
!currentSecretPath ||
!currentWorkspace ||
!referenceKey
) {
// this need to clean up? // this need to clean up?
setListReference(currentListReference); setListReference(currentListReference);
return; return;
@@ -142,8 +147,7 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
setSecretPath(currentSecretPath); setSecretPath(currentSecretPath);
setEnvironment(currentEnvironment); setEnvironment(currentEnvironment);
setShowReferencePopup(true); setShowReferencePopup(true);
}, [referenceKey]);
}, [referenceKey])
useEffect(() => { useEffect(() => {
const currentListReference: ReferenceType[] = []; const currentListReference: ReferenceType[] = [];
@@ -195,7 +199,6 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
setLastSelectionIndex(pos); setLastSelectionIndex(pos);
setReferenceKey(match?.[2]); setReferenceKey(match?.[2]);
} }
setShowReferencePopup(!!match); setShowReferencePopup(!!match);
} }
@@ -224,11 +227,10 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
setValue(newValue); setValue(newValue);
// TODO: there should be a better way to do // TODO: there should be a better way to do
onChange?.({ target: { value: newValue } } as any); onChange?.({ target: { value: newValue } } as any);
setCaretPos(currCaretPos);
if (event.currentTarget) { if (event.currentTarget) {
setCaretPos(currCaretPos);
setTimeout(() => { setTimeout(() => {
// on next tick
referencePopup(newValue, currCaretPos); referencePopup(newValue, currCaretPos);
}, 200); }, 200);
@@ -277,25 +279,28 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
]; ];
let oldReferenceStr = oldReference.slice(2, -1); let oldReferenceStr = oldReference.slice(2, -1);
let currentPath = type === "environment" ? slug! : name;
currentPath = currentPath.replace(/\./g, "\\.");
let replaceReference = ""; let replaceReference = "";
let offset = 3; let offset = 3;
switch (type) { switch (type) {
case "folder": case "folder":
replaceReference = `${oldReferenceStr}${name}.`; replaceReference = `${oldReferenceStr}${currentPath}.`;
offset -= 1; offset -= 1;
break; break;
case "secret": { case "secret": {
if (oldReferenceStr.indexOf(".") === -1) oldReferenceStr = ""; if (oldReferenceStr.indexOf(".") === -1) oldReferenceStr = "";
replaceReference = `${oldReferenceStr}${name}`; replaceReference = `${oldReferenceStr}${currentPath}`;
break; break;
} }
case "environment": case "environment":
replaceReference = `${slug}.`; replaceReference = `${currentPath}.`;
offset -= 1; offset -= 1;
break; break;
default: default:
} }
replaceReference = replaceReference.replace(/[//]/g, "");
newValue = `${start}$\{${replaceReference}}${end}`; newValue = `${start}$\{${replaceReference}}${end}`;
setValue(newValue); setValue(newValue);
// TODO: there should be a better way to do // TODO: there should be a better way to do
@@ -16,7 +16,7 @@ type Props = {
const schema = yup.object({ const schema = yup.object({
environmentName: yup.string().label("Environment Name").required(), environmentName: yup.string().label("Environment Name").required(),
environmentSlug: yup.string().label("Environment Slug").required() environmentSlug: yup.string().label("Environment Slug").matches(/^[^./]*$/g, { message: "Invalid [.] or [/] not allowed"}).required()
}); });
export type FormData = yup.InferType<typeof schema>; export type FormData = yup.InferType<typeof schema>;
@@ -16,7 +16,7 @@ type Props = {
const schema = yup.object({ const schema = yup.object({
name: yup.string().label("Environment Name").required(), name: yup.string().label("Environment Name").required(),
slug: yup.string().label("Environment Slug").required() slug: yup.string().label("Environment Slug").matches(/^[^./]*$/g, { message: "Invalid [.] or [/] not allowed"}).required()
}); });
export type FormData = yup.InferType<typeof schema>; export type FormData = yup.InferType<typeof schema>;