Skip to content

Commit

Permalink
feat(frontend): validate property name format
Browse files Browse the repository at this point in the history
  • Loading branch information
alice.juan committed Feb 8, 2024
1 parent 0aea13a commit 33c4b45
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Box, Grid, Paper, Typography } from "@mui/material";
import MaterialTable, { MTableToolbar } from "@material-table/core";
import { useState } from "react";
import ISO6391, { LanguageCode } from "iso-639-1";

type RowType = {
propertyName: string;
Expand Down Expand Up @@ -65,14 +66,33 @@ const ListAllEntryProperties = ({ nodeObject, setNodeObject }) => {
});
};

const LanguageCodes = ISO6391.getAllCodes();

const validatePropertyName = (propertyName:string) : boolean => {
// Every property name should be in the form property_name:lang_code
if (propertyName) {
const [, langCode] = propertyName.split(':');
if (!LanguageCodes.includes(langCode as LanguageCode)) {
return false;
}
// Property name should not include special caracters
const pattern = /^[a-zA-Z0-9_]+:[a-zA-Z0-9_]+$/;
if (!pattern.test(propertyName)) {
return false;
}
return true;
}
return false;
}

return (
<Box>
{/* Properties */}
<Box sx={{ width: "90%", ml: 4, maxWidth: "1000px", m: "auto", mb: 3 }}>
<MaterialTable
data={data}
columns={[
{ title: "Name", field: "propertyName" },
{ title: "Name", field: "propertyName", validate: rowData => validatePropertyName(rowData.propertyName) ? true : { isValid: false, helperText: 'Property name should not contain special caracters and should follow the format : property_name:lang_code' } , },
{ title: "Value", field: "propertyValue" },
]}
editable={{
Expand Down

0 comments on commit 33c4b45

Please sign in to comment.