|
1 | | -#' Check Data Package object |
| 1 | +#' Check a Data Package object |
2 | 2 | #' |
3 | | -#' Check if an object is a list describing a Data Package, i.e. it has the |
4 | | -#' required properties `resources` and `directory`. |
| 3 | +#' Check if an object is a Data Package object with the required properties. |
5 | 4 | #' |
6 | | -#' @param package List describing a Data Package. |
| 5 | +#' @inheritParams read_resource |
7 | 6 | #' @return `TRUE` or error. |
8 | 7 | #' @family check functions |
9 | | -#' @noRd |
| 8 | +#' @export |
10 | 9 | check_package <- function(package) { |
11 | | - # Check package is a list with resources (list) and directory (character) |
12 | | - if ( |
13 | | - !is.list(package) || |
14 | | - !all(c("resources", "directory") %in% names(package)) || |
15 | | - !is.list(package$resources) || |
16 | | - !is.character(package$directory) |
17 | | - ) { |
| 10 | + general_message <- "{.arg package} must be a Data Package object." |
| 11 | + tip_message <- paste( |
| 12 | + "Create a valid Data Package object with {.fun read_package} or ", |
| 13 | + "{.fun create_package}." |
| 14 | + ) |
| 15 | + |
| 16 | + # Check package is a list |
| 17 | + if (!is.list(package)) { |
| 18 | + cli::cli_abort( |
| 19 | + c( |
| 20 | + general_message, |
| 21 | + "x" = "{.arg package} is not a list.", |
| 22 | + "i" = tip_message |
| 23 | + ), |
| 24 | + class = "frictionless_error_package_invalid" |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + # Check package has resources (list) |
| 29 | + if (!is.list(package$resources)) { |
| 30 | + cli::cli_abort( |
| 31 | + c( |
| 32 | + general_message, |
| 33 | + "x" = "{.arg package} is missing a {.field resources} property or it is |
| 34 | + not a list.", |
| 35 | + "i" = tip_message |
| 36 | + ), |
| 37 | + class = "frictionless_error_package_invalid" |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + # Check package has directory (character) |
| 42 | + if (!is.character(package$directory)) { |
18 | 43 | cli::cli_abort( |
19 | | - "{.arg package} must be a list describing a Data Package created with |
20 | | - {.fun read_package} or {.fun create_package}.", |
| 44 | + c( |
| 45 | + general_message, |
| 46 | + "x" = "{.arg package} is missing a {.field directory} property or it is |
| 47 | + not a character.", |
| 48 | + "i" = tip_message |
| 49 | + ), |
21 | 50 | class = "frictionless_error_package_invalid" |
22 | 51 | ) |
23 | 52 | } |
24 | 53 |
|
25 | 54 | # Check all resources (if any) have a name |
26 | 55 | if (purrr::some(package$resources, ~ is.null(.x$name))) { |
27 | 56 | cli::cli_abort( |
28 | | - "All resources in {.arg package} must have a {.field name} property.", |
| 57 | + "All {.field resources} in {.arg package} must have a {.field name} |
| 58 | + property.", |
29 | 59 | class = "frictionless_error_resources_without_name" |
30 | 60 | ) |
31 | 61 | } |
32 | 62 |
|
| 63 | + # Warn if class is missing |
| 64 | + if (!"datapackage" %in% class(package)) { |
| 65 | + cli::cli_warn( |
| 66 | + c( |
| 67 | + general_message, |
| 68 | + "x" = "{.arg package} is missing a {.val datapackage} class.", |
| 69 | + "i" = tip_message |
| 70 | + ), |
| 71 | + class = "frictionless_warning_package_without_class" |
| 72 | + ) |
| 73 | + } |
| 74 | + |
33 | 75 | return(TRUE) |
34 | 76 | } |
0 commit comments