From f3e6012e9671b83cf52c8e17a33f8d6357ff477b Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 7 Aug 2024 13:00:39 -0400 Subject: [PATCH] feat: Add .schema to context.dataset, proxy as context.schema --- bids-validator/src/schema/context.ts | 12 +++++++++--- bids-validator/src/schema/walk.test.ts | 4 ++-- .../src/tests/local/hed-integration.test.ts | 8 ++++++-- bids-validator/src/types/context.ts | 4 ++-- bids-validator/src/validators/bids.ts | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/bids-validator/src/schema/context.ts b/bids-validator/src/schema/context.ts index 8558f4246..963bf9cc7 100644 --- a/bids-validator/src/schema/context.ts +++ b/bids-validator/src/schema/context.ts @@ -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' @@ -30,9 +30,11 @@ export class BIDSContextDataset implements ContextDataset { issues: DatasetIssues sidecarKeyValidated: Set options?: ValidatorOptions + schema: Schema constructor( options?: ValidatorOptions, + schema?: Schema, tree?: FileTree, description?: Record, ignored?: BIDSFile[], @@ -40,6 +42,7 @@ export class BIDSContextDataset implements ContextDataset { 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 || [] @@ -82,7 +85,6 @@ export class BIDSContextDatasetSubjects implements ContextDatasetSubjects { } export class BIDSContext implements Context { - schema?: GenericSchema dataset: BIDSContextDataset subject: ContextSubject // path: string <- getter @@ -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 = '' @@ -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 } diff --git a/bids-validator/src/schema/walk.test.ts b/bids-validator/src/schema/walk.test.ts index 1bdd823b0..d5561d758 100644 --- a/bids-validator/src/schema/walk.test.ts +++ b/bids-validator/src/schema/walk.test.ts @@ -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, @@ -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( diff --git a/bids-validator/src/tests/local/hed-integration.test.ts b/bids-validator/src/tests/local/hed-integration.test.ts index 0a64a5b20..c7e03e439 100644 --- a/bids-validator/src/tests/local/hed-integration.test.ts +++ b/bids-validator/src/tests/local/hed-integration.test.ts @@ -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) @@ -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) diff --git a/bids-validator/src/types/context.ts b/bids-validator/src/types/context.ts index 0db8a7922..6c4d79910 100644 --- a/bids-validator/src/types/context.ts +++ b/bids-validator/src/types/context.ts @@ -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' @@ -95,7 +95,7 @@ export interface ContextNiftiHeader { sform_code: number } export interface Context { - schema?: GenericSchema + schema: Schema dataset: ContextDataset subject: ContextSubject path: string diff --git a/bids-validator/src/validators/bids.ts b/bids-validator/src/validators/bids.ts index 1e7eb71c2..77275ad67 100644 --- a/bids-validator/src/validators/bids.ts +++ b/bids-validator/src/validators/bids.ts @@ -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])