Skip to content

Commit

Permalink
Potential solution for frictionlessdata#254
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafnuss committed Aug 21, 2024
1 parent f9185cb commit 677a953
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions R/add_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ add_resource <- function(package, resource_name, data, schema = NULL,
# Check schema (also checks df)
check_schema(schema, df)

# Order df according to schema
schema_name <- purrr::map_chr(schema$fields, ~ .x$name %||% NA_character_)
schema_name_available <- schema_name[schema_name%in% names(df)]
df <- df[,schema_name_available]

# Check ellipsis
if (...length() != length(...names())) {
cli::cli_abort(
Expand Down
5 changes: 4 additions & 1 deletion R/check_schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ check_schema <- function(schema, data = NULL) {
)
}

# Check required value
field_required <- purrr::map_chr(fields, ~ .x$constraints$required %||% NA)

# Check data when present
if (!is.null(data)) {
check_data(data)

col_names <- colnames(data)
if (!identical(field_names, col_names)) {
if (!all(col_names %in% field_names[field_required])) {
cli::cli_abort(
c(
"Field names in {.arg schema} must match column names in {.arg data}.",
Expand Down

0 comments on commit 677a953

Please sign in to comment.