From 514da55923cd9e44ca0893be6fdd126d0d40cc8d Mon Sep 17 00:00:00 2001 From: Sanjai Kumar <84461672+sanjai0py@users.noreply.github.com> Date: Sat, 30 Nov 2024 19:16:09 +0530 Subject: [PATCH] Fix/improve env modal error when duplicate names (#3557) * Enhance environment creation validation and improve error handling Co-authored-by: Anoop M D --- .../EnvironmentSettings/CreateEnvironment/index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/bruno-app/src/components/Environments/EnvironmentSettings/CreateEnvironment/index.js b/packages/bruno-app/src/components/Environments/EnvironmentSettings/CreateEnvironment/index.js index a731328c8..95dc550c8 100644 --- a/packages/bruno-app/src/components/Environments/EnvironmentSettings/CreateEnvironment/index.js +++ b/packages/bruno-app/src/components/Environments/EnvironmentSettings/CreateEnvironment/index.js @@ -10,6 +10,12 @@ import Modal from 'components/Modal'; const CreateEnvironment = ({ collection, onClose }) => { const dispatch = useDispatch(); const inputRef = useRef(); + + // todo: Add this to global env too. + const validateEnvironmentName = (name) => { + return !collection.environments.some((env) => env?.name?.toLowerCase().trim() === name?.toLowerCase().trim()); + }; + const formik = useFormik({ enableReinitialize: true, initialValues: { @@ -17,9 +23,10 @@ const CreateEnvironment = ({ collection, onClose }) => { }, validationSchema: Yup.object({ name: Yup.string() - .min(1, 'must be at least 1 character') - .max(50, 'must be 50 characters or less') - .required('name is required') + .min(1, 'Must be at least 1 character') + .max(50, 'Must be 50 characters or less') + .required('Name is required') + .test('duplicate-name', 'Environment already exists', validateEnvironmentName) }), onSubmit: (values) => { dispatch(addEnvironment(values.name, collection.uid))