From a4772c8dc7109bd4b342eeff72ff13bef7f32aef Mon Sep 17 00:00:00 2001 From: afwillia Date: Mon, 19 Aug 2024 09:26:19 -0700 Subject: [PATCH 1/3] Add a function to handle schematic API validation output --- R/utils.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/utils.R b/R/utils.R index 4ea8a93e..2975253e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -11,4 +11,18 @@ parse_env_var <- function(x, el_delim=",", kv_delim=":"){ kv <- stringr::str_split(y, kv_delim, n=2) setNames(kv[[1]][[2]], kv[[1]][[1]]) })) +} + +#' @title Truncate the results of schematic validation +#' @param x Schematic validation result +#' @param n Number of records to keep +#' @export +format_validation_response <- function(x, n = 50) { + if ("errors" %in% names(x) && length(x$errors) > n) { + x$errors <- x$errors[1:n] + } + if ("warnings" %in% names(x) && length(x$warnings) > n) { + x$warnings <- x$warnings[1:n] + } + x } \ No newline at end of file From 32cac79ac2b62c04c9edf8fe9eb0b14c38b67887 Mon Sep 17 00:00:00 2001 From: afwillia Date: Mon, 19 Aug 2024 09:27:20 -0700 Subject: [PATCH 2/3] Update schematic validation API call to return the response body of an error --- R/schematic_rest_api.R | 9 ++++++++- functions/schematic_rest_api.R | 36 ++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/R/schematic_rest_api.R b/R/schematic_rest_api.R index d2a574fb..fad8baed 100644 --- a/R/schematic_rest_api.R +++ b/R/schematic_rest_api.R @@ -216,7 +216,14 @@ manifest_validate <- function(url="http://localhost:3001/v1/model/validate", # ) # ) # } - httr2::resp_body_json(resp) + if (httr2::resp_is_error(resp)) { + list(list( + "errors" = list( + Row = NA, Column = NA, Value = NA, + Error = httr2::resp_body_string(resp) + ) + )) + } else httr2::resp_body_json(resp) } diff --git a/functions/schematic_rest_api.R b/functions/schematic_rest_api.R index 1e1546c2..fad8baed 100644 --- a/functions/schematic_rest_api.R +++ b/functions/schematic_rest_api.R @@ -6,6 +6,11 @@ check_success <- function(x){ if (tolower(status$category) == "success") { return() } else { + # Return content text for Data Type errors + if (grepl("LookupError: The DataType", httr::content(x, "text"))) { + stop(httr::content(x, "text")) + } + stop(sprintf("Response from server: %s", status$reason)) } } @@ -211,7 +216,14 @@ manifest_validate <- function(url="http://localhost:3001/v1/model/validate", # ) # ) # } - httr2::resp_body_json(resp) + if (httr2::resp_is_error(resp)) { + list(list( + "errors" = list( + Row = NA, Column = NA, Value = NA, + Error = httr2::resp_body_string(resp) + ) + )) + } else httr2::resp_body_json(resp) } @@ -221,8 +233,18 @@ manifest_validate <- function(url="http://localhost:3001/v1/model/validate", #' @param schema_url URL to a schema jsonld #' @param data_type Type of dataset #' @param dataset_id Synapse ID of existing manifest -#' @param access_token Synapse login cookie, PAT, or API key. -#' @param csv_file Filepath of csv to validate +#' @param restrict_rules Default = FALSE +#' @param access_token Synapse login cookie, PAT, or API key +#' @param json_str Json string to submit +#' @param asset_view Synapse fileview +#' @param manifest_record_type Default = "table_and_file" +#' @param file_name Name of file +#' @param table_manipulation Default = "replace" +#' @param hide_blanks Default = FALSE +#' @param table_column_names Default = "class_and_label" +#' @param annotation_keys Default = "class_and_label" +#' @param data_model_labels Default = "class_and_label" +#' @param upload_file_annotations Default = TRUE #' #' @returns TRUE if successful upload or validate errors if not. #' @export @@ -240,7 +262,8 @@ model_submit <- function(url="http://localhost:3001/v1/model/submit", hide_blanks=FALSE, table_column_names="class_label", annotation_keys="class_label", - data_model_labels="class_label") { + data_model_labels="class_label", + file_annotations_upload=TRUE) { req <- httr::POST(url, httr::add_headers(Authorization = sprintf("Bearer %s", access_token)), query=list( @@ -255,7 +278,8 @@ model_submit <- function(url="http://localhost:3001/v1/model/submit", table_column_names=table_column_names, annotation_keys=annotation_keys, data_model_labels=data_model_labels, - hide_blanks=hide_blanks), + hide_blanks=hide_blanks, + file_annotations_upload=file_annotations_upload), body=list(file_name=httr::upload_file(file_name)) #body=list(file_name=file_name) ) @@ -397,7 +421,7 @@ get_asset_view_table <- function(url="http://localhost:3001/v1/storage/assets/ta check_success(req) if (return_type=="json") { - return(list2DF(fromJSON(httr::content(req)))) + return(list2DF(jsonlite::fromJSON(httr::content(req)))) } else { csv <- readr::read_csv(httr::content(req), show_col_types = FALSE) return(csv) From f3c83227ba06f647a8fb479c3f9ecdf8f5093121 Mon Sep 17 00:00:00 2001 From: afwillia Date: Mon, 19 Aug 2024 09:27:39 -0700 Subject: [PATCH 3/3] Only show the first 50 validation errors --- server.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server.R b/server.R index f9e418ab..5cb15766 100644 --- a/server.R +++ b/server.R @@ -846,6 +846,7 @@ shinyServer(function(input, output, session) { ) # validation messages + annotation_status <- format_validation_response(annotation_status, 50) validationResult(annotation_status, .dd_template, .infile_data) }) %...>% validation_res() }) @@ -1067,7 +1068,7 @@ shinyServer(function(input, output, session) { .restrict_rules <- dcc_config_react()$schematic$model_validate$restrict_rules .hide_blanks <- dcc_config_react()$schematic$model_submit$hide_blanks .file_annotations_upload <- dcc_config_react()$schematic$model_submit$file_annotations_upload - + # associates metadata with data and returns manifest id promises::future_promise({ try(