Skip to content

Commit

Permalink
Load version.schema for v0.5 schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Jul 4, 2024
1 parent fdd8a55 commit e150aa9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/JsonValidator/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
43 changes: 31 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -217,24 +225,35 @@ 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) {
console.log("No version found, using: " + CURRENT_VERSION);
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<schemaUrls.length; s++) {
let schema = await getSchema(schemaUrls[s]);
let errs = validateData(schema, jsonData);
let errs = validateData(schema, jsonData, refSchemas);
errors = errors.concat(errs);
}
return errors;
Expand Down

0 comments on commit e150aa9

Please sign in to comment.