Skip to content

Commit

Permalink
Merge pull request #2174 from happy5214/fix-hed-validator-error-repor…
Browse files Browse the repository at this point in the history
…ting

Fix HED error reporting and tests
  • Loading branch information
effigies authored Oct 31, 2024
2 parents 0b90e13 + a656a9c commit 9d0d585
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
28 changes: 10 additions & 18 deletions bids-validator/tests/hed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('HED', function () {
]
const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'Duration/5 s',
},
Expand Down Expand Up @@ -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' },
Expand All @@ -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',
},
},
},
Expand Down Expand Up @@ -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',
},
},
},
Expand Down Expand Up @@ -260,7 +252,7 @@ describe('HED', function () {
]
const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'Duration/5 s',
},
Expand Down
60 changes: 33 additions & 27 deletions bids-validator/validators/hed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'),
Expand All @@ -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) {
Expand Down

0 comments on commit 9d0d585

Please sign in to comment.