From 8dfef0d3427922fd40d8c53970f4bc7360937973 Mon Sep 17 00:00:00 2001 From: Maurits Lourens Date: Mon, 16 Aug 2021 21:15:15 +0200 Subject: [PATCH 1/2] return validation errors --- .../components/backend/DataTable/modals/CreateEditRow.svelte | 5 ++++- packages/server/src/api/controllers/row/internal.js | 4 ++-- packages/server/src/api/index.js | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte index 06ab8fd4a9..e99f294380 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte @@ -31,7 +31,10 @@ .flat() // Prevent modal closing if there were errors return false - } else if (rowResponse.status === 400 || rowResponse.status === 500) { + } else if (rowResponse.status === 400 && rowResponse.validationErrors) { + errors = Object.keys(rowResponse.validationErrors).map(field => ({message: `${field} ${rowResponse.validationErrors[field][0]}`})) + return false + } else if (rowResponse.status === 500) { errors = [{ message: rowResponse.message }] return false } diff --git a/packages/server/src/api/controllers/row/internal.js b/packages/server/src/api/controllers/row/internal.js index 28c88c1eac..e39bc2aff9 100644 --- a/packages/server/src/api/controllers/row/internal.js +++ b/packages/server/src/api/controllers/row/internal.js @@ -57,7 +57,7 @@ exports.patch = async ctx => { }) if (!validateResult.valid) { - throw validateResult.errors + throw { validation: validateResult.errors } } // returned row is cleaned and prepared for writing to DB @@ -105,7 +105,7 @@ exports.save = async function (ctx) { }) if (!validateResult.valid) { - throw validateResult.errors + throw { validation: validateResult.errors } } // make sure link rows are up to date diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index 81601eea1a..f6e5648a26 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -58,6 +58,7 @@ router.use(async (ctx, next) => { ctx.body = { message: err.message, status: ctx.status, + validationErrors: err.validation } if (env.NODE_ENV !== "jest") { ctx.log.error(err) From a2e7fe91dd1d29b25ccec6157e4db3eda5cc1488 Mon Sep 17 00:00:00 2001 From: Maurits Lourens Date: Mon, 16 Aug 2021 22:07:15 +0200 Subject: [PATCH 2/2] fix lint issues --- .../components/backend/DataTable/modals/CreateEditRow.svelte | 4 +++- packages/server/src/api/index.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte index e99f294380..ce52287c99 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte @@ -32,7 +32,9 @@ // Prevent modal closing if there were errors return false } else if (rowResponse.status === 400 && rowResponse.validationErrors) { - errors = Object.keys(rowResponse.validationErrors).map(field => ({message: `${field} ${rowResponse.validationErrors[field][0]}`})) + errors = Object.keys(rowResponse.validationErrors).map(field => ({ + message: `${field} ${rowResponse.validationErrors[field][0]}`, + })) return false } else if (rowResponse.status === 500) { errors = [{ message: rowResponse.message }] diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index f6e5648a26..c473bf619b 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -58,7 +58,7 @@ router.use(async (ctx, next) => { ctx.body = { message: err.message, status: ctx.status, - validationErrors: err.validation + validationErrors: err.validation, } if (env.NODE_ENV !== "jest") { ctx.log.error(err)