-
Notifications
You must be signed in to change notification settings - Fork 111
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
Generate JSON schema's from spec schema to validate values in sidecars. #2020
Changes from all commits
1606d7f
27a4c45
9231f97
90032c4
5b00b5f
13e666c
52e1a74
313ef04
9ed3e6e
fd1575a
60bbe35
ca0dec4
aaaff98
6b61737
672fa43
8b5ec12
92cbef9
1945f1e
180d4fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# Mon Jul 29 19:08:32 2024 -0400 - [email protected] - chore(fmt): deno fmt [ignore-rev] | ||
92cbef9624663b719b7b4c22b6104669eb518f09 | ||
# Sun Jul 28 22:01:32 2024 -0400 - [email protected] - chore(fmt): deno fmt [ignore-rev] | ||
1bccd0a2d09f1b7ff6a26d99c7845bf59908c421 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Ajv, type JSONSchemaType, type ValidateFunction } from 'https://esm.sh/[email protected]' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
import { buildAssociations } from './associations.ts' | ||
import { ValidatorOptions } from '../setup/options.ts' | ||
import { logger } from '../utils/logger.ts' | ||
import { Ajv, JSONSchemaType, ValidateFunction } from '../deps/ajv.ts' | ||
import { memoize } from '../utils/memoize.ts' | ||
import { Schema } from '../types/schema.ts' | ||
|
||
export class BIDSContextDataset implements ContextDataset { | ||
dataset_description: Record<string, unknown> | ||
|
@@ -25,13 +28,22 @@ | |
ignored: any[] | ||
modalities: any[] | ||
subjects?: ContextDatasetSubjects | ||
ajv: Ajv | ||
sidecarKeyValidated: Set<string> | ||
|
||
constructor(options?: ValidatorOptions, description = {}) { | ||
constructor(options?: ValidatorOptions, schema?: Schema, description = {}) { | ||
this.dataset_description = description | ||
this.files = [] | ||
this.tree = {} | ||
this.ignored = [] | ||
this.modalities = [] | ||
this.ajv = new Ajv({ strictSchema: false }) | ||
// @ts-expect-error | ||
this.ajv.compile = memoize(this.ajv.compile) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't been able to figure out how to make these types mesh. Could store it as another entry in the dataset context/. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe AJV itself should be a private field and compile is just a method on the context? |
||
this.sidecarKeyValidated = new Set<string>() | ||
if (schema) { | ||
this.setCustomAjvFormats(schema) | ||
} | ||
if (options) { | ||
this.options = options | ||
} | ||
|
@@ -44,6 +56,27 @@ | |
this.dataset_description.DatasetType = 'raw' | ||
} | ||
} | ||
|
||
setCustomAjvFormats(schema: Schema): void { | ||
if (typeof schema.objects.formats !== 'object') { | ||
// logger.warning( | ||
console.log( | ||
`schema.objects.formats missing from schema, format validation disabled.`, | ||
) | ||
return | ||
} | ||
const schemaFormats = schema.objects.formats | ||
for (let key of Object.keys(schemaFormats)) { | ||
if (typeof schemaFormats[key]['pattern'] !== 'string') { | ||
// logger.warning( | ||
console.log( | ||
`schema.objects.formats.${key} pattern missing or invalid. Skipping this format for addition to context json validator`, | ||
) | ||
continue | ||
} | ||
this.ajv.addFormat(key, schemaFormats[key]['pattern']) | ||
} | ||
} | ||
} | ||
|
||
export class BIDSContextDatasetSubjects implements ContextDatasetSubjects { | ||
|
@@ -78,6 +111,7 @@ | |
datatype: string | ||
modality: string | ||
sidecar: Record<string, any> | ||
sidecarKeyOrigin: Record<string, string> | ||
json: object | ||
columns: ColumnsMap | ||
associations: ContextAssociations | ||
|
@@ -102,6 +136,7 @@ | |
this.datatype = '' | ||
this.modality = '' | ||
this.sidecar = {} | ||
this.sidecarKeyOrigin = {} | ||
this.columns = new ColumnsMap() | ||
this.json = {} | ||
this.associations = {} as ContextAssociations | ||
|
@@ -166,6 +201,7 @@ | |
.then((text) => JSON.parse(text)) | ||
.catch((error) => {}) | ||
this.sidecar = { ...this.sidecar, ...json } | ||
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] = validSidecars[0].path) | ||
} | ||
const nextDir = fileTree.directories.find((directory) => { | ||
return this.file.path.startsWith(`${directory.path}/`) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should pull this code from schema.rules.errors (and make all other errors there available. Internally represent with code as the key and rest of object as contents. add
addSchemaIssue
to src/issues/schemaIssues.ts or something.