-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): #13 factor validation and serialization to function
allows for reuse in other functions
- Loading branch information
1 parent
fc8e4e2
commit 000ce0f
Showing
2 changed files
with
39 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |