-
Notifications
You must be signed in to change notification settings - Fork 156
Add JSON lockfile schema and validation function #1889
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
#' Validate the renv lockfile against a schema | ||
#' | ||
#' @description | ||
#' `renv::lockfile_validate()` can be used to validate your `renv.lock` | ||
#' against a default or custom schema. It can be used to automate checks, | ||
#' check for obvious errors, and ensure that any custom fields you add fit | ||
#' your specific needs. | ||
#' | ||
#' @details | ||
#' See the [JSON Schema docs](https://json-schema.org/) for more information | ||
#' on JSON schemas, their use in validation, and how to write your own schema. | ||
#' | ||
#' `renv::lockfile_validate()` wraps ROpenSci's | ||
#' [`jsonvalidate`](https://docs.ropensci.org/jsonvalidate/) package, passing | ||
#' many of its parameters to that package's `json_validate()` function. Use | ||
#' `?jsonvalidate::json_validate` for more information. | ||
#' | ||
#' @param lockfile Contents of the lockfile, or a filename containing one. | ||
#' If not provided, it defaults to the project's lockfile. | ||
#' | ||
#' @param schema Contents of a renv schema, or a filename containing a schema. | ||
#' If not provided, renv's default schema is used. | ||
#' | ||
#' @param greedy Boolean. Continue after first error? | ||
#' | ||
#' @param error Boolean. Throw an error on parse failure? | ||
#' | ||
#' @param verbose Boolean. If `TRUE`, then an attribute `errors` will list validation failures as a `data.frame`. | ||
#' | ||
#' @param strict Boolean. Set whether the schema should be parsed strictly or not. | ||
#' If in strict mode schemas will error to "prevent any unexpected behaviours or silently ignored mistakes in user schema". | ||
#' For example it will error if encounters unknown formats or unknown keywords. | ||
#' See https://ajv.js.org/strict-mode.html for details. | ||
#' | ||
#' @return Boolean. `TRUE` if validation passes. `FALSE` if validation fails. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' | ||
#' # validate the project's lockfile | ||
#' renv::lockfile_validate() | ||
#' | ||
#' # validate the project's lockfile using a non-default schema | ||
#' renv::lockfile_validate(schema = "/path/to/your/custom/schema.json") | ||
#' | ||
#' # validate a lockfile using its path | ||
#' renv::lockfile_validate(lockfile = "/path/to/your/renv.lock") | ||
#' } | ||
#' @export | ||
lockfile_validate <- function(project = NULL, | ||
lockfile = NULL, # Use default project lockfile if not provided | ||
schema = NULL, # Use default schema if not provided | ||
greedy = FALSE, | ||
error = FALSE, | ||
verbose = FALSE, | ||
strict = FALSE) | ||
{ | ||
|
||
project <- renv_project_resolve(project) | ||
lockfile <- lockfile %||% renv_lockfile_path(project = project) | ||
schema <- schema %||% system.file("schema", | ||
"draft-07.renv.lock.schema.json", | ||
package = "renv", | ||
mustWork = TRUE) | ||
|
||
# "ajv" engine required for schema specifications later than draft-04 | ||
jsonvalidate::json_validate(lockfile, | ||
jrdnbradford marked this conversation as resolved.
Show resolved
Hide resolved
|
||
schema, | ||
engine = "ajv", | ||
greedy = greedy, | ||
error = error, | ||
verbose = verbose, | ||
strict = strict) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$comment": "See https://github.com/rstudio/renv", | ||
"title": "renv.lock file", | ||
"description": "A schema for renv.lock files generated by {renv}", | ||
"type": "object", | ||
"properties": { | ||
"R": { | ||
"description": "Version of R used in the project", | ||
"type": "object", | ||
"properties": { | ||
"Version": { | ||
"description": "The version of R used", | ||
"type": "string", | ||
"examples": ["4.2.3"] | ||
}, | ||
"Repositories": { | ||
"description": "The R repositories used in this project", | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"Name": { | ||
"description": "Name of the repository", | ||
"type": "string", | ||
"examples": ["CRAN"] | ||
}, | ||
"URL": { | ||
"description": "URL of the repository", | ||
"type": "string", | ||
"format": "uri", | ||
"examples": ["https://cloud.r-project.org"] | ||
} | ||
}, | ||
"required": ["Name", "URL"] | ||
} | ||
} | ||
}, | ||
"required": ["Version", "Repositories"] | ||
}, | ||
"Bioconductor": { | ||
"description": "", | ||
"type": "object", | ||
"properties": { | ||
"Version": { | ||
"description": "Release of Bioconductor", | ||
"type": "string", | ||
"examples": ["3.18"] | ||
} | ||
}, | ||
"required": ["Version"] | ||
}, | ||
"Python": { | ||
"description": "Version of Python used in the project", | ||
"type": "object", | ||
"properties": { | ||
"Name": { | ||
"description": "Path to the Python environment", | ||
"type": "string", | ||
"examples": [ | ||
".venv", | ||
"./renv/python/virtualenvs/renv-python-3.10" | ||
] | ||
}, | ||
"Type": { | ||
"description": "Type of Python environment", | ||
"type": "string", | ||
"examples": ["virtualenv"] | ||
}, | ||
"Version": { | ||
"description": "Version of Python required", | ||
"type": "string", | ||
"examples": ["3.10.12", "3.9.0"] | ||
} | ||
}, | ||
"required": ["Name", "Type", "Version"] | ||
}, | ||
"Packages": { | ||
"description": "Packages required by the R project", | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "object", | ||
"properties": { | ||
"Package": { | ||
"description": "The package name", | ||
"type": "string", | ||
"examples": ["ggplot2", "dplyr"] | ||
}, | ||
"Version": { | ||
"description": "The package version", | ||
"type": "string", | ||
"examples": ["1.0.0", "3.4.6"] | ||
}, | ||
"Source": { | ||
"description": "The location from which this package was retrieved", | ||
"type": "string", | ||
"examples": [ | ||
"Repository", | ||
"Bioconductor", | ||
"/mnt/r/pkg/package_name_1.0.1.tar.gz" | ||
] | ||
}, | ||
"Repository": { | ||
"description": "The name of the repository (if any) from which this package was retrieved", | ||
"type": "string", | ||
"examples": ["CRAN"] | ||
}, | ||
"Hash": { | ||
"description": "A unique hash for this package, used for package caching", | ||
"type": "string", | ||
"pattern": "^[a-fA-F0-9]{32}$", | ||
"examples": ["06230136b2d2b9ba5805e1963fa6e890"] | ||
}, | ||
"biocViews": { | ||
"description": "Bioconductor package dependencies", | ||
"type": "string" | ||
}, | ||
"RemoteType": { | ||
"description": "Type of the remote, typically written for packages installed by the devtools, remotes and pak packages", | ||
"type": "string", | ||
"examples": ["standard", "github"] | ||
}, | ||
"RemoteHost": { | ||
"description": "Host for the remote", | ||
"type": "string", | ||
"format": "hostname", | ||
"examples": ["api.github.com"] | ||
}, | ||
"RemoteUsername": { | ||
"description": "Username for the remote", | ||
"type": "string" | ||
}, | ||
"RemoteRepo": { | ||
"description": "Repositories for the package", | ||
"type": "string", | ||
"examples": [ | ||
"https://cran.rstudio.com", | ||
"https://cloud.r-project.org" | ||
] | ||
}, | ||
"RemoteRepos": { | ||
"description": "Repositories for the package", | ||
"type": "string", | ||
"format": "uri", | ||
"examples": [ | ||
"https://cran.rstudio.com", | ||
"https://cloud.r-project.org" | ||
] | ||
}, | ||
"RemoteRef": { | ||
"description": "Ref of the package", | ||
"type": "string", | ||
"examples": ["renv", "main"] | ||
}, | ||
"RemotePkgRef": { | ||
"description": "Name of the packages", | ||
"type": "string" | ||
}, | ||
"RemotePkgPlatform": { | ||
"description": "Architecture/platform of the remote", | ||
"type": "string", | ||
"examples": ["aarch64-apple-darwin20"] | ||
}, | ||
"RemoteSha": { | ||
"description": "Version number of the package", | ||
"type": "string", | ||
"examples": ["1763e0dcb72fb58d97bab97bb834fc71f1e012bc"] | ||
}, | ||
"Requirements": { | ||
"description": "Dependencies of the package", | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"examples": [ | ||
[ | ||
"R", | ||
"jsonlite", | ||
"lifecycle", | ||
"magrittr", | ||
"stringi" | ||
], | ||
[ | ||
"R6", | ||
"Rcpp", | ||
"later", | ||
"magrittr", | ||
"rlang", | ||
"stats" | ||
] | ||
] | ||
} | ||
}, | ||
"required": ["Package"] | ||
} | ||
} | ||
}, | ||
"required": ["R", "Packages"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.