Skip to content

Commit

Permalink
Merge pull request #845 from mhines-usgs/master
Browse files Browse the repository at this point in the history
handle check for data.frame differently so as not to falsely flag the…
  • Loading branch information
mhines-usgs committed Feb 25, 2019
2 parents a768b3c + d76470e commit 6617b42
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions R/utils-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ checkRequiredFields <- function(data, requiredFields){
#' @param stopEmpty (optional - default = TRUE) whether or not the function should
#' throw an error if the data is present but empty.
validateFetchedData <- function(data, name, requiredFields, stopNull=TRUE, stopMissing=TRUE, stopEmpty=TRUE){

#If data not found, error
if(is.null(data)){
if(!stopNull){
Expand All @@ -186,31 +187,31 @@ validateFetchedData <- function(data, name, requiredFields, stopNull=TRUE, stopM
stop(paste("Data for: '", name, "' was not found in report JSON."))
}
}

#Check for required fields
if(!isEmptyOrBlank(data) && !isEmptyOrBlank(requiredFields)){
missingFields <- checkRequiredFields(data, requiredFields)

if(!isEmptyOrBlank(missingFields)){
if(!stopMissing){
warning(paste("Data retrieved for: '", name, "' is missing required fields: {", paste(missingFields, collapse=', '), "}."))

#Check for required fields
if((length(data)>0 || !isEmptyOrBlank(data)) && !isEmptyOrBlank(requiredFields)){
missingFields <- checkRequiredFields(data, requiredFields)

if(!isEmptyOrBlank(missingFields)){
if(!stopMissing){
warning(paste("Data retrieved for: '", name, "' is missing required fields: {", paste(missingFields, collapse=', '), "}."))
return(FALSE)
} else {
stop(paste("Data retrieved for: '", name, "' is missing required fields: {", paste(missingFields, collapse=', '), "}."))
}
}
}

#Check for valid but empty data
if((class(data) != "list" && length(data)==0 && isEmptyOrBlank(data)) || (class(data) == "list" && length(data) == 0)){
if(!stopEmpty){
warning(paste("Data was retrieved for: '", name, "' but it is empty."))
return(FALSE)
} else {
stop(paste("Data retrieved for: '", name, "' is missing required fields: {", paste(missingFields, collapse=', '), "}."))
stop(paste("Data was retrieved for: '", name, "' but it is empty."))
}
}
}

#Check for valid but empty data
if((class(data) != "list" && isEmptyOrBlank(data)) || (class(data) == "list" && length(data) == 0)){
if(!stopEmpty){
warning(paste("Data was retrieved for: '", name, "' but it is empty."))
return(FALSE)
} else {
stop(paste("Data was retrieved for: '", name, "' but it is empty."))
}
}


return(TRUE)
}

Expand Down

0 comments on commit 6617b42

Please sign in to comment.