Skip to content

Commit 37ff18c

Browse files
committed
First pass at patching schema on load with file passed to cli.
1 parent 9792807 commit 37ff18c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/setup/loadSchema.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,31 @@ import { objectPathHandler } from '../utils/objectPathHandler.ts'
33
import { schema as schemaDefault } from '@bids/schema'
44
import { setCustomMetadataFormats } from '../validators/json.ts'
55

6+
function merge(obj1, obj2) {
7+
if (Array.isArray(obj1) && Array.isArray(obj2)) {
8+
return [...obj1, ...obj2]
9+
}
10+
11+
let merged = obj1
12+
if (typeof obj1 !== "object" || typeof obj2 !== "object") {
13+
return merged
14+
}
15+
Object.keys(obj2).map(key => {
16+
if (key in obj1) {
17+
merged[key] = merge(obj1[key], obj2[key])
18+
} else {
19+
merged[key] = obj2[key]
20+
}
21+
})
22+
return merged
23+
}
24+
625
/**
726
* Load the schema from the specification
827
*
928
* version is ignored when the network cannot be accessed
1029
*/
11-
export async function loadSchema(version?: string): Promise<Schema> {
30+
export async function loadSchema(version?: string, patch?: string): Promise<Schema> {
1231
let schemaUrl = version
1332
const bidsSchema = typeof Deno !== 'undefined' ? Deno.env.get('BIDS_SCHEMA') : undefined
1433
if (bidsSchema !== undefined) {
@@ -38,6 +57,13 @@ export async function loadSchema(version?: string): Promise<Schema> {
3857
)
3958
}
4059
}
60+
61+
if (patch) {
62+
let patchText = await Deno.readTextFile(patch);
63+
let patchJson = JSON.parse(patchText)
64+
schema = merge(schema, patchJson)
65+
}
66+
4167
setCustomMetadataFormats(schema)
4268
return schema
4369
}

src/setup/options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type ValidatorOptions = {
3232
blacklistModalities: string[]
3333
prune?: boolean
3434
maxRows?: number
35+
schemaAddon?: string
3536
}
3637

3738
const datasetType = new EnumType<string>(
@@ -98,6 +99,10 @@ export const validateCommand: Command<void, void, any, string[], void> = new Com
9899
'-o, --outfile <file:string>',
99100
'File to write validation results to.',
100101
)
102+
.option(
103+
'--schema-addon <file:string>',
104+
'Json file to be merged with loaded schema.',
105+
)
101106

102107
// Disabling color output is only available in Deno
103108
if (typeof Deno !== 'undefined') {

src/validators/bids.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function validate(
4646
config?: Config,
4747
): Promise<ValidationResult> {
4848
const summary = new Summary()
49-
const schema = await loadSchema(options.schema)
49+
const schema = await loadSchema(options.schema, options?.schemaAddon)
5050
summary.schemaVersion = schema.schema_version
5151

5252
/* There should be a dataset_description in root, this will tell us if we

0 commit comments

Comments
 (0)