Skip to content

Commit aca2dcf

Browse files
committed
Add a helper function to validate attributes
1 parent 8455c98 commit aca2dcf

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export(eml_creator)
2525
export(eml_individual_name)
2626
export(eml_metadata_provider)
2727
export(eml_project)
28+
export(eml_validate_attributes)
2829
export(env_get)
2930
export(env_load)
3031
export(filter_obsolete_pids)

R/eml.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,79 @@ set_abstract <- function(doc, text) {
662662
}
663663

664664

665+
#' Validate an EML attributeList attribute-by-attribute
666+
#'
667+
#' The attributes passed into this function are validated one-by-one and the
668+
#' progress of going through each attribute is printed to the screen along
669+
#' with any and all validation issues.
670+
#'
671+
#' This is done by, for each attribute in the list, creating a minimum valid
672+
#' EML document and adding a new otherEntity with a new attributeList containing
673+
#' the single attribute to be validated.
674+
#'
675+
#' @param attributes (attributeList) An attributeList
676+
#'
677+
#' @return (boolean) Named vector of TRUE/FALSE indicating which attributes
678+
#' are valid
679+
#' @export
680+
#'
681+
#' @examples
682+
#' \dontrun{
683+
#' atts_df <- read.csv('attributes_table.csv', stringsAsFactors = F)
684+
#' enum_domain <- read.csv('enumerated_domain.csv') # optional
685+
#' attributes <- EML::set_attributes(atts_df, factor = enum_domain)
686+
#' eml_validate_attributes(attributes)
687+
#' }
688+
eml_validate_attributes <- function(attributes) {
689+
stopifnot(is(attributes, "attributeList"))
690+
691+
# Define an interal applyable function to validate each attribute
692+
eml_validate_attribute <- function(attribute) {
693+
stopifnot(is(attribute, "attribute"))
694+
695+
doc@dataset@otherEntity[[1]]@attributeList@attribute[[1]] <- attribute
696+
697+
# Validate!
698+
eml_validate(doc)
699+
}
700+
701+
702+
# Create a minimum valid EML doc we'll re-use each time we validate a single
703+
# attribute
704+
doc <- new("eml", packageId = "test", system = " test")
705+
doc@dataset@title <- c(new("title", .Data = "test"))
706+
doc@dataset@creator <- new("ListOfcreator", list(eml_creator("Test", "test")))
707+
doc@dataset@contact <- new("ListOfcontact", list(eml_contact("Test", "test")))
708+
709+
# Create a dummy otherEntity with our attributeList
710+
entity <- new("otherEntity",
711+
entityName = "name",
712+
entityType = "type")
713+
entity@attributeList <- new("attributeList")
714+
doc@dataset@otherEntity <- new("ListOfotherEntity", list(entity))
715+
716+
results <- sapply(attributes@attribute, function(attribute) {
717+
cat(paste0("Validating single attribute '", attribute@attributeName@.Data, "': "))
718+
719+
result <- NULL
720+
result <- tryCatch({
721+
eml_validate_attribute(attribute)
722+
},
723+
message = function(m) { m }
724+
)
725+
726+
if (is(result, "simpleMessage")) {
727+
cat("FALSE\n")
728+
message(trimws(result$message))
729+
return(FALSE)
730+
} else {
731+
cat("TRUE\n")
732+
return(TRUE)
733+
}
734+
})
735+
736+
names(results) <- sapply(attributes@attribute, function(x) x@attributeName)
737+
738+
results
739+
}
740+

man/eml_validate_attributes.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)