Skip to content

Commit

Permalink
Merge branch 'ephys-metadata' of https://github.com/NeurodataWithoutB…
Browse files Browse the repository at this point in the history
…orders/nwb-guide into ephys-metadata
  • Loading branch information
garrettmflynn committed Dec 28, 2023
2 parents e7df0ab + ce31bc4 commit 4723b05
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
84 changes: 39 additions & 45 deletions src/renderer/src/stories/JSONSchemaForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,46 +388,44 @@ export class JSONSchemaForm extends LitElement {

validateSchema = async (resolved, schema, name) => {
return await validator
.validate(resolved, schema)
.errors.map((e) => {
.validate(resolved, schema)
.errors.map((e) => {
const propName = e.path.slice(-1)[0] ?? name ?? e.property;
const rowName = e.path.slice(-2)[0];

const propName = e.path.slice(-1)[0] ?? name ?? e.property;
const rowName = e.path.slice(-2)[0];
const isRow = typeof rowName === "number";

const isRow = typeof rowName === "number";
const resolvedValue = e.path.reduce((acc, token) => acc[token], resolved);

const resolvedValue = e.path.reduce((acc, token) => acc[token], resolved);
// ------------ Exclude Certain Errors ------------
// Non-Strict Rule
if (schema.strict === false && e.message.includes("is not one of enum values")) return;

// ------------ Exclude Certain Errors ------------
// Non-Strict Rule
if (schema.strict === false && e.message.includes("is not one of enum values")) return;

// Allow referring to floats as null (i.e. JSON NaN representation)
if (e.message === "is not of a type(s) number") {
if (resolvedValue === null) return;
}
// Allow referring to floats as null (i.e. JSON NaN representation)
if (e.message === "is not of a type(s) number") {
if (resolvedValue === null) return;
}

return {
type: "error",
message: `${
typeof propName === "string"
? `${header(propName)}${isRow ? ` on Row ${rowName}` : ""}`
: `Row ${propName}`
} ${e.message}.`,
};
})
.filter((v) => !!v)
}
return {
type: "error",
message: `${
typeof propName === "string"
? `${header(propName)}${isRow ? ` on Row ${rowName}` : ""}`
: `Row ${propName}`
} ${e.message}.`,
};
})
.filter((v) => !!v);
};

validate = async (resolved = this.resolved) => {

// Validate against the entire JSON Schema
const copy = structuredClone(resolved);
delete copy.__disabled
delete copy.__disabled;

const result = await this.validateSchema(copy, this.schema);

const resolvedErrors = this.#resolveErrors(result, this.base, resolved)
const resolvedErrors = this.#resolveErrors(result, this.base, resolved);

// Check if any required inputs are missing
const requiredButNotSpecified = await this.#validateRequirements(resolved); // get missing required paths
Expand All @@ -443,10 +441,10 @@ export class JSONSchemaForm extends LitElement {
}

const allErrors = Array.from(flaggedInputs)
.map((el) => {
return Array.from(el.nextElementSibling.children).map((li) => li.message);
}).flat()

.map((el) => {
return Array.from(el.nextElementSibling.children).map((li) => li.message);
})
.flat();

const nMissingRequired = allErrors.reduce((acc, curr) => {
return (acc += curr.includes(this.#isARequiredPropertyString) ? 1 : 0);
Expand Down Expand Up @@ -689,8 +687,8 @@ export class JSONSchemaForm extends LitElement {
let invalid = [];

for (let name in requirements) {
let isRequired = this.#isRequired(name, requirements)
let isRequired = this.#isRequired(name, requirements);

if (this.#accordions[name]?.disabled) continue; // Skip disabled accordions

// // NOTE: Uncomment to block checking requirements inside optional properties
Expand Down Expand Up @@ -824,7 +822,7 @@ export class JSONSchemaForm extends LitElement {
#isARequiredPropertyString = `is a required property`;

#resolveErrors = (errors, externalPath, parent) => {
return errors
return errors
.map((e) => {
// Custom Error Transformations
if (this.transformErrors) {
Expand All @@ -836,7 +834,7 @@ export class JSONSchemaForm extends LitElement {
return e;
})
.filter((v) => !!v);
}
};

// Assume this is going to return as a Promise—even if the change function isn't returning one
triggerValidation = async (
Expand Down Expand Up @@ -866,10 +864,7 @@ export class JSONSchemaForm extends LitElement {
const skipValidation = !this.validateEmptyValues && value === undefined;
const validateArgs = input.pattern || skipValidation ? [] : [value, schema];

const jsonSchemaErrors =
validateArgs.length === 2
? await this.validateSchema(...validateArgs, name)
: [];
const jsonSchemaErrors = validateArgs.length === 2 ? await this.validateSchema(...validateArgs, name) : [];

const valid = skipValidation ? true : await this.validateOnChange(name, parent, pathToValidate, value);

Expand Down Expand Up @@ -957,7 +952,7 @@ export class JSONSchemaForm extends LitElement {

if (!isValid && errors.length === 0) errors.push({ type: "error", message: "Invalid value detected" });

const resolvedErrors = this.#resolveErrors(errors, externalPath, parent)
const resolvedErrors = this.#resolveErrors(errors, externalPath, parent);

// Track errors and warnings
const updatedWarnings = warnings.map((info) => (onWarning ? onWarning(info) : info)).filter((v) => !!v);
Expand Down Expand Up @@ -1162,11 +1157,10 @@ export class JSONSchemaForm extends LitElement {
const nestedResults = __disabled[name] ?? results[name] ?? this.results[name]; // One or the other will exist—depending on global or local disabling

if (renderableInside.length) {

const allIgnore = this.ignore["*"] ?? {};
const ignore = this.ignore[name] ?? {}
if (ignore["*"]) ignore["*"] = { ...allIgnore, ...ignore["*"] }
else ignore["*"] = allIgnore
const ignore = this.ignore[name] ?? {};
if (ignore["*"]) ignore["*"] = { ...allIgnore, ...ignore["*"] };
else ignore["*"] = allIgnore;

const ogContext = this;
const nested = (this.#nestedForms[name] = new JSONSchemaForm({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class GuidedMetadataPage extends ManagedPage {
transformErrors: (e, externalPath, parent) => {
// JSON Schema Exceptions
if (e.message.includes('does not conform to the "date-time" format.')) return false;
if (e.message.includes('not allowed to have the additional property \"Ecephys\".')) return false; // NOTE: Remove after including Ecephys metadata
if (e.message.includes('not allowed to have the additional property "Ecephys".')) return false; // NOTE: Remove after including Ecephys metadata
},

conditionalRequirements: [
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/src/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ export async function validateOnChange(name, parent, path, value) {
});

const res = resolveAll(results, (arr) => {

arr = arr.map((v, i) => {
const func = functions[i];
if (typeof func === "function") return v;
else return v === null ? undefined : v;
})
});

const flat = arr.flat();
if (flat.find((res) => res?.message)) {
return flat
Expand Down

0 comments on commit 4723b05

Please sign in to comment.