diff --git a/src/setup/loadSchema.ts b/src/setup/loadSchema.ts index d8b45f56..b24072f6 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, print?: boolean): Promise { let schemaUrl = version const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined if (bidsSchema !== undefined) { @@ -38,6 +57,17 @@ export async function loadSchema(version?: string): Promise { ) } } + + if (patch) { + let patchText = await Deno.readTextFile(patch); + let patchJson = JSON.parse(patchText) + schema = merge(schema, patchJson) + } + + if (print) { + console.log(JSON.stringify(schema)) + Deno.exit(0) + } setCustomMetadataFormats(schema) return schema } diff --git a/src/setup/options.ts b/src/setup/options.ts index 0c58b129..3a0c62d2 100644 --- a/src/setup/options.ts +++ b/src/setup/options.ts @@ -32,6 +32,8 @@ export type ValidatorOptions = { blacklistModalities: string[] prune?: boolean maxRows?: number + schemaAddon?: string + printSchema?: boolean } const datasetType = new EnumType( @@ -98,6 +100,14 @@ 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.', + ) + .option( + '--printSchema', + 'Print schema that was loaded and exit.', + ) // 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 32e57efb..cdc89795 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