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

mrc-2476: Upgrade ajv to version 8.5.0 and include draft-04 extension #41

Merged
merged 26 commits into from
Jul 6, 2021

Conversation

r-ash
Copy link
Collaborator

@r-ash r-ash commented Jun 21, 2021

See https://ajv.js.org/json-schema.html#draft-04

  • Upgrade ajv to 8.5.0
  • Include draft-04 schema extension
  • Include ajv-formats plugin
  • Add arg strict to json_validator and json_validate FALSE by default which allows permissive handling of schema validation see https://ajv.js.org/strict-mode.html
  • If running with schema > draft-04 then fallback to using ajv and print message to user
  • Add dataPath property to verbose errors, for backwards compatibility as this has been renamed to instancePath in ajv version 8+

Checked this is working with biocompute, and with hintr. Checked this is working with dependencies using revdepcheck::revdep_check() too - failing on TKCat

Issues encountered (which I think I have fixed)

Remaining issue

  • TKCat uses "$schema": {"enum": ["TKCat_collections_1.0"]}, which throws
Unknown meta schema version 'TKCat_collections_1.0'
E> --- failed re-building ‘TKCat-Collections.Rmd

With cran version they are validating against imjv. But with updated version currently on master this won't work as it tries to parse the version of the schema so it errors early if not using draft-04. This didn't error previously so I assume validated permissively. Now updated to support 2019-09 this is failing validation on ajv. I don't really know what this is - some kind of metaschema? Not sure if "$schema": {"enum": ["TKCat_collections_1.0"]} is ever valid?

@r-ash r-ash marked this pull request as ready for review June 21, 2021 16:10
@r-ash r-ash requested a review from richfitz June 21, 2021 16:10
Copy link
Member

@richfitz richfitz left a comment

Choose a reason for hiding this comment

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

Nice, this is fantastic. A few minor changes below, most important is to check that we didn't break any of the other packages (if we did we'll need to leave imjv as the default, perhaps unless draft >= 4 is detected)

NEWS.md Outdated Show resolved Hide resolved
R/validate.R Outdated
error = FALSE, engine = "imjv", reference = NULL,
query = NULL) {
tmp <- json_validator(schema, engine, reference = reference)
error = FALSE, engine = "ajv", reference = NULL,
Copy link
Member

Choose a reason for hiding this comment

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

If we make this change we might break other packages, can you check please?

https://cran.r-project.org/web/packages/jsonvalidate/index.html - see devtools::revdep to automate

R/validate.R Outdated
tmp <- json_validator(schema, engine, reference = reference)
error = FALSE, engine = "ajv", reference = NULL,
query = NULL, strict = FALSE) {
tmp <- json_validator(schema, engine, reference = reference, strict = strict)
Copy link
Member

Choose a reason for hiding this comment

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

While we're here, could you rename tmp to something better (validator, perhaps?)

R/validate.R Outdated Show resolved Hide resolved
js/in.js Outdated
if (meta_schema_version === "draft-06") {
ret.addMetaSchema(AjvSchema6);
}
addFormats(ret);
Copy link
Member

Choose a reason for hiding this comment

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

Aha, is that how you do this?

v8 <- env$ct
schema <- read_schema(schema, v8)
switch(engine,
imjv = json_validator_imjv(schema, v8, reference),
ajv = json_validator_ajv(schema, v8, reference),
ajv = json_validator_ajv(schema, v8, reference, strict),
Copy link
Member

Choose a reason for hiding this comment

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

What happens if we try and use imjv with a meta schema version of draft-6/7? Still throws an error? We should probably document that. Also the docstring for engine needs updating to reflect the new default

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It still throws an error yeah - this referenced issue from the code mafintosh/is-my-json-valid#160 still a problem

Alternative approach to compatibility
@r-ash r-ash requested a review from richfitz July 6, 2021 09:56
.github/workflows/R-CMD-check.yaml Outdated Show resolved Hide resolved
.github/workflows/test-coverage.yaml Outdated Show resolved Hide resolved
r-ash and others added 2 commits July 6, 2021 11:08
Co-authored-by: Rich FitzJohn <r.fitzjohn@imperial.ac.uk>
Co-authored-by: Rich FitzJohn <r.fitzjohn@imperial.ac.uk>
@r-ash r-ash requested a review from richfitz July 6, 2021 10:09
@richfitz richfitz merged commit 0d99561 into master Jul 6, 2021
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.

None yet

2 participants