egnha / valaddin

Functional input validation to make R functions more readable and robust
Other
33 stars 1 forks source link

Admit custom condition for input validation errors #25

Closed egnha closed 7 years ago

egnha commented 7 years ago

If an input validation error occurs, firmly signals a simpleError. For more discriminating error handling downstream, firmly should be allowed to signal other classes of errors. In other words, firmly should have a character-vector parameter .error_class that gives the subclass of the condition, inheriting from c("error", "condition") (default value "simpleError"), that is to be signaled by an input validation error.

New signature should be

firmly(.f, ..., .checklist = list(),
       .warn_missing = character(), .error_class = character())
egnha commented 7 years ago

And update %secure%, accordingly:

`%secure%` <- function(args, f) {
  nms <- names(args) %||% character(length(args))
  firmly(
    f,
    .checklist    = args[!nms %in% c(".warn_missing", ".error_class")],
    .warn_missing = args[[".warn_missing"]] %||% character(),
    .error_class  = args[[".error_class"]] %||% character()
  )
}

(No need for %secure% to check types, since firmly() will do that for itself.)