Skip to content

Commit

Permalink
Fix handling of IssueErrors thrown from hed-validator (second attempt)
Browse files Browse the repository at this point in the history
This does not issue warnings for missing HEDVersion fields for every dataset.
  • Loading branch information
happy5214 committed Oct 31, 2024
1 parent beb0101 commit a656a9c
Showing 1 changed file with 33 additions and 27 deletions.
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)

Check warning on line 92 in bids-validator/validators/hed.js

View check run for this annotation

Codecov / codecov/patch

bids-validator/validators/hed.js#L92

Added line #L92 was not covered by tests
}
}
return issues
return issues.flat()
}

function getSidecarFileObject(sidecarName, jsonFiles) {
Expand Down

0 comments on commit a656a9c

Please sign in to comment.