Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tsv): Check explicit formats for column contents when defined #2025

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion bids-validator/src/schema/applyRules.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-nocheck
import { assert, assertEquals, assertObjectMatch } from '../deps/asserts.ts'
import { loadSchema } from '../setup/loadSchema.ts'
import { applyRules, evalCheck } from './applyRules.ts'
import { applyRules, evalCheck, evalColumns } from './applyRules.ts'
import { DatasetIssues } from '../issues/datasetIssues.ts'

const ruleContextData = [
Expand Down Expand Up @@ -45,6 +45,31 @@ const schemaDefs = {
},
},
},
tabular_data: {
modality_agnostic: {
Scans: {
selectors: ['suffix == "scans"', 'extension == ".tsv"'],
initial_columns: ['filename'],
columns: {
filename: {
level: 'required',
description_addendum: 'There MUST be exactly one row for each file.',
},
acq_time__scans: 'optional',
},
index_columns: ['filename'],
additional_columns: 'allowed',
},
},
made_up: {
MadeUp: {
columns: {
onset: 'required',
strain_rrid: 'optional',
},
},
},
},
},
}

Expand Down Expand Up @@ -107,3 +132,54 @@ Deno.test(
assert(context.issues.hasIssue({ key: 'CHECK_ERROR' }))
},
)

Deno.test('evalColumns tests', async (t) => {
const schema = await loadSchema()

await t.step('check invalid datetime (scans.tsv:acq_time)', () => {
const context = {
path: '/sub-01/sub-01_scans.tsv',
extension: '.tsv',
sidecar: {},
columns: {
filename: ['func/sub-01_task-rest_bold.nii.gz'],
acq_time: ['1900-01-01T00:00:78'],
},
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' }))
})

await t.step('check formatless column', () => {
const context = {
path: '/sub-01/sub-01_something.tsv',
extension: '.tsv',
sidecar: {},
columns: {
onset: ['1', '2', 'not a number'],
},
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' }))
})

await t.step('verify n/a is allowed', () => {
const context = {
path: '/sub-01/sub-01_something.tsv',
extension: '.tsv',
sidecar: {},
columns: {
onset: ['1', '2', 'n/a'],
strain_rrid: ['RRID:SCR_012345', 'RRID:SCR_012345', 'n/a'],
},
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)
})
})
8 changes: 6 additions & 2 deletions bids-validator/src/schema/applyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ function schemaObjectTypeCheck(
return schemaObject.enum.some((x) => x === value)
}
// @ts-expect-error
const format = schema.objects.formats[schemaObject.type]
const format = schemaObject.format
// @ts-expect-error
? schema.objects.formats[schemaObject.format]
// @ts-expect-error
: schema.objects.formats[schemaObject.type]
const re = new RegExp(`^${format.pattern}$`)
return re.test(value)
}
Expand Down Expand Up @@ -208,7 +212,7 @@ function sidecarDefinedTypeCheck(
* otherwise we type check each value in the column according to the type
* specified in the schema rule (or sidecar type information if applicable).
*/
function evalColumns(
export function evalColumns(
rule: GenericRule,
context: BIDSContext,
schema: GenericSchema,
Expand Down
Loading