Skip to contents

assert_length() checks that the number of characters in an output field is within min and max. Like the other assert_*() helpers, it is shorthand for assert_output() (or suggest_output()) with a ready-made condition.

Usage

assert_length(
  field = NULL,
  min = NULL,
  max = NULL,
  type = c("assert", "suggest")
)

Arguments

field

The output field to check. With NULL, the whole output is checked, which suits outputs with a single field. A missing field fails the check.

min, max

Inclusive bounds on the number of characters. Give at least one.

type

"assert" (the default) for a hard assertion, which makes with_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

assert_length("answer", max = 100)
#> 
#> ── Hard Assertion 
#> • Field: any field
#> • Message: "answer: Length must be at most 100 characters"
assert_length("summary", min = 10, max = 200, type = "suggest")
#> 
#> ── Soft Suggestion 
#> • Field: any field
#> • Message: "summary: Length must be between 10 and 200 characters"