From beb0101485562e46eebf57d25d86d37893b6040c Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Fri, 25 Oct 2024 09:00:52 -0500 Subject: [PATCH 1/2] Fix legacy HED tests (which had several pieces of actually invalid data) --- bids-validator/tests/hed.spec.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) 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', }, From a656a9c619e25ef5a2c51aeb0a9e7514d1e17e9c Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Wed, 30 Oct 2024 20:54:27 -0500 Subject: [PATCH 2/2] Fix handling of IssueErrors thrown from hed-validator (second attempt) This does not issue warnings for missing HEDVersion fields for every dataset. --- bids-validator/validators/hed.js | 60 ++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) 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) {