From 4f2def6f5939656118a0ed43d9a10d24f3999ce5 Mon Sep 17 00:00:00 2001 From: hughhhh Date: Tue, 22 Jun 2021 17:22:19 -0400 Subject: [PATCH 1/4] temp working validation --- .../CRUD/data/database/DatabaseModal/index.tsx | 10 +++++++++- superset-frontend/src/views/CRUD/hooks.ts | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx index cdefb58da834c..4f5ab533b3a63 100644 --- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx +++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx @@ -255,7 +255,7 @@ function dbReducer( return { ...action.payload, - engine: trimmedState.engine, + engine: action.payload.backend, configuration_method: action.payload.configuration_method, extra_json: deserializeExtraJSON, parameters: { @@ -364,6 +364,14 @@ const DatabaseModal: FunctionComponent = ({ // Clone DB object const dbToUpdate = JSON.parse(JSON.stringify(update)); + + // Validtion DB + await getValidation(dbToUpdate, true); + if (validationErrors) { + console.log('got validation errors'); + return; + } + if (dbToUpdate.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM) { if (dbToUpdate?.parameters?.query) { // convert query params into dictionary diff --git a/superset-frontend/src/views/CRUD/hooks.ts b/superset-frontend/src/views/CRUD/hooks.ts index 3132422d841c9..b51ff526d70cd 100644 --- a/superset-frontend/src/views/CRUD/hooks.ts +++ b/superset-frontend/src/views/CRUD/hooks.ts @@ -638,7 +638,7 @@ export function useDatabaseValidation() { null, ); const getValidation = useCallback( - (database: Partial | null) => { + (database: Partial | null, onCreate = false) => { SupersetClient.post({ endpoint: '/api/v1/database/validate_parameters', body: JSON.stringify(database), @@ -653,7 +653,8 @@ export function useDatabaseValidation() { const parsedErrors = errors .filter( (error: { error_type: string }) => - error.error_type !== 'CONNECTION_MISSING_PARAMETERS_ERROR', + error.error_type !== + 'CONNECTION_MISSING_PARAMETERS_ERROR' || onCreate, ) .reduce( ( @@ -662,7 +663,7 @@ export function useDatabaseValidation() { extra, message, }: { - extra: { invalid?: string[] }; + extra: { invalid?: string[]; missing?: string[] }; message: string; }, ) => { @@ -672,10 +673,18 @@ export function useDatabaseValidation() { if (extra.invalid) { return { ...obj, [extra.invalid[0]]: message }; } + if (extra.missing) { + const missingFields = {}; + extra.missing.map(d => { + missingFields[d] = 'This is a required field'; + }); + return { ...obj, ...missingFields }; + } return obj; }, {}, ); + console.log(parsedErrors); setValidationErrors(parsedErrors); }); } else { From 8cd46c4ddaba74576644b34e7c038a8c903041a3 Mon Sep 17 00:00:00 2001 From: hughhhh Date: Tue, 22 Jun 2021 17:41:44 -0400 Subject: [PATCH 2/4] remove console.log --- .../src/views/CRUD/data/database/DatabaseModal/index.tsx | 1 - superset-frontend/src/views/CRUD/hooks.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx index 4f5ab533b3a63..718715fe3952d 100644 --- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx +++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx @@ -368,7 +368,6 @@ const DatabaseModal: FunctionComponent = ({ // Validtion DB await getValidation(dbToUpdate, true); if (validationErrors) { - console.log('got validation errors'); return; } diff --git a/superset-frontend/src/views/CRUD/hooks.ts b/superset-frontend/src/views/CRUD/hooks.ts index b51ff526d70cd..4e2ba19da94a6 100644 --- a/superset-frontend/src/views/CRUD/hooks.ts +++ b/superset-frontend/src/views/CRUD/hooks.ts @@ -684,7 +684,6 @@ export function useDatabaseValidation() { }, {}, ); - console.log(parsedErrors); setValidationErrors(parsedErrors); }); } else { From 55ffd8e0b78aa5525170c7fd5cc72a9d4dc1aad3 Mon Sep 17 00:00:00 2001 From: hughhhh Date: Tue, 22 Jun 2021 17:44:45 -0400 Subject: [PATCH 3/4] fix comment --- .../src/views/CRUD/data/database/DatabaseModal/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx index 718715fe3952d..b0c2704094bd6 100644 --- a/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx +++ b/superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx @@ -365,7 +365,7 @@ const DatabaseModal: FunctionComponent = ({ // Clone DB object const dbToUpdate = JSON.parse(JSON.stringify(update)); - // Validtion DB + // Validate DB before saving await getValidation(dbToUpdate, true); if (validationErrors) { return; From 9ae61e5b19c68f9a8d592f72045fb02b5f621e37 Mon Sep 17 00:00:00 2001 From: hughhhh Date: Tue, 22 Jun 2021 18:02:07 -0400 Subject: [PATCH 4/4] cool way to set array values --- superset-frontend/src/views/CRUD/hooks.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/superset-frontend/src/views/CRUD/hooks.ts b/superset-frontend/src/views/CRUD/hooks.ts index 4e2ba19da94a6..bd8e19cde1bd8 100644 --- a/superset-frontend/src/views/CRUD/hooks.ts +++ b/superset-frontend/src/views/CRUD/hooks.ts @@ -674,11 +674,15 @@ export function useDatabaseValidation() { return { ...obj, [extra.invalid[0]]: message }; } if (extra.missing) { - const missingFields = {}; - extra.missing.map(d => { - missingFields[d] = 'This is a required field'; - }); - return { ...obj, ...missingFields }; + return { + ...obj, + ...Object.assign( + {}, + ...extra.missing.map(field => ({ + [field]: 'This is a required field', + })), + ), + }; } return obj; },