From 37ff18c3a14f224c08abd3e593d6074941c9e803 Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Tue, 16 Sep 2025 15:35:18 -0500 Subject: [PATCH 1/2] First pass at patching schema on load with file passed to cli. --- src/setup/loadSchema.ts | 28 +++++++++++++++++++++++++++- src/setup/options.ts | 5 +++++ src/validators/bids.ts | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/setup/loadSchema.ts b/src/setup/loadSchema.ts index d8b45f56f..1ae8ae0c2 100644 --- a/src/setup/loadSchema.ts +++ b/src/setup/loadSchema.ts @@ -3,12 +3,31 @@ import { objectPathHandler } from '../utils/objectPathHandler.ts' import { schema as schemaDefault } from '@bids/schema' import { setCustomMetadataFormats } from '../validators/json.ts' +function merge(obj1, obj2) { + if (Array.isArray(obj1) && Array.isArray(obj2)) { + return [...obj1, ...obj2] + } + + let merged = obj1 + if (typeof obj1 !== "object" || typeof obj2 !== "object") { + return merged + } + Object.keys(obj2).map(key => { + if (key in obj1) { + merged[key] = merge(obj1[key], obj2[key]) + } else { + merged[key] = obj2[key] + } + }) + return merged +} + /** * Load the schema from the specification * * version is ignored when the network cannot be accessed */ -export async function loadSchema(version?: string): Promise { +export async function loadSchema(version?: string, patch?: string): Promise { let schemaUrl = version const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined if (bidsSchema !== undefined) { @@ -38,6 +57,13 @@ export async function loadSchema(version?: string): Promise { ) } } + + if (patch) { + let patchText = await Deno.readTextFile(patch); + let patchJson = JSON.parse(patchText) + schema = merge(schema, patchJson) + } + setCustomMetadataFormats(schema) return schema } diff --git a/src/setup/options.ts b/src/setup/options.ts index 0c58b1291..3d32540c3 100644 --- a/src/setup/options.ts +++ b/src/setup/options.ts @@ -32,6 +32,7 @@ export type ValidatorOptions = { blacklistModalities: string[] prune?: boolean maxRows?: number + schemaAddon?: string } const datasetType = new EnumType( @@ -98,6 +99,10 @@ export const validateCommand: Command = new Com '-o, --outfile ', 'File to write validation results to.', ) + .option( + '--schema-addon ', + 'Json file to be merged with loaded schema.', + ) // Disabling color output is only available in Deno if (typeof Deno !== 'undefined') { diff --git a/src/validators/bids.ts b/src/validators/bids.ts index 32e57efb3..cdc897958 100644 --- a/src/validators/bids.ts +++ b/src/validators/bids.ts @@ -46,7 +46,7 @@ export async function validate( config?: Config, ): Promise { const summary = new Summary() - const schema = await loadSchema(options.schema) + const schema = await loadSchema(options.schema, options?.schemaAddon) summary.schemaVersion = schema.schema_version /* There should be a dataset_description in root, this will tell us if we From bc745d7692591e70eb085135e0476482cb5f6bbe Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Tue, 16 Sep 2025 15:42:11 -0500 Subject: [PATCH 2/2] add print flag. Need to figure out how to make datasetPath optional when its used... --- src/setup/loadSchema.ts | 6 +++++- src/setup/options.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/setup/loadSchema.ts b/src/setup/loadSchema.ts index 1ae8ae0c2..b24072f67 100644 --- a/src/setup/loadSchema.ts +++ b/src/setup/loadSchema.ts @@ -27,7 +27,7 @@ function merge(obj1, obj2) { * * version is ignored when the network cannot be accessed */ -export async function loadSchema(version?: string, patch?: string): Promise { +export async function loadSchema(version?: string, patch?: string, print?: boolean): Promise { let schemaUrl = version const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined if (bidsSchema !== undefined) { @@ -64,6 +64,10 @@ export async function loadSchema(version?: string, patch?: string): Promise( @@ -103,6 +104,10 @@ export const validateCommand: Command = new Com '--schema-addon ', 'Json file to be merged with loaded schema.', ) + .option( + '--printSchema', + 'Print schema that was loaded and exit.', + ) // Disabling color output is only available in Deno if (typeof Deno !== 'undefined') {