Skip to contents

assert_one_of() checks that an output field equals one of values.

Usage

assert_one_of(
  field = NULL,
  values,
  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.

values

A character vector of allowed values.

ignore_case

If TRUE, ignore case when comparing.

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_one_of("sentiment", c("positive", "negative", "neutral"))
#> 
#> ── Hard Assertion 
#> • Field: any field
#> • Message: "sentiment must be one of: positive, negative, neutral"
assert_one_of("grade", c("A", "B", "C"), ignore_case = TRUE)
#> 
#> ── Hard Assertion 
#> • Field: any field
#> • Message: "grade must be one of: A, B, C"