From 003b6424a294291c702d45df5dcb889a4224888e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 29 Nov 2021 17:54:09 +0000 Subject: [PATCH] Fixing issue with updating row validation to allow empty objects. --- packages/server/src/api/controllers/row/utils.js | 12 ++++-------- packages/server/src/utilities/rowProcessor/index.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.js index f7a4b13304..83c97ceb78 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.js @@ -50,10 +50,10 @@ exports.validate = async ({ appId, tableId, row, table }) => { const errors = {} for (let fieldName of Object.keys(table.schema)) { const constraints = cloneDeep(table.schema[fieldName].constraints) + const type = table.schema[fieldName].type // special case for options, need to always allow unselected (null) if ( - table.schema[fieldName].type === - (FieldTypes.OPTIONS || FieldTypes.ARRAY) && + (type === FieldTypes.OPTIONS || type === FieldTypes.ARRAY) && constraints.inclusion ) { constraints.inclusion.push(null) @@ -61,17 +61,13 @@ exports.validate = async ({ appId, tableId, row, table }) => { let res // Validate.js doesn't seem to handle array - if ( - table.schema[fieldName].type === FieldTypes.ARRAY && - row[fieldName] && - row[fieldName].length - ) { + if (type === FieldTypes.ARRAY && row[fieldName] && row[fieldName].length) { row[fieldName].map(val => { if (!constraints.inclusion.includes(val)) { errors[fieldName] = "Field not in list" } }) - } else if (table.schema[fieldName].type === FieldTypes.FORMULA) { + } else if (type === FieldTypes.FORMULA) { res = validateJs.single( processStringSync(table.schema[fieldName].formula, row), constraints diff --git a/packages/server/src/utilities/rowProcessor/index.js b/packages/server/src/utilities/rowProcessor/index.js index ea63c23f7d..860063f173 100644 --- a/packages/server/src/utilities/rowProcessor/index.js +++ b/packages/server/src/utilities/rowProcessor/index.js @@ -81,6 +81,18 @@ const TYPE_TRANSFORM_MAP = { [FieldTypes.AUTO]: { parse: () => undefined, }, + [FieldTypes.JSON]: { + parse: input => { + try { + if (input === "") { + return undefined + } + return JSON.parse(input) + } catch (err) { + return input + } + }, + }, } /**