From e150aa91e7ba8bca3105dcc3bc3abbc88e69aac1 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 4 Jul 2024 10:23:19 +0100 Subject: [PATCH] Load version.schema for v0.5 schemas --- src/JsonValidator/index.svelte | 4 ++-- src/utils.js | 43 ++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/JsonValidator/index.svelte b/src/JsonValidator/index.svelte index 2ec315a..fd74388 100644 --- a/src/JsonValidator/index.svelte +++ b/src/JsonValidator/index.svelte @@ -21,14 +21,14 @@ export let rootAttrs; // v0.5+ unwrap the attrs under "ome" - const omeAttrs = rootAttrs.ome || rootAttrs; + const omeAttrs = rootAttrs?.attributes?.ome || rootAttrs; const msVersion = getVersion(rootAttrs); const dtype = getDataType(omeAttrs); const schemaUrls = getSchemaUrlsForJson(omeAttrs); console.log("index.svelte schemaUrls", schemaUrls) - const promise = validate(omeAttrs); + const promise = validate(rootAttrs); const dirs = source.split("/").filter(Boolean); const zarrName = dirs[dirs.length - 1]; diff --git a/src/utils.js b/src/utils.js index c683ccd..026e692 100644 --- a/src/utils.js +++ b/src/utils.js @@ -73,8 +73,7 @@ export function getZarrArrayAttrsFileName(ngffVersion) { } export async function getZarrGroupAttrs(zarr_dir) { - let rawJson = await getZarrJson(zarr_dir, ".zattrs"); - let groupAttrs = rawJson.attributes || rawJson; + let groupAttrs = await getZarrJson(zarr_dir, ".zattrs"); return groupAttrs; } @@ -146,6 +145,9 @@ export function getVersion(ngffData) { if (ngffData.ome?.version) { return ngffData.ome.version; } + if (ngffData.version) { + return ngffData.version; + } let version = ngffData.multiscales ? ngffData.multiscales[0].version : ngffData.plate @@ -194,18 +196,24 @@ export function getSchemaNames(ngffData) { export function getSchemaUrlsForJson(rootAttrs) { console.log('getSchemaUrlsForJson rootAttrs', rootAttrs) - const msVersion = getVersion(rootAttrs); + // v0.5+ unwrap the attrs under "attributes.ome" + let omeAttrs = rootAttrs?.attributes?.ome || rootAttrs; + + const msVersion = getVersion(omeAttrs); const version = msVersion || CURRENT_VERSION; // for v0.5 onwards, rootAttrs is nested under attributes.ome... - if (rootAttrs.ome) { - rootAttrs = rootAttrs.ome; + if (omeAttrs.ome) { + console.trace("WARNING - FIXME!") + omeAttrs = omeAttrs.ome; } - const schemaNames = getSchemaNames(rootAttrs); + const schemaNames = getSchemaNames(omeAttrs); return schemaNames.map(name => getSchemaUrl(name, version)); } -export function validateData(schema, jsonData) { - const validate = ajv.compile(schema); +export function validateData(schema, jsonData, extraSchemas) { + // call ajv.addSchema(schema) for each schema + let withSchema = extraSchemas.reduce((prev, curr) => prev.addSchema(curr), ajv); + const validate = withSchema.compile(schema); const valid = validate(jsonData); let errors = []; if (!valid) { @@ -217,13 +225,15 @@ export function validateData(schema, jsonData) { export async function validate(jsonData) { // get version, lookup schema, do validation... - let version = getVersion(jsonData); - console.log("VERSION", version); + // v0.5+ unwrap the attrs under "attributes.ome" + let omeAttrs = jsonData?.attributes?.ome || jsonData; + let version = getVersion(omeAttrs); + console.log("validate VERSION", version, jsonData); const schemaUrls = getSchemaUrlsForJson(jsonData); if (schemaUrls.length == 0) { - return ["Unrecognised JSON data"]; + return ["No schemas found. Unrecognised JSON data"]; } if (!version) { @@ -231,10 +241,19 @@ export async function validate(jsonData) { version = CURRENT_VERSION; } + let refSchemas = []; + // TODO: need to know whether to load other schemas... + // For now, we can use version check... + if (version === "0.5") { + // const ctSchema = await getSchema(version, "coordinate_transformation"); + // const csSchema = await getSchema(version, "coordinate_systems"); + const versionSchema = await getSchema(getSchemaUrl("version", version)); + refSchemas = [versionSchema]; + } let errors = []; for (let s=0; s