Skip to content

Commit

Permalink
feat: Add .schema to context.dataset, proxy as context.schema
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Aug 7, 2024
1 parent 841e6c1 commit f3e6012
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
12 changes: 9 additions & 3 deletions bids-validator/src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ContextNiftiHeader,
ContextSubject,
} from '../types/context.ts'
import { GenericSchema } from '../types/schema.ts'
import { Schema } from '../types/schema.ts'
import { BIDSFile, FileTree } from '../types/filetree.ts'
import { ColumnsMap } from '../types/columns.ts'
import { readEntities } from './entities.ts'
Expand All @@ -30,16 +30,19 @@ export class BIDSContextDataset implements ContextDataset {
issues: DatasetIssues
sidecarKeyValidated: Set<string>
options?: ValidatorOptions
schema: Schema

constructor(
options?: ValidatorOptions,
schema?: Schema,
tree?: FileTree,
description?: Record<string, unknown>,
ignored?: BIDSFile[],
datatypes?: string[],
modalities?: string[],
issues?: DatasetIssues,
) {
this.schema = schema || {} as unknown as Schema
this.dataset_description = description || {}
this.tree = tree || new FileTree('/unknown', 'unknown')
this.ignored = ignored || []
Expand Down Expand Up @@ -82,7 +85,6 @@ export class BIDSContextDatasetSubjects implements ContextDatasetSubjects {
}

export class BIDSContext implements Context {
schema?: GenericSchema
dataset: BIDSContextDataset
subject: ContextSubject
// path: string <- getter
Expand Down Expand Up @@ -116,7 +118,7 @@ export class BIDSContext implements Context {
this.suffix = bidsEntities.suffix
this.extension = bidsEntities.extension
this.entities = bidsEntities.entities
this.dataset = dsContext ? dsContext : new BIDSContextDataset(undefined, fileTree)
this.dataset = dsContext ? dsContext : new BIDSContextDataset(undefined, undefined, fileTree)
this.subject = {} as ContextSubject
this.datatype = ''
this.modality = ''
Expand All @@ -127,6 +129,10 @@ export class BIDSContext implements Context {
this.associations = {} as ContextAssociations
}

get schema(): Schema {
return this.dataset.schema
}

get size(): number {
return this.file.size
}
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/schema/walk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { simpleDataset, simpleDatasetFileCount } from '../tests/simple-dataset.t

Deno.test('file tree walking', async (t) => {
await t.step('visits each file and creates a BIDSContext', async () => {
const dsContext = new BIDSContextDataset(undefined, simpleDataset)
const dsContext = new BIDSContextDataset(undefined, undefined, simpleDataset)
for await (const context of walkFileTree(dsContext)) {
assert(
context instanceof BIDSContext,
Expand All @@ -15,7 +15,7 @@ Deno.test('file tree walking', async (t) => {
}
})
await t.step('visits every file expected', async () => {
const dsContext = new BIDSContextDataset(undefined, simpleDataset)
const dsContext = new BIDSContextDataset(undefined, undefined, simpleDataset)
let accumulator = 0
for await (const context of walkFileTree(dsContext)) {
assert(
Expand Down
8 changes: 6 additions & 2 deletions bids-validator/src/tests/local/hed-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ Deno.test('hed-validator not triggered', async (t) => {
const PATH = 'tests/data/bids-examples/ds003'
const tree = await readFileTree(PATH)
const schema = await loadSchema()
const dsContext = new BIDSContextDataset(undefined, undefined, { 'HEDVersion': ['bad_version'] })
const dsContext = new BIDSContextDataset(undefined, undefined, undefined, {
'HEDVersion': ['bad_version'],
})
await t.step('detect hed returns false', async () => {
const eventFile = getFile(tree, 'sub-01/func/sub-01_task-rhymejudgment_events.tsv')
assert(eventFile !== undefined)
Expand All @@ -46,7 +48,9 @@ Deno.test('hed-validator fails with bad schema version', async (t) => {
const PATH = 'tests/data/bids-examples/eeg_ds003645s_hed_library'
const tree = await readFileTree(PATH)
const schema = await loadSchema()
const dsContext = new BIDSContextDataset(undefined, undefined, { 'HEDVersion': ['bad_version'] })
const dsContext = new BIDSContextDataset(undefined, undefined, undefined, {
'HEDVersion': ['bad_version'],
})
await t.step('detect hed returns false', async () => {
const eventFile = getFile(tree, 'sub-002/eeg/sub-002_task-FacePerception_run-3_events.tsv')
assert(eventFile !== undefined)
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GenericSchema } from './schema.ts'
import { Schema } from './schema.ts'
import { ValidatorOptions } from '../setup/options.ts'
import { FileTree } from '../types/filetree.ts'

Expand Down Expand Up @@ -95,7 +95,7 @@ export interface ContextNiftiHeader {
sform_code: number
}
export interface Context {
schema?: GenericSchema
schema: Schema
dataset: ContextDataset
subject: ContextSubject
path: string
Expand Down
2 changes: 1 addition & 1 deletion bids-validator/src/validators/bids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function validate(
(file: BIDSFile) => file.name === 'dataset_description.json',
)

const dsContext = new BIDSContextDataset(options, fileTree)
const dsContext = new BIDSContextDataset(options, schema, fileTree)
if (ddFile) {
dsContext.dataset_description = await loadJSON(ddFile).catch((error) => {
dsContext.issues.addNonSchemaIssue(error.key, [ddFile])
Expand Down

0 comments on commit f3e6012

Please sign in to comment.