Skip to contents

assert_matches() checks that an output field matches a Perl-compatible regular expression.

Usage

assert_matches(
  field = NULL,
  pattern,
  message = NULL,
  ignore_case = FALSE,
  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.

pattern

A regular expression (Perl syntax).

message

The message shown when the check fails, which with_assertions() also sends back to the model on a retry. With NULL, a message naming the field and pattern.

ignore_case

If TRUE, ignore case when matching.

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_matches("answer", "^[A-Z]", "Start with a capital letter")
#> 
#> ── Hard Assertion 
#> • Field: any field
#> • Message: "Start with a capital letter"
assert_matches("email", "^[^@]+@[^@]+\\.[^@]+$", "Return a valid email address")
#> 
#> ── Hard Assertion 
#> • Field: any field
#> • Message: "Return a valid email address"