Skip to content

Commit

Permalink
fix: Fix name validation consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-treason committed Oct 4, 2023
1 parent bf2d122 commit 249f318
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useFormik } from 'formik';
import { addEnvironment } from 'providers/ReduxStore/slices/collections/actions';
import * as Yup from 'yup';
import { useDispatch } from 'react-redux';
import { filenameRegex } from 'utils/common/regex';

const CreateEnvironment = ({ collection, onClose }) => {
const dispatch = useDispatch();
Expand All @@ -19,6 +20,7 @@ const CreateEnvironment = ({ collection, onClose }) => {
name: Yup.string()
.min(1, 'must be atleast 1 characters')
.max(50, 'must be 50 characters or less')
.matches(filenameRegex, 'Folder name contains invalid characters')
.required('name is required')
}),
onSubmit: (values) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useFormik } from 'formik';
import { renameEnvironment } from 'providers/ReduxStore/slices/collections/actions';
import * as Yup from 'yup';
import { useDispatch } from 'react-redux';
import { filenameRegex } from 'utils/common/regex';

const RenameEnvironment = ({ onClose, environment, collection }) => {
const dispatch = useDispatch();
Expand All @@ -19,6 +20,7 @@ const RenameEnvironment = ({ onClose, environment, collection }) => {
name: Yup.string()
.min(1, 'must be atleast 1 characters')
.max(50, 'must be 50 characters or less')
.matches(filenameRegex, 'Folder name contains invalid characters')
.required('name is required')
}),
onSubmit: (values) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Modal from 'components/Modal';
import { useDispatch } from 'react-redux';
import { isItemAFolder } from 'utils/tabs';
import { renameItem } from 'providers/ReduxStore/slices/collections/actions';
import { filenameRegex } from 'utils/common/regex';

const RenameCollectionItem = ({ collection, item, onClose }) => {
const dispatch = useDispatch();
Expand All @@ -19,6 +20,7 @@ const RenameCollectionItem = ({ collection, item, onClose }) => {
name: Yup.string()
.min(1, 'must be atleast 1 characters')
.max(50, 'must be 50 characters or less')
.matches(filenameRegex, `${isFolder ? 'folder' : 'request'} name contains invalid characters`)
.required('name is required')
}),
onSubmit: (values) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createCollection } from 'providers/ReduxStore/slices/collections/action
import toast from 'react-hot-toast';
import Tooltip from 'components/Tooltip';
import Modal from 'components/Modal';
import { filenameRegex } from 'utils/common/regex';

const CreateCollection = ({ onClose }) => {
const inputRef = useRef();
Expand All @@ -27,7 +28,7 @@ const CreateCollection = ({ onClose }) => {
collectionFolderName: Yup.string()
.min(1, 'must be atleast 1 characters')
.max(50, 'must be 50 characters or less')
.matches(/^[\w\-. ]+$/, 'Folder name contains invalid characters')
.matches(filenameRegex, 'Folder name contains invalid characters')
.required('folder name is required'),
collectionLocation: Yup.string()
.min(1, 'location is required')
Expand Down
2 changes: 2 additions & 0 deletions packages/bruno-app/src/components/Sidebar/NewFolder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Yup from 'yup';
import Modal from 'components/Modal';
import { useDispatch } from 'react-redux';
import { newFolder } from 'providers/ReduxStore/slices/collections/actions';
import { filenameRegex } from 'utils/common/regex';

const NewFolder = ({ collection, item, onClose }) => {
const dispatch = useDispatch();
Expand All @@ -18,6 +19,7 @@ const NewFolder = ({ collection, item, onClose }) => {
folderName: Yup.string()
.min(1, 'must be atleast 1 characters')
.required('name is required')
.matches(filenameRegex, 'Folder name contains invalid characters')
.test({
name: 'folderName',
message: 'The folder name "environments" at the root of the collection is reserved in bruno',
Expand Down
2 changes: 2 additions & 0 deletions packages/bruno-app/src/components/Sidebar/NewRequest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { addTab } from 'providers/ReduxStore/slices/tabs';
import HttpMethodSelector from 'components/RequestPane/QueryUrl/HttpMethodSelector';
import { getDefaultRequestPaneTab } from 'utils/collections';
import StyledWrapper from './StyledWrapper';
import { filenameRegex } from 'utils/common/regex';

const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
const dispatch = useDispatch();
Expand All @@ -27,6 +28,7 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
requestName: Yup.string()
.min(1, 'must be atleast 1 characters')
.required('name is required')
.matches(filenameRegex, 'request name contains invalid characters')
.test({
name: 'requestName',
message: 'The request name "index" is reserved in bruno',
Expand Down
3 changes: 3 additions & 0 deletions packages/bruno-app/src/utils/common/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Regex for validating filenames that covers most cases
// See https://github.com/usebruno/bruno/pull/349 for more info
export const filenameRegex = /^(?!CON|PRN|AUX|NUL|COM\d|LPT\d|^ |^\-)[\w\-\. \(\)\[\]]+[^\. ]$/
5 changes: 0 additions & 5 deletions packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { ipcMain, shell } = require('electron');
const { envJsonToBru, bruToJson, jsonToBru } = require('../bru');

const {
isValidPathname,
writeFile,
hasBruExtension,
isDirectory,
Expand Down Expand Up @@ -51,10 +50,6 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
throw new Error(`collection: ${dirPath} already exists`);
}

if (!isValidPathname(dirPath)) {
throw new Error(`collection: invalid pathname - ${dir}`);
}

await createDirectory(dirPath);

const uid = generateUidBasedOnHash(dirPath);
Expand Down

0 comments on commit 249f318

Please sign in to comment.