From d9ecd3e3b3cd9c4ef9fbc2fa66a2a136f9772986 Mon Sep 17 00:00:00 2001 From: Maurits Lourens Date: Tue, 18 Jan 2022 11:38:39 +0100 Subject: [PATCH] make empty array valid when not required --- .../server/src/api/controllers/row/utils.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.js index 71b22375f7..3fb0cc78d8 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.js @@ -61,12 +61,17 @@ exports.validate = async ({ appId, tableId, row, table }) => { let res // Validate.js doesn't seem to handle array - if (type === FieldTypes.ARRAY && row[fieldName] && row[fieldName].length) { - row[fieldName].map(val => { - if (!constraints.inclusion.includes(val)) { - errors[fieldName] = "Field not in list" - } - }) + if (type === FieldTypes.ARRAY && row[fieldName]) { + if (row[fieldName].length) { + row[fieldName].map(val => { + if (!constraints.inclusion.includes(val)) { + errors[fieldName] = "Field not in list" + } + }) + } else if (constraints.presence && row[fieldName].length === 0) { + // non required MultiSelect creates an empty array, which should not throw errors + errors[fieldName] = [`${fieldName} is required`] + } } else if (type === FieldTypes.JSON && typeof row[fieldName] === "string") { // this should only happen if there is an error try {