-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation improvements #149
Changes from 1 commit
5b99ad5
f159ca0
a2ab6b7
f2b2957
28c4941
2105da1
268e9d8
b000121
2bd44d5
f36abf1
f1afc91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
#' Validate an R7 object | ||
#' | ||
#' @description | ||
#' `validate()` calls the validator of an R7 object. This is done automatically | ||
#' when creating new objects (at the end of [new_object()]) and when setting | ||
#' any property with [prop<-]. | ||
#' `validate()` ensures that an R7 object is valid by calling the `validator` | ||
#' provided in [new_class()]. This is done automatically when constructing new | ||
#' objects and when modifying properties. | ||
#' | ||
#' `valid_eventually()` disables validation, modifies the object, then | ||
#' revalidates. This is useful when a sequence of operations would otherwise | ||
#' lead an object to be temporarily invalid. | ||
#' lead an object to be temporarily invalid, or when repeated property | ||
#' modification causes a performance bottleneck because the validator is | ||
#' relatively expensive. | ||
#' | ||
#' `valid_implicitly()` does the same but does not validate the object at the | ||
#' end. It should only be used rarely, and in performance critical code where | ||
|
@@ -68,22 +70,24 @@ validate <- function(object, properties = TRUE) { | |
# is likely to return spurious errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the errors in the object validator are likely to be spurious, should we just go ahead and error immediately if any properties are invalid? Maybe it could have a header of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see you've tried to do something like this by avoiding the object validator with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good idea. |
||
if (properties) { | ||
errors <- validate_properties(object, class) | ||
} else { | ||
errors <- character() | ||
if (length(errors) > 0) { | ||
bullets <- paste0("- ", errors, collapse = "\n") | ||
msg <- sprintf("%s object properties are invalid:\n%s", obj_desc(object), bullets) | ||
stop(msg, call. = FALSE) | ||
} | ||
} | ||
|
||
# Next, recursively validate the object | ||
if (length(errors) == 0) { | ||
while(!is.null(class) && is_class(class)) { | ||
errors <- c(errors, class@validator(object)) | ||
class <- prop_safely(class, "parent") | ||
} | ||
errors <- character() | ||
while(!is.null(class) && is_class(class)) { | ||
errors <- c(errors, class@validator(object)) | ||
class <- prop_safely(class, "parent") | ||
} | ||
|
||
# If needed, report errors | ||
if (length(errors) > 0) { | ||
bullets <- paste0("- ", errors, collapse = "\n") | ||
msg <- sprintf("Invalid %s object:\n%s", obj_desc(object), bullets) | ||
msg <- sprintf("%s object is invalid:\n%s", obj_desc(object), bullets) | ||
stop(msg, call. = FALSE) | ||
} | ||
|
||
|
@@ -128,5 +132,6 @@ valid_implicitly <- function(object, fun) { | |
attr(object, ".should_validate") <- FALSE | ||
out <- fun(object) | ||
attr(out, ".should_validate") <- old | ||
invisible(out) | ||
|
||
out | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo here, validator->valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thanks.