assert_custom() builds an assertion from your own condition. It is
assert_output() or suggest_output(), chosen by type.
Usage
assert_custom(condition, message, field = NULL, type = c("assert", "suggest"))Arguments
- condition
A function, or a formula using
.x, that takes the output (or thefield) and returnsTRUEorFALSE.- message
The message shown when the check fails, which
with_assertions()also sends back to the model on a retry.- field
The output field passed to
condition. WithNULL(the default), the whole output (a named list) is passed.- type
"assert"(the default) for a hard assertion, which makeswith_assertions()retry when it fails, or"suggest"for a soft one, which only gives a warning.
Value
An assertion for assertion_set() or with_assertions().
Examples
# Exactly three sentences
assert_custom(
~ lengths(regmatches(.x$answer, gregexpr("[.!?]", .x$answer))) == 3,
"Answer in exactly three sentences"
)
#>
#> ── Hard Assertion
#> • Field: any field
#> • Message: "Answer in exactly three sentences"
# Compare two output fields
assert_custom(
function(x) nchar(x$summary) < nchar(x$details),
"The summary must be shorter than the details"
)
#>
#> ── Hard Assertion
#> • Field: any field
#> • Message: "The summary must be shorter than the details"