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

Defer validation to form change #752

Merged
merged 4 commits into from
Apr 17, 2024
Merged
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 @@ -9,10 +9,11 @@ import projectGlobalSchema from "../../../../../../../schemas/json/project/globa
import { merge } from "../../utils.js";
import { onThrow } from "../../../../errors";
import { header } from "../../../forms/utils";
import { preprocessMetadataSchema } from "../../../../../../../schemas/base-metadata.schema";

const projectMetadataSchema = merge(projectGlobalSchema, projectGeneralSchema);

const skipError = (message) => message.includes("requires") && message.includes("name");

export class GuidedNewDatasetPage extends Page {
constructor(...args) {
super(...args);
Expand All @@ -31,18 +32,21 @@ export class GuidedNewDatasetPage extends Page {
onNext: async () => {
const globalState = this.info.globalState.project;

this.dismiss(); // Dismiss all notifications

await this.form.validate().catch((error) => {
if (skipError(error.message)) return;
throw error;
});

// Check validity of project name
const name = this.state.name;
if (!name) {
if (this.#nameNotification) this.dismiss(this.#nameNotification); // Dismiss previous custom notification
this.#nameNotification = this.notify("Please enter a project name.", "error");
this.#nameNotification = this.notify("Please enter a project name.", "error", 3000);
return;
}

this.dismiss(); // Dismiss all notifications

await this.form.validate();

if (!name) return;

// Check if name is already used
Expand Down Expand Up @@ -86,16 +90,23 @@ export class GuidedNewDatasetPage extends Page {
this.form = new JSONSchemaForm({
schema,
results: this.state,
// validateEmptyValues: null,
validateEmptyValues: false,
dialogOptions: {
properties: ["createDirectory"],
},
onOverride: (name) => {
this.notify(`<b>${header(name)}</b> has been overridden with a global value.`, "warning", 3000);
},
validateOnChange,
validateOnChange: async (...args) => {
const results = await validateOnChange.call(this.form, ...args);
if (!results && args[0] === "name") this.#nameNotification && this.dismiss(this.#nameNotification);
return results;
},
onUpdate: () => (this.unsavedUpdates = true),
onThrow,
onThrow: function (message) {
if (skipError(message)) return;
onThrow(message);
},
});

return this.form;
Expand Down
Loading