Skip to content

Commit

Permalink
Merge pull request #2066 from effigies/rf/datasetcontext
Browse files Browse the repository at this point in the history
rf(context): Move file tree, schema, and issues into context.dataset
  • Loading branch information
rwblair authored Aug 7, 2024
2 parents a830d17 + e710edd commit c98cb99
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 148 deletions.
20 changes: 10 additions & 10 deletions bids-validator/src/schema/applyRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Deno.test('evalCheck ensure expression language functions work', () => {
const context = {
x: [1, 2, 3, 4],
y: [1, 1, 1, 1],
issues: new DatasetIssues(),
dataset: { issues: new DatasetIssues() },
}
const rule = [
{
Expand All @@ -117,20 +117,20 @@ Deno.test('evalCheck ensure expression language functions work', () => {
},
]
applyRules(rule, context)
assert(!context.issues.hasIssue({ key: 'CHECK_ERROR' }))
assert(!context.dataset.issues.hasIssue({ key: 'CHECK_ERROR' }))
})
Deno.test(
'evalCheck ensure expression language will fail appropriately',
() => {
const context = { issues: new DatasetIssues() }
const context = { dataset: { issues: new DatasetIssues() } }
const rule = [
{
selectors: ['true'],
checks: ['length(1)'],
},
]
applyRules(rule, context)
assert(context.issues.hasIssue({ key: 'CHECK_ERROR' }))
assert(context.dataset.issues.hasIssue({ key: 'CHECK_ERROR' }))
},
)

Expand All @@ -146,11 +146,11 @@ Deno.test('evalColumns tests', async (t) => {
filename: ['func/sub-01_task-rest_bold.nii.gz'],
acq_time: ['1900-01-01T00:00:78'],
},
issues: new DatasetIssues(),
dataset: { issues: new DatasetIssues() },
}
const rule = schemaDefs.rules.tabular_data.modality_agnostic.Scans
evalColumns(rule, context, schema, 'rules.tabular_data.modality_agnostic.Scans')
assert(context.issues.hasIssue({ key: 'TSV_VALUE_INCORRECT_TYPE_NONREQUIRED' }))
assert(context.dataset.issues.hasIssue({ key: 'TSV_VALUE_INCORRECT_TYPE_NONREQUIRED' }))
})

await t.step('check formatless column', () => {
Expand All @@ -161,11 +161,11 @@ Deno.test('evalColumns tests', async (t) => {
columns: {
onset: ['1', '2', 'not a number'],
},
issues: new DatasetIssues(),
dataset: { issues: new DatasetIssues() },
}
const rule = schemaDefs.rules.tabular_data.made_up.MadeUp
evalColumns(rule, context, schema, 'rules.tabular_data.made_up.MadeUp')
assert(context.issues.hasIssue({ key: 'TSV_VALUE_INCORRECT_TYPE' }))
assert(context.dataset.issues.hasIssue({ key: 'TSV_VALUE_INCORRECT_TYPE' }))
})

await t.step('verify n/a is allowed', () => {
Expand All @@ -177,11 +177,11 @@ Deno.test('evalColumns tests', async (t) => {
onset: ['1', '2', 'n/a'],
strain_rrid: ['RRID:SCR_012345', 'RRID:SCR_012345', 'n/a'],
},
issues: new DatasetIssues(),
dataset: { issues: new DatasetIssues() },
}
const rule = schemaDefs.rules.tabular_data.made_up.MadeUp
evalColumns(rule, context, schema, 'rules.tabular_data.made_up.MadeUp')
assert(context.issues.size === 0)
assert(context.dataset.issues.size === 0)
})
})

Expand Down
30 changes: 15 additions & 15 deletions bids-validator/src/schema/applyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ function evalRuleChecks(
): boolean {
if (rule.checks && !mapEvalCheck(rule.checks, context)) {
if (rule.issue?.code && rule.issue?.message) {
context.issues.add({
context.dataset.issues.add({
key: rule.issue.code,
reason: rule.issue.message,
files: [{ ...context.file, evidence: schemaPath }],
severity: rule.issue.level as Severity,
})
} else {
context.issues.addNonSchemaIssue('CHECK_ERROR', [
context.dataset.issues.addNonSchemaIssue('CHECK_ERROR', [
{ ...context.file, evidence: schemaPath },
])
}
Expand Down Expand Up @@ -241,7 +241,7 @@ export function evalColumns(
let errorObject = columnObject

if (!headers.includes(name) && requirement === 'required') {
context.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
context.dataset.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
{
...context.file,
evidence: `Column with header ${name} listed as required. ${schemaPath}`,
Expand All @@ -267,7 +267,7 @@ export function evalColumns(
typeCheck = (value) => sidecarDefinedTypeCheck(context.sidecar[name], value, schema)
errorObject = context.sidecar[name]
} else {
context.issues.addNonSchemaIssue('TSV_COLUMN_TYPE_REDEFINED', [{
context.dataset.issues.addNonSchemaIssue('TSV_COLUMN_TYPE_REDEFINED', [{
...context.file,
evidence: `'${name}' redefined with sidecar ${inspect(context.sidecar[name])}`,
}])
Expand All @@ -282,7 +282,7 @@ export function evalColumns(
if (
!typeCheck(value)
) {
context.issues.addNonSchemaIssue(error_code, [
context.dataset.issues.addNonSchemaIssue(error_code, [
{
...context.file,
evidence: `'${value}' ${inspect(columnObject)}`,
Expand Down Expand Up @@ -317,13 +317,13 @@ function evalInitialColumns(
if (contextIndex === -1) {
const evidence =
`Column with header ${ruleHeaderName} not found, indexed from 0 it should appear in column ${ruleIndex}. ${schemaPath}`
context.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
context.dataset.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
{ ...context.file, evidence: evidence },
])
} else if (ruleIndex !== contextIndex) {
const evidence =
`Column with header ${ruleHeaderName} found at index ${contextIndex} while rule specifies, indexed from 0, it should be in column ${ruleIndex}. ${schemaPath}`
context.issues.addNonSchemaIssue('TSV_COLUMN_ORDER_INCORRECT', [
context.dataset.issues.addNonSchemaIssue('TSV_COLUMN_ORDER_INCORRECT', [
{ ...context.file, evidence: evidence },
])
}
Expand Down Expand Up @@ -351,7 +351,7 @@ function evalAdditionalColumns(
extraCols = extraCols.filter((header) => !(header in context.sidecar))
}
if (extraCols.length) {
context.issues.addNonSchemaIssue('TSV_ADDITIONAL_COLUMNS_NOT_ALLOWED', [
context.dataset.issues.addNonSchemaIssue('TSV_ADDITIONAL_COLUMNS_NOT_ALLOWED', [
{ ...context.file, evidence: `Disallowed columns found ${extraCols}` },
])
}
Expand Down Expand Up @@ -380,7 +380,7 @@ function evalIndexColumns(
})
const missing = index_columns.filter((col: string) => !headers.includes(col))
if (missing.length) {
context.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
context.dataset.issues.addNonSchemaIssue('TSV_COLUMN_MISSING', [
{
...context.file,
evidence: `Columns cited as index columns not in file: ${missing}. ${schemaPath}`,
Expand All @@ -397,7 +397,7 @@ function evalIndexColumns(
)
})
if (uniqueIndexValues.has(indexValue)) {
context.issues.addNonSchemaIssue('TSV_INDEX_VALUE_NOT_UNIQUE', [
context.dataset.issues.addNonSchemaIssue('TSV_INDEX_VALUE_NOT_UNIQUE', [
{ ...context.file, evidence: `Row: ${i + 2}, Value: ${indexValue}` },
])
} else {
Expand Down Expand Up @@ -431,14 +431,14 @@ function evalJsonCheck(
const keyName: string = metadataDef.name
if (severity && severity !== 'ignore' && !(keyName in json)) {
if (requirement.issue?.code && requirement.issue?.message) {
context.issues.add({
context.dataset.issues.add({
key: requirement.issue.code,
reason: requirement.issue.message,
severity,
files: [{ ...context.file }],
})
} else if (severity === 'error') {
context.issues.addNonSchemaIssue(
context.dataset.issues.addNonSchemaIssue(
sidecarRule ? 'SIDECAR_KEY_REQUIRED' : 'JSON_KEY_REQUIRED',
[
{
Expand All @@ -448,7 +448,7 @@ function evalJsonCheck(
],
)
} else if (severity === 'warning') {
context.issues.addNonSchemaIssue(
context.dataset.issues.addNonSchemaIssue(
sidecarRule ? 'SIDECAR_KEY_RECOMMENDED' : 'JSON_KEY_RECOMMENDED',
[
{
Expand Down Expand Up @@ -485,7 +485,7 @@ function evalJsonCheck(
if (result === false) {
const evidenceBase = `Failed for this file.key: ${originFileKey} Schema path: ${schemaPath}`
if (!validate.errors) {
context.issues.addNonSchemaIssue('JSON_SCHEMA_VALIDATION_ERROR', [
context.dataset.issues.addNonSchemaIssue('JSON_SCHEMA_VALIDATION_ERROR', [
{
...context.file,
evidence: evidenceBase,
Expand All @@ -494,7 +494,7 @@ function evalJsonCheck(
} else {
for (let error of validate.errors) {
const message = 'message' in error ? `message: ${error['message']}` : ''
context.issues.addNonSchemaIssue('JSON_SCHEMA_VALIDATION_ERROR', [
context.dataset.issues.addNonSchemaIssue('JSON_SCHEMA_VALIDATION_ERROR', [
{
...context.file,
evidence: `${evidenceBase} ${message}`,
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/schema/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BIDSContext } from './context.ts'
import { dataFile, rootFileTree } from './fixtures.test.ts'

Deno.test('test context LoadSidecar', async (t) => {
const context = new BIDSContext(rootFileTree, dataFile, new DatasetIssues())
const context = new BIDSContext(dataFile)
await context.loadSidecar()
await t.step('sidecar overwrites correct fields', () => {
const { rootOverwrite, subOverwrite } = context.sidecar
Expand All @@ -24,7 +24,7 @@ Deno.test('test context LoadSidecar', async (t) => {
})

Deno.test('test context loadSubjects', async (t) => {
const context = new BIDSContext(rootFileTree, dataFile, new DatasetIssues())
const context = new BIDSContext(dataFile, undefined, rootFileTree)
await context.loadSubjects()
await t.step('context produces correct subjects object', () => {
assert(context.dataset.subjects, 'subjects object exists')
Expand Down
Loading

0 comments on commit c98cb99

Please sign in to comment.