Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Validate field default values

1ilit9311c1fbaf34
diff --git a/src/components/table.jsx b/src/components/table.jsx
index 84ab2f30..a82c51a8 100644
--- a/src/components/table.jsx
+++ b/src/components/table.jsx
@@ -389,6 +389,20 @@ export default function Table(props) {
length: 255,
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 {
updateField(props.tableData.id, j, {
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>
<InputNumber
diff --git a/src/components/table_overview.jsx b/src/components/table_overview.jsx
index ca0fb235..062355bb 100644
--- a/src/components/table_overview.jsx
+++ b/src/components/table_overview.jsx
@@ -235,6 +235,20 @@ export default function TableOverview(props) {
length: 255,
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 {
updateField(i, j, {
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>
<InputNumber
diff --git a/src/utils/index.js b/src/utils/index.js
index 2e20b3e5..f3550a5d 100644
--- a/src/utils/index.js
+++ b/src/utils/index.js
@@ -115,6 +115,29 @@ function arrayIsEqual(arr1, 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) {
const issues = [];
const duplicateTableNames = {};
@@ -140,6 +163,7 @@ function validateDiagram(diagram) {
if (field.name === "") {
issues.push(`Empty field name in table "${table.name}"`);
}
+
if (field.type === "") {
issues.push(`Empty field type in table "${table.name}"`);
} 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]) {
issues.push(`Duplicate table fields in table "${table.name}"`);
} else {