From 1da37b1fa553e9a5b2337db88b563b99b8970377 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Thu, 2 Jul 2020 21:09:49 +0100 Subject: [PATCH] #290 field name validation --- .../nav/ModelSetupNav/ModelSetupNav.svelte | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/builder/src/components/nav/ModelSetupNav/ModelSetupNav.svelte b/packages/builder/src/components/nav/ModelSetupNav/ModelSetupNav.svelte index 39be32bf07..e1f4d3601b 100644 --- a/packages/builder/src/components/nav/ModelSetupNav/ModelSetupNav.svelte +++ b/packages/builder/src/components/nav/ModelSetupNav/ModelSetupNav.svelte @@ -52,7 +52,35 @@ }) } + function validate() { + let errors = [] + for (let field of Object.values($backendUiStore.draftModel.schema)) { + const restrictedFieldNames = ["type", "modelId"] + if (field.name.startsWith("_")) { + errors.push(`field '${field.name}' - name cannot begin with '_''`) + } else if (restrictedFieldNames.includes(field.name)) { + errors.push(`field '${field.name}' - is a restricted name, please rename`) + } else if (!field.name || !field.name.trim()) { + errors.push("field name cannot be blank") + } + } + + if (!$backendUiStore.draftModel.name) { + errors.push("Table name cannot be blank") + } + + return errors + } + async function saveModel() { + const errors = validate() + if (errors.length > 0) { + notifier.danger( + errors.join("/n") + ) + return + } + await backendUiStore.actions.models.save({ model: $backendUiStore.draftModel, })