diff --git a/bids-validator/tests/hed.spec.js b/bids-validator/tests/hed.spec.js index 11ea1c5a5..cf5fe8f13 100644 --- a/bids-validator/tests/hed.spec.js +++ b/bids-validator/tests/hed.spec.js @@ -27,7 +27,7 @@ describe('HED', function () { ] const jsonDictionary = { '/sub01/sub01_task-test_events.json': { - myCodes: { + test: { HED: { one: 'Duration/5 s', }, @@ -55,12 +55,8 @@ describe('HED', function () { ] const jsonDictionary = { '/sub01/sub01_task-test_events.json': { - myCodes: { - test: { - HED: { - one: 'Label/#', - }, - }, + test: { + HED: 'Label/#', }, }, '/dataset_description.json': { HEDVersion: '8.0.0' }, @@ -86,11 +82,9 @@ describe('HED', function () { const jsonDictionary = { '/sub01/sub01_task-test_events.json': { - myCodes: { - test: { - HED: { - one: 'ts:Sensory-presentation, Label/#', - }, + test: { + HED: { + one: 'ts:Sensory-presentation, Train', }, }, }, @@ -148,11 +142,9 @@ describe('HED', function () { const jsonDictionary = { '/sub01/sub01_task-test_events.json': { - myCodes: { - test: { - HED: { - one: 'ts:Sensory-presentation, Label/#, sc:Sleep-deprivation', - }, + test: { + HED: { + one: 'ts:Sensory-presentation, Walk, sc:Sleep-deprivation', }, }, }, @@ -260,7 +252,7 @@ describe('HED', function () { ] const jsonDictionary = { '/sub01/sub01_task-test_events.json': { - myCodes: { + test: { HED: { one: 'Duration/5 s', }, diff --git a/bids-validator/validators/hed.js b/bids-validator/validators/hed.js index 4dde02b48..59ea86c55 100644 --- a/bids-validator/validators/hed.js +++ b/bids-validator/validators/hed.js @@ -29,32 +29,24 @@ async function checkHedStrings(tsvs, jsonContents, jsonFiles) { ) } - const issues = [] - for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) { - try { - const sidecarFile = buildSidecar(sidecarName, sidecarContents, jsonFiles) - issues.push(...validateFile(sidecarFile, hedSchemas)) - } catch (e) { - issues.push(new Issue({ code: 109 })) - return issues - } - } + const sidecarIssues = validateFiles( + buildSidecars(jsonContents, jsonFiles), + hedSchemas, + ) - if (issues.some((issue) => issue.isError())) { - return issues + if (sidecarIssues.some((issue) => issue.isError() || issue.code === 109)) { + return sidecarIssues } - for (const tsv of tsvs) { - try { - const tsvFile = buildTsv(tsv, jsonContents) - issues.push(...validateFile(tsvFile, hedSchemas)) - } catch (e) { - issues.push(new Issue({ code: 109 })) - return issues - } - } + const tsvIssues = validateFiles(buildTsvs(tsvs, jsonContents), hedSchemas) - return issues + return [...sidecarIssues, ...tsvIssues] +} + +function* buildSidecars(jsonContents, jsonFiles) { + for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) { + yield buildSidecar(sidecarName, sidecarContents, jsonFiles) + } } function buildSidecar(sidecarName, sidecarContents, jsonFiles) { @@ -63,6 +55,12 @@ function buildSidecar(sidecarName, sidecarContents, jsonFiles) { return new hedValidator.bids.BidsSidecar(sidecarName, sidecarContents, file) } +function* buildTsvs(tsvs, jsonContents) { + for (const tsv of tsvs) { + yield buildTsv(tsv, jsonContents) + } +} + function buildTsv(tsv, jsonContents) { const potentialSidecars = utils.files.potentialLocations( tsv.file.relativePath.replace('.tsv', '.json'), @@ -81,12 +79,20 @@ function buildTsv(tsv, jsonContents) { ) } -function validateFile(file, hedSchemas) { - const issues = file.validate(hedSchemas) - if (issues === null) { - throw new Error() +function validateFiles(fileGenerator, hedSchemas) { + const issues = [] + for (const file of fileGenerator) { + try { + const fileIssues = file.validate(hedSchemas) + if (fileIssues === null) { + return [new hedValidator.bids.BidsIssue(109)] + } + issues.push(fileIssues) + } catch (issueError) { + return hedValidator.bids.BidsHedIssue.fromHedIssues(issueError, file.file) + } } - return issues + return issues.flat() } function getSidecarFileObject(sidecarName, jsonFiles) {