Skip to content

Commit 4f101aa

Browse files
authored
Merge pull request #214 from effigies/enh/summarization
feat: Implement warning for sidecar overrides
2 parents ba743df + 5392b96 commit 4f101aa

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,26 @@ export class BIDSContext implements Context {
208208
}
209209
}
210210
for (const file of sidecars) {
211-
const json = await loadJSON(file).catch((error) => {
211+
const json = await loadJSON(file).catch((error): Record<string, unknown> => {
212212
if (error.key) {
213213
this.dataset.issues.add({ code: error.key, location: file.path })
214214
return {}
215215
} else {
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+
if (json[key] !== this.sidecar[key]) {
222+
const overrideLocation = this.sidecarKeyOrigin[key]
223+
this.dataset.issues.add({
224+
code: 'SIDECAR_FIELD_OVERRIDE',
225+
subCode: key,
226+
location: overrideLocation,
227+
issueMessage: `Sidecar key defined in ${file.path} overrides previous value (${json[key]}) from ${overrideLocation}`,
228+
})
229+
}
230+
}
219231
this.sidecar = { ...json, ...this.sidecar }
220232
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= file.path)
221233
}

0 commit comments

Comments
 (0)