Skip to content

Commit 30544f1

Browse files
committed
feat: Implement warning for sidecar overrides
1 parent ba743df commit 30544f1

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/issues/list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ export const bidsIssues: IssueDefinitionRecord = {
166166
severity: 'error',
167167
reason: 'A json sidecar file was found without a corresponding data file',
168168
},
169+
SIDECAR_FIELD_OVERRIDE: {
170+
severity: 'warning',
171+
reason: 'Sidecar files should not override values assigned at a higher level.',
172+
},
169173
BLACKLISTED_MODALITY: {
170174
severity: 'error',
171175
reason: 'The modality in this file is blacklisted through validator configuration.',

src/schema/context.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, assertObjectMatch } from '@std/assert'
1+
import { assert, assertEquals, assertObjectMatch } from '@std/assert'
22
import type { DatasetIssues } from '../issues/datasetIssues.ts'
33
import { BIDSContext } from './context.ts'
44
import { dataFile, rootFileTree } from './fixtures.test.ts'
@@ -21,6 +21,12 @@ Deno.test('test context LoadSidecar', async (t) => {
2121
anatValue: 'anat',
2222
})
2323
})
24+
await t.step('Warnings are emitted for overriding sidecar fields', () => {
25+
assertEquals(
26+
context.dataset.issues.get({ code: 'SIDECAR_FIELD_OVERRIDE' }).length,
27+
2,
28+
)
29+
})
2430
})
2531

2632
Deno.test('test context loadSubjects', async (t) => {

src/schema/context.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ export class BIDSContext implements Context {
216216
throw error
217217
}
218218
})
219+
const overrides = Object.keys(this.sidecar).filter((x) => Object.hasOwn(json, x))
220+
for (const key of overrides) {
221+
// @ts-ignore
222+
if (json[key] !== this.sidecar[key]) {
223+
const overrideLocation = this.sidecarKeyOrigin[key]
224+
this.dataset.issues.add({
225+
code: 'SIDECAR_FIELD_OVERRIDE',
226+
subCode: key,
227+
location: overrideLocation,
228+
// @ts-ignore
229+
issueMessage: `Sidecar key defined in ${file.path} overrides previous value (${json[key]}) from ${overrideLocation}`,
230+
})
231+
}
232+
}
219233
this.sidecar = { ...json, ...this.sidecar }
220234
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= file.path)
221235
}

0 commit comments

Comments
 (0)