Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation on edit #15310

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -364,6 +364,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