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

Generate JSON schema's from spec schema to validate values in sidecars. #2020

Merged
merged 19 commits into from
Jul 30, 2024

Conversation

rwblair
Copy link
Member

@rwblair rwblair commented Jul 12, 2024

No description provided.

@rwblair rwblair marked this pull request as draft July 12, 2024 15:22
Copy link

codecov bot commented Jul 12, 2024

Codecov Report

Attention: Patch coverage is 68.60465% with 27 lines in your changes missing coverage. Please review.

Project coverage is 87.10%. Comparing base (1b17f77) to head (180d4fa).

Files Patch % Lines
bids-validator/src/schema/applyRules.ts 72.09% 12 Missing ⚠️
bids-validator/src/schema/context.ts 63.63% 12 Missing ⚠️
bids-validator/src/utils/objectPathHandler.ts 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2020      +/-   ##
==========================================
+ Coverage   85.75%   87.10%   +1.34%     
==========================================
  Files          91      133      +42     
  Lines        3785     6411    +2626     
  Branches     1218     1545     +327     
==========================================
+ Hits         3246     5584    +2338     
- Misses        453      737     +284     
- Partials       86       90       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rwblair
Copy link
Member Author

rwblair commented Jul 12, 2024

This isn't using AJV to validate column value types yet. Currently all values read in from a tsv are treated as strings. The schema specifies the type of some columns as numbers. For now the code looks up the format associated with the type and runs that regex on the values. I'll need to attempt coercion of values if we want type checking there to be similar to json files.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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/.

Copy link
Member

Choose a reason for hiding this comment

The 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?

@effigies
Copy link
Collaborator

I'll need to attempt coercion of values if we want type checking there to be similar to json files.

I think the regex checking is probably going to be faster than coercion and validation, and it gets us where we want to be.

Co-authored-by: Chris Markiewicz <effigies@gmail.com>
@@ -47,6 +47,10 @@ export const filenameIssues: IssueDefinitionRecord = {
severity: "warning",
reason: "A data files JSON sidecar is missing a key listed as recommended.",
},
JSON_SCHEMA_VALIDATION_ERROR: {
Copy link
Member Author

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.

const validate = context.dataset.ajv.compile(schema.objects.metadata[keyName])
const result = validate(context.sidecar[keyName])
if (result === false) {
const evidenceBase = `Failed for this file.key: ${originFileKey} Schema path: ${schemaPath}`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors could take schema path as a seperate argument and print the entire rule. https://deno.land/x/json2yaml@v1.0.1

@rwblair
Copy link
Member Author

rwblair commented Jul 29, 2024

@effigies
Copy link
Collaborator

effigies commented Jul 29, 2024

Actually a schema bug. Should be:

diff --git a/src/schema/rules/sidecars/meg.yaml b/src/schema/rules/sidecars/meg.yaml
index 33d851f6..5391c829 100644
--- a/src/schema/rules/sidecars/meg.yaml
+++ b/src/schema/rules/sidecars/meg.yaml
@@ -205,7 +205,7 @@ MEGCoordsystemDigitizedHeadPoints:
     - datatype == "meg"
     - suffix == "coordsystem"
   fields:
-    DigitizedHeadPoints: optional
+    DigitizedHeadPoints__coordsystem: optional
     DigitizedHeadPointsCoordinateSystem: optional
     DigitizedHeadPointsCoordinateUnits: optional
     DigitizedHeadPointsCoordinateSystemDescription:

@effigies
Copy link
Collaborator

Should be able to test the above patch with:

BIDS_SCHEMA=https://bids-specification--1876.org.readthedocs.build/en/1876/schema.json deno test ...

@effigies effigies force-pushed the schema/json_array_type_checks branch from 675c62d to 6b61737 Compare July 29, 2024 22:46
@effigies
Copy link
Collaborator

@rwblair I pushed a small fix in 6b61737, but otherwise I think we're good to go on this.

@rwblair rwblair marked this pull request as ready for review July 30, 2024 14:09
Copy link
Collaborator

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. I have a potential refactor that pulls the ajv out of context.dataset, but I don't want to hold this up on that. I'll clean it up and make a PR after this is in, if it seems worth the effort.

@effigies effigies merged commit ccfd5db into bids-standard:master Jul 30, 2024
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants