Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/setup/loadSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema> {
export async function loadSchema(version?: string, patch?: string, print?: boolean): Promise<Schema> {
let schemaUrl = version
const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined
if (bidsSchema !== undefined) {
Expand Down Expand Up @@ -38,6 +57,17 @@ export async function loadSchema(version?: string): Promise<Schema> {
)
}
}

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
}
10 changes: 10 additions & 0 deletions src/setup/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type ValidatorOptions = {
blacklistModalities: string[]
prune?: boolean
maxRows?: number
schemaAddon?: string
printSchema?: boolean
}

const datasetType = new EnumType<string>(
Expand Down Expand Up @@ -98,6 +100,14 @@ export const validateCommand: Command<void, void, any, string[], void> = new Com
'-o, --outfile <file:string>',
'File to write validation results to.',
)
.option(
'--schema-addon <file:string>',
'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') {
Expand Down
2 changes: 1 addition & 1 deletion src/validators/bids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function validate(
config?: Config,
): Promise<ValidationResult> {
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
Expand Down
Loading