Skip to content

Commit

Permalink
fix: validation on edit (#15310)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh authored Jun 22, 2021
1 parent 5de931a commit 725f406
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function dbReducer(

return {
...action.payload,
engine: trimmedState.engine,
engine: action.payload.backend,
configuration_method: action.payload.configuration_method,
extra_json: deserializeExtraJSON,
parameters: {
Expand Down Expand Up @@ -365,6 +365,13 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({

// Clone DB object
const dbToUpdate = JSON.parse(JSON.stringify(update));

// Validate DB before saving
await getValidation(dbToUpdate, true);
if (validationErrors) {
return;
}

if (dbToUpdate.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM) {
if (dbToUpdate?.parameters?.query) {
// convert query params into dictionary
Expand Down
18 changes: 15 additions & 3 deletions superset-frontend/src/views/CRUD/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ export function useDatabaseValidation() {
null,
);
const getValidation = useCallback(
(database: Partial<DatabaseObject> | null) => {
(database: Partial<DatabaseObject> | null, onCreate = false) => {
SupersetClient.post({
endpoint: '/api/v1/database/validate_parameters',
body: JSON.stringify(database),
Expand All @@ -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(
(
Expand All @@ -662,7 +663,7 @@ export function useDatabaseValidation() {
extra,
message,
}: {
extra: { invalid?: string[] };
extra: { invalid?: string[]; missing?: string[] };
message: string;
},
) => {
Expand All @@ -672,6 +673,17 @@ export function useDatabaseValidation() {
if (extra.invalid) {
return { ...obj, [extra.invalid[0]]: message };
}
if (extra.missing) {
return {
...obj,
...Object.assign(
{},
...extra.missing.map(field => ({
[field]: 'This is a required field',
})),
),
};
}
return obj;
},
{},
Expand Down

0 comments on commit 725f406

Please sign in to comment.