Validate field default values

This commit is contained in:
1ilit
2023-09-19 15:51:21 +03:00
parent 81792a5243
commit 9311c1fbaf
3 changed files with 61 additions and 2 deletions

View File

@@ -389,6 +389,20 @@ export default function Table(props) {
length: 255, length: 255,
increment: incr, increment: incr,
}); });
} else if (
f.type === "BLOB" ||
f.type === "JSON" ||
f.type === "GEOMETRY" ||
f.type === "TEXT" ||
incr
) {
updateField(props.tableData.id, j, {
type: value,
increment: incr,
default: "",
length: "",
values: [],
});
} else { } else {
updateField(props.tableData.id, j, { updateField(props.tableData.id, j, {
type: value, type: value,
@@ -542,7 +556,7 @@ export default function Table(props) {
/> />
</> </>
)} )}
{f.type === "VARCHAR" && ( {(f.type === "VARCHAR" || f.type === "CHAR") && (
<> <>
<div className="font-semibold">Length</div> <div className="font-semibold">Length</div>
<InputNumber <InputNumber

View File

@@ -235,6 +235,20 @@ export default function TableOverview(props) {
length: 255, length: 255,
increment: incr, increment: incr,
}); });
} else if (
f.type === "BLOB" ||
f.type === "JSON" ||
f.type === "GEOMETRY" ||
f.type === "TEXT" ||
incr
) {
updateField(props.tableData.id, j, {
type: value,
increment: incr,
default: "",
length: "",
values: [],
});
} else { } else {
updateField(i, j, { updateField(i, j, {
type: value, type: value,
@@ -389,7 +403,7 @@ export default function TableOverview(props) {
/> />
</> </>
)} )}
{f.type === "VARCHAR" && ( {(f.type === "VARCHAR" || f.type === "CHAR") && (
<> <>
<div className="font-semibold">Length</div> <div className="font-semibold">Length</div>
<InputNumber <InputNumber

View File

@@ -115,6 +115,29 @@ function arrayIsEqual(arr1, arr2) {
return JSON.stringify(arr1) === JSON.stringify(arr2); return JSON.stringify(arr1) === JSON.stringify(arr2);
} }
function checkDefault(field) {
if (field.default === "") return true;
switch (field.type) {
case "INT":
case "BIGINT":
case "SMALLINT":
return /^\d*$/.test(field.default);
case "ENUM":
case "SET":
return field.values.includes(field.default);
case "CHAR":
case "VARCHAR":
return field.default.length <= field.length;
case "BOOLEAN":
return (
field.default.trim() === "false" || field.default.trim() === "true"
);
default:
return true;
}
}
function validateDiagram(diagram) { function validateDiagram(diagram) {
const issues = []; const issues = [];
const duplicateTableNames = {}; const duplicateTableNames = {};
@@ -140,6 +163,7 @@ function validateDiagram(diagram) {
if (field.name === "") { if (field.name === "") {
issues.push(`Empty field name in table "${table.name}"`); issues.push(`Empty field name in table "${table.name}"`);
} }
if (field.type === "") { if (field.type === "") {
issues.push(`Empty field type in table "${table.name}"`); issues.push(`Empty field type in table "${table.name}"`);
} else if (field.type === "ENUM" || field.type === "SET") { } else if (field.type === "ENUM" || field.type === "SET") {
@@ -149,6 +173,13 @@ function validateDiagram(diagram) {
); );
} }
} }
if (!checkDefault(field)) {
issues.push(
`Default value for field "${field.name}" in table "${table.name}" does not match its type.`
);
}
if (duplicateFieldNames[field.name]) { if (duplicateFieldNames[field.name]) {
issues.push(`Duplicate table fields in table "${table.name}"`); issues.push(`Duplicate table fields in table "${table.name}"`);
} else { } else {