diff --git a/R/process_directory.R b/R/process_directory.R index 118c0d5..d01b9dd 100644 --- a/R/process_directory.R +++ b/R/process_directory.R @@ -24,20 +24,7 @@ process_directory <- function( logger::log_info("Preparing metadata JSON file.") if (validate) { logger::log_info("Validating output.") - sch <- jsonvalidate::json_schema[["new"]]( - schema = schema_file, - strict = TRUE, - engine = "ajv" - ) - summaries_json <- jsonlite::prettify( - sch[["serialise"]](all_summaries) - ) - json_is_valid <- sch[["validate"]](summaries_json, verbose = TRUE) - if (json_is_valid) { - logger::log_debug("JSON is valid.") - } else { - logger::log_error("JSON is not valid.") - } + summaries_json <- schema_serialize(all_summaries) } else { logger::log_warn("Skipping JSON validation.") summaries_json <- jsonlite::toJSON( diff --git a/R/schema_serialize.R b/R/schema_serialize.R new file mode 100644 index 0000000..0b450d6 --- /dev/null +++ b/R/schema_serialize.R @@ -0,0 +1,38 @@ +schema_serialize <- function( + object, + schema_file = system.file( + "extdata", "schema", "metadata.json", + package = "workflow.portfolio.parsing" + ), + reference = NULL +) { + sch <- jsonvalidate::json_schema[["new"]]( + schema = readLines(schema_file), + strict = TRUE, + engine = "ajv", + reference = reference + ) + json <- sch[["serialise"]](object) + json_is_valid <- sch[["validate"]](json, verbose = TRUE) + if (json_is_valid) { + logger::log_trace("JSON is valid.") + } else { + json_errors <- attributes(json_is_valid)[["errors"]] + logger::log_warn( + "Portfolio metadata could not be validated against ", + "JSON schema: \"", schema_file, "\"." + ) + logger::log_trace( + logger::skip_formatter(paste("JSON string: ", json)) + ) + logger::log_trace("Validation errors:") + for (i in seq(from = 1L, to = nrow(json_errors), by = 1L)) { + logger::log_trace( + "instancePath: ", json_errors[i, "instancePath"], + " message: ", json_errors[i, "message"] + ) + } + warning("Portfolio metadata could not be validated against schema.") + } + return(json) +}