Debian Codegraph @

R/lockfile-validate.R

raw
1
2 #' Validate an renv lockfile against a schema
3 #'
4 #' @description
5 #' `renv::lockfile_validate()` can be used to validate your `renv.lock`
6 #' against a default or custom schema. It can be used to automate checks,
7 #' check for obvious errors, and ensure that any custom fields you add fit
8 #' your specific needs.
9 #'
10 #' @details
11 #' See the [JSON Schema docs](https://json-schema.org/) for more information
12 #' on JSON schemas, their use in validation, and how to write your own schema.
13 #'
14 #' `renv::lockfile_validate()` wraps ROpenSci's
15 #' [`jsonvalidate`](https://docs.ropensci.org/jsonvalidate/) package, passing
16 #' many of its parameters to that package's `json_validate()` function. Use
17 #' `?jsonvalidate::json_validate` for more information.
18 #'
19 #' @inheritParams renv-params
20 #'
21 #' @param lockfile Contents of the lockfile, or a filename containing one.
22 #' If not provided, it defaults to the project's lockfile.
23 #'
24 #' @param schema Contents of a renv schema, or a filename containing a schema.
25 #' If not provided, renv's default schema is used.
26 #'
27 #' @param greedy Boolean. Continue after first error?
28 #'
29 #' @param error Boolean. Throw an error on parse failure?
30 #'
31 #' @param verbose Boolean. If `TRUE`, then an attribute `errors` will list
32 #' validation failures as a `data.frame`.
33 #'
34 #' @param strict Boolean. Set whether the schema should be parsed strictly or
35 #' not. If in strict mode schemas will error to "prevent any unexpected
36 #' behaviours or silently ignored mistakes in user schema". For example it
37 #' will error if encounters unknown formats or unknown keywords. See
38 #' https://ajv.js.org/strict-mode.html for details.
39 #'
40 #' @return Boolean. `TRUE` if validation passes. `FALSE` if validation fails.
41 #'
42 #' @examples
43 #' \dontrun{
44 #'
45 #' # validate the project's lockfile
46 #' renv::lockfile_validate()
47 #'
48 #' # validate the project's lockfile using a non-default schema
49 #' renv::lockfile_validate(schema = "/path/to/your/custom/schema.json")
50 #'
51 #' # validate a lockfile using its path
52 #' renv::lockfile_validate(lockfile = "/path/to/your/renv.lock")
53 #' }
54 #'
55 #' @keywords internal
56 #' @export
57 lockfile_validate <- function(project = NULL,
58 lockfile = NULL, # Use default project lockfile if not provided
59 schema = NULL, # Use default schema if not provided
60 greedy = FALSE,
61 error = FALSE,
62 verbose = FALSE,
63 strict = FALSE)
64 {
65
66 project <- renv_project_resolve(project)
67 lockfile <- lockfile %||% renv_lockfile_path(project = project)
68 schema <- schema %||% system.file("schema",
69 "draft-07.renv.lock.schema.json",
70 package = "renv",
71 mustWork = TRUE)
72
73 # "ajv" engine required for schema specifications later than draft-04
74 jsonvalidate::json_validate(lockfile,
75 schema,
76 engine = "ajv",
77 greedy = greedy,
78 error = error,
79 verbose = verbose,
80 strict = strict)
81 }