Skip to content

Commit 9d0d585

Browse files
authored
Merge pull request #2174 from happy5214/fix-hed-validator-error-reporting
Fix HED error reporting and tests
2 parents 0b90e13 + a656a9c commit 9d0d585

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

bids-validator/tests/hed.spec.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('HED', function () {
2727
]
2828
const jsonDictionary = {
2929
'/sub01/sub01_task-test_events.json': {
30-
myCodes: {
30+
test: {
3131
HED: {
3232
one: 'Duration/5 s',
3333
},
@@ -55,12 +55,8 @@ describe('HED', function () {
5555
]
5656
const jsonDictionary = {
5757
'/sub01/sub01_task-test_events.json': {
58-
myCodes: {
59-
test: {
60-
HED: {
61-
one: 'Label/#',
62-
},
63-
},
58+
test: {
59+
HED: 'Label/#',
6460
},
6561
},
6662
'/dataset_description.json': { HEDVersion: '8.0.0' },
@@ -86,11 +82,9 @@ describe('HED', function () {
8682

8783
const jsonDictionary = {
8884
'/sub01/sub01_task-test_events.json': {
89-
myCodes: {
90-
test: {
91-
HED: {
92-
one: 'ts:Sensory-presentation, Label/#',
93-
},
85+
test: {
86+
HED: {
87+
one: 'ts:Sensory-presentation, Train',
9488
},
9589
},
9690
},
@@ -148,11 +142,9 @@ describe('HED', function () {
148142

149143
const jsonDictionary = {
150144
'/sub01/sub01_task-test_events.json': {
151-
myCodes: {
152-
test: {
153-
HED: {
154-
one: 'ts:Sensory-presentation, Label/#, sc:Sleep-deprivation',
155-
},
145+
test: {
146+
HED: {
147+
one: 'ts:Sensory-presentation, Walk, sc:Sleep-deprivation',
156148
},
157149
},
158150
},
@@ -260,7 +252,7 @@ describe('HED', function () {
260252
]
261253
const jsonDictionary = {
262254
'/sub01/sub01_task-test_events.json': {
263-
myCodes: {
255+
test: {
264256
HED: {
265257
one: 'Duration/5 s',
266258
},

bids-validator/validators/hed.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,24 @@ async function checkHedStrings(tsvs, jsonContents, jsonFiles) {
2929
)
3030
}
3131

32-
const issues = []
33-
for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) {
34-
try {
35-
const sidecarFile = buildSidecar(sidecarName, sidecarContents, jsonFiles)
36-
issues.push(...validateFile(sidecarFile, hedSchemas))
37-
} catch (e) {
38-
issues.push(new Issue({ code: 109 }))
39-
return issues
40-
}
41-
}
32+
const sidecarIssues = validateFiles(
33+
buildSidecars(jsonContents, jsonFiles),
34+
hedSchemas,
35+
)
4236

43-
if (issues.some((issue) => issue.isError())) {
44-
return issues
37+
if (sidecarIssues.some((issue) => issue.isError() || issue.code === 109)) {
38+
return sidecarIssues
4539
}
4640

47-
for (const tsv of tsvs) {
48-
try {
49-
const tsvFile = buildTsv(tsv, jsonContents)
50-
issues.push(...validateFile(tsvFile, hedSchemas))
51-
} catch (e) {
52-
issues.push(new Issue({ code: 109 }))
53-
return issues
54-
}
55-
}
41+
const tsvIssues = validateFiles(buildTsvs(tsvs, jsonContents), hedSchemas)
5642

57-
return issues
43+
return [...sidecarIssues, ...tsvIssues]
44+
}
45+
46+
function* buildSidecars(jsonContents, jsonFiles) {
47+
for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) {
48+
yield buildSidecar(sidecarName, sidecarContents, jsonFiles)
49+
}
5850
}
5951

6052
function buildSidecar(sidecarName, sidecarContents, jsonFiles) {
@@ -63,6 +55,12 @@ function buildSidecar(sidecarName, sidecarContents, jsonFiles) {
6355
return new hedValidator.bids.BidsSidecar(sidecarName, sidecarContents, file)
6456
}
6557

58+
function* buildTsvs(tsvs, jsonContents) {
59+
for (const tsv of tsvs) {
60+
yield buildTsv(tsv, jsonContents)
61+
}
62+
}
63+
6664
function buildTsv(tsv, jsonContents) {
6765
const potentialSidecars = utils.files.potentialLocations(
6866
tsv.file.relativePath.replace('.tsv', '.json'),
@@ -81,12 +79,20 @@ function buildTsv(tsv, jsonContents) {
8179
)
8280
}
8381

84-
function validateFile(file, hedSchemas) {
85-
const issues = file.validate(hedSchemas)
86-
if (issues === null) {
87-
throw new Error()
82+
function validateFiles(fileGenerator, hedSchemas) {
83+
const issues = []
84+
for (const file of fileGenerator) {
85+
try {
86+
const fileIssues = file.validate(hedSchemas)
87+
if (fileIssues === null) {
88+
return [new hedValidator.bids.BidsIssue(109)]
89+
}
90+
issues.push(fileIssues)
91+
} catch (issueError) {
92+
return hedValidator.bids.BidsHedIssue.fromHedIssues(issueError, file.file)
93+
}
8894
}
89-
return issues
95+
return issues.flat()
9096
}
9197

9298
function getSidecarFileObject(sidecarName, jsonFiles) {

0 commit comments

Comments
 (0)