From 4fa36727e40cdbecb2640661fa439ee1846df107 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Thu, 26 Aug 2021 14:04:18 +0100 Subject: [PATCH] Update validation for array type --- .../backend/DataTable/RowFieldControl.svelte | 2 +- .../DataTable/modals/CreateEditColumn.svelte | 9 +---- .../FilterEditor/FilterDrawer.svelte | 7 ++-- .../ValidationEditor/ValidationDrawer.svelte | 16 +++------ .../builder/src/constants/backend/index.js | 2 +- .../server/src/api/controllers/row/utils.js | 2 +- .../src/forms/optionsParser.js | 3 -- .../src/forms/validation.js | 35 ++++++------------- 8 files changed, 21 insertions(+), 55 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/RowFieldControl.svelte b/packages/builder/src/components/backend/DataTable/RowFieldControl.svelte index 5b3a3689ba..1fa8b1ac06 100644 --- a/packages/builder/src/components/backend/DataTable/RowFieldControl.svelte +++ b/packages/builder/src/components/backend/DataTable/RowFieldControl.svelte @@ -34,7 +34,7 @@ {:else if type === "boolean"} {:else if type === "array"} - + {:else if type === "link"} {:else if type === "longform"} diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 52393e5666..1a7d75f28a 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -29,7 +29,6 @@ const AUTO_TYPE = "auto" const FORMULA_TYPE = FIELDS.FORMULA.type - const ARRAY_TYPE = FIELDS.ARRAY.type const LINK_TYPE = FIELDS.LINK.type let fieldDefinitions = cloneDeep(FIELDS) const { hide } = getContext(Context.Modal) @@ -82,12 +81,6 @@ if (field.type === AUTO_TYPE) { field = buildAutoColumn($tables.draft.name, field.name, field.subtype) } - if (field.type === ARRAY_TYPE) { - let arr = field.constraints.inclusion - let newArr = [] - newArr.push(arr) - field.constraints.inclusion = newArr - } tables.saveField({ originalName, field, @@ -272,7 +265,7 @@ {:else if field.type === "array"} {:else if field.type === "datetime"} { const schema = schemaFields.find(x => x.name === field) - const opt = - schema.type == "array" - ? schema?.constraints?.inclusion[0] - : schema?.constraints?.inclusion || [] + const opt = schema?.constraints?.inclusion || [] return opt } @@ -136,7 +133,7 @@ /> {:else if ["string", "longform", "number"].includes(filter.type)} - {:else if filter.type === "options" || filter.type === "array"} + {:else if filter.type === "options"} (rule.value = e.detail)} /> - {:else if ["maxLength", "minLength", "regex", "notRegex", "containsRowID", "notContainsRowID"].includes(rule.constraint)} + {:else if ["maxLength", "minLength", "regex", "notRegex"].includes(rule.constraint)} x} getOptionValue={x => x} on:change={e => (rule.value = e.detail)} diff --git a/packages/builder/src/constants/backend/index.js b/packages/builder/src/constants/backend/index.js index 2ce88c9648..088db024a6 100644 --- a/packages/builder/src/constants/backend/index.js +++ b/packages/builder/src/constants/backend/index.js @@ -27,7 +27,7 @@ export const FIELDS = { }, }, ARRAY: { - name: "List", + name: "Multi-select", type: "array", constraints: { type: "array", diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.js index bdb7e03785..9a518ff023 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.js @@ -69,7 +69,7 @@ exports.validate = async ({ appId, tableId, row, table }) => { // Validate.js doesn't seem to handle array of array very well if (table.schema[fieldName].type === FieldTypes.ARRAY) { row[fieldName].map(val => { - if (constraints.inclusion.includes(val)) { + if (!constraints.inclusion.includes(val)) { errors[fieldName] = "Field not in list" } }) diff --git a/packages/standard-components/src/forms/optionsParser.js b/packages/standard-components/src/forms/optionsParser.js index c79b2a4776..dd37989be7 100644 --- a/packages/standard-components/src/forms/optionsParser.js +++ b/packages/standard-components/src/forms/optionsParser.js @@ -9,9 +9,6 @@ export const getOptions = ( const isArray = fieldSchema?.type === "array" // Take options from schema if (optionsSource == null || optionsSource === "schema") { - if (isArray) { - return fieldSchema?.constraints?.inclusion[0] ?? [] - } return fieldSchema?.constraints?.inclusion ?? [] } diff --git a/packages/standard-components/src/forms/validation.js b/packages/standard-components/src/forms/validation.js index 2505e032d3..2e3b9390fb 100644 --- a/packages/standard-components/src/forms/validation.js +++ b/packages/standard-components/src/forms/validation.js @@ -64,9 +64,8 @@ export const createValidatorFromConstraints = ( // Inclusion constraint if ( - !schemaConstraints.type == "array" - ? exists(schemaConstraints.inclusion) - : false + exists(schemaConstraints.inclusion) && + schemaConstraints.type !== "array" ) { const options = schemaConstraints.inclusion || [] rules.push({ @@ -77,13 +76,11 @@ export const createValidatorFromConstraints = ( }) } - // Handle the array type but link also returns as an array, so handle via the inclusion check if ( - schemaConstraints.type == "array" && schemaConstraints.inclusion - ? exists(schemaConstraints.inclusion[0]) - : false + schemaConstraints.type === "array" && + exists(schemaConstraints.inclusion) ) { - const options = schemaConstraints.inclusion[0] || [] + const options = schemaConstraints.inclusion || [] rules.push({ type: "array", constraint: "inclusion", @@ -297,26 +294,18 @@ const notRegexHandler = (value, rule) => { } // Evaluates a contains constraint -const containsRowIDHandler = (value, rule) => { +const containsHandler = (value, rule) => { + if (rule.type == "array") { + const expectedValue = parseType(rule.value, "array") + return value && value.some(val => expectedValue.includes(val)) + } const expectedValue = parseType(rule.value, "string") return value && value.includes(expectedValue) } -// Evaluates a not contains constraint -const notContainsRowIDHandler = (value, rule) => { - return !containsHandler(value, rule) -} - -// Evaluates a contains constraint -const containsHandler = (value, rule) => { - const ruleValue = parseType(rule.value, "array") - return value && value.some(val => ruleValue.includes(val)) -} - // Evaluates a not contains constraint const notContainsHandler = (value, rule) => { - const ruleValue = parseType(rule.value, "array") - return value && !value.some(val => ruleValue.includes(val)) + return !containsHandler(value, rule) } /** @@ -333,8 +322,6 @@ const handlerMap = { notEqual: notEqualHandler, regex: regexHandler, notRegex: notRegexHandler, - containsRowID: containsRowIDHandler, - notContainsRowID: notContainsRowIDHandler, contains: containsHandler, notContains: notContainsHandler, }