Skip to content

Commit

Permalink
feat(eval): Distinguish JSON rules from sidecar rules
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 30, 2024
1 parent ccfd5db commit 1e72b1a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
12 changes: 10 additions & 2 deletions bids-validator/src/issues/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ export const filenameIssues: IssueDefinitionRecord = {
},
JSON_KEY_REQUIRED: {
severity: 'error',
reason: "A data file's JSON sidecar is missing a key listed as required.",
reason: 'A JSON flle is missing a key listed as required.',
},
JSON_KEY_RECOMMENDED: {
severity: 'warning',
reason: 'A data files JSON sidecar is missing a key listed as recommended.',
reason: 'A JSON file is missing a key listed as recommended.',
},
SIDECAR_KEY_REQUIRED: {
severity: 'error',
reason: "A data file's JSON sidecar is missing a key listed as required.",
},
SIDECAR_KEY_RECOMMENDED: {
severity: 'warning',
reason: "A data file's JSON sidecar is missing a key listed as recommended.",
},
JSON_SCHEMA_VALIDATION_ERROR: {
severity: 'error',
Expand Down
38 changes: 25 additions & 13 deletions bids-validator/src/schema/applyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,18 @@ function evalJsonCheck(
schema: GenericSchema,
schemaPath: string,
): void {
const sidecarRule = schemaPath.match(/rules\.sidecar/)
// Sidecar rules apply specifically to data files, as JSON files cannot have sidecars
// Count on other JSON rules to use selectors to match the correct files
if (context.extension === '.json' && sidecarRule) return

const json = sidecarRule ? context.sidecar : context.json
for (const [key, requirement] of Object.entries(rule.fields)) {
const severity = getFieldSeverity(requirement, context)
// @ts-expect-error
const metadataDef = schema.objects.metadata[key]
const keyName: string = metadataDef.name
if (severity && severity !== 'ignore' && !(keyName in context.sidecar)) {
if (severity && severity !== 'ignore' && !(keyName in json)) {
if (requirement.issue?.code && requirement.issue?.message) {
context.issues.add({
key: requirement.issue.code,
Expand All @@ -427,19 +433,25 @@ function evalJsonCheck(
files: [{ ...context.file }],
})
} else if (severity === 'error') {
context.issues.addNonSchemaIssue('JSON_KEY_REQUIRED', [
{
...context.file,
evidence: `missing ${keyName} as per ${schemaPath}`,
},
])
context.issues.addNonSchemaIssue(

Check warning on line 436 in bids-validator/src/schema/applyRules.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/schema/applyRules.ts#L436

Added line #L436 was not covered by tests
sidecarRule ? 'SIDECAR_KEY_REQUIRED' : 'JSON_KEY_REQUIRED',
[

Check warning on line 438 in bids-validator/src/schema/applyRules.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/schema/applyRules.ts#L438

Added line #L438 was not covered by tests
{
...context.file,
evidence: `missing ${keyName} as per ${schemaPath}`,
},
],
)
} else if (severity === 'warning') {
context.issues.addNonSchemaIssue('JSON_KEY_RECOMMENDED', [
{
...context.file,
evidence: `missing ${keyName} as per ${schemaPath}`,
},
])
context.issues.addNonSchemaIssue(

Check warning on line 446 in bids-validator/src/schema/applyRules.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/schema/applyRules.ts#L446

Added line #L446 was not covered by tests
sidecarRule ? 'SIDECAR_KEY_RECOMMENDED' : 'JSON_KEY_RECOMMENDED',
[

Check warning on line 448 in bids-validator/src/schema/applyRules.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/schema/applyRules.ts#L448

Added line #L448 was not covered by tests
{
...context.file,
evidence: `missing ${keyName} as per ${schemaPath}`,
},
],
)
}
}

Expand Down
3 changes: 3 additions & 0 deletions bids-validator/src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ export class BIDSContext implements Context {
* json sidecars found.
*/
async loadSidecar(fileTree?: FileTree) {
if (this.extension === '.json') {
return
}
if (!fileTree) {
fileTree = this.fileTree
}
Expand Down

0 comments on commit 1e72b1a

Please sign in to comment.