mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
add kebab case validation to rename page handle (#10352)
This commit is contained in:
parent
41c0111418
commit
f905f96299
2 changed files with 32 additions and 2 deletions
|
|
@ -3,6 +3,8 @@ import { Modal } from 'react-bootstrap';
|
|||
import { Button } from '@/_ui/LeftSidebar';
|
||||
import { Alert } from '@/_ui/Alert';
|
||||
import { EditInput } from './EditInput';
|
||||
import { validateKebabCase } from '@/_helpers/utils';
|
||||
import _ from 'lodash';
|
||||
|
||||
export const EditModal = ({ slug, page, show, handleClose, updatePageHandle, darkMode }) => {
|
||||
const [pageHandle, setPageHandle] = useState(page.handle);
|
||||
|
|
@ -17,9 +19,14 @@ export const EditModal = ({ slug, page, show, handleClose, updatePageHandle, dar
|
|||
if (pageHandle === page.handle) {
|
||||
return handleClose();
|
||||
}
|
||||
|
||||
const { isValid, error } = validateKebabCase(pageHandle);
|
||||
if (!isValid) {
|
||||
setError(error);
|
||||
return;
|
||||
}
|
||||
const transformedPageHandle = _.kebabCase(pageHandle);
|
||||
setIsSaving(true);
|
||||
updatePageHandle(page.id, pageHandle);
|
||||
updatePageHandle(page.id, transformedPageHandle);
|
||||
setTimeout(() => {
|
||||
setIsSaving(false);
|
||||
return handleClose();
|
||||
|
|
|
|||
|
|
@ -318,6 +318,29 @@ export function validateQueryName(name) {
|
|||
return nameRegex.test(name);
|
||||
}
|
||||
|
||||
export function validateKebabCase(slug) {
|
||||
const pattern = /^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/;
|
||||
if (slug === '') {
|
||||
return { isValid: false, error: 'Handle cannot be empty.' };
|
||||
}
|
||||
if (!/^[a-zA-Z0-9]/.test(slug)) {
|
||||
return { isValid: false, error: 'Handle must start with a letter or number.' };
|
||||
}
|
||||
if (/[^a-zA-Z0-9-]/.test(slug)) {
|
||||
return { isValid: false, error: 'Handle can only contain letters, numbers, and hyphens.' };
|
||||
}
|
||||
if (/--/.test(slug)) {
|
||||
return { isValid: false, error: 'Handle cannot contain consecutive hyphens.' };
|
||||
}
|
||||
if (slug.endsWith('-')) {
|
||||
return { isValid: false, error: 'Handle cannot end with a hyphen.' };
|
||||
}
|
||||
if (!pattern.test(slug)) {
|
||||
return { isValid: false, error: 'Handle does not match the kebab-case pattern.' };
|
||||
}
|
||||
return { isValid: true, error: null };
|
||||
}
|
||||
|
||||
export const convertToKebabCase = (string) =>
|
||||
string
|
||||
.replace(/([a-z])([A-Z])/g, '$1-$2')
|
||||
|
|
|
|||
Loading…
Reference in a new issue