v_enum() returns a validator function that checks if a value is a single character value matching one of a predefined set of choices. This is useful for options that must be one of several allowed values.

v_enum(choices)

Arguments

choices

A character vector of allowed values.

Value

A validator function that takes a value x and raises an error if:

  • x is not a single character value

  • x is not in the predefined choices

Examples

# Create a validator for one of several color choices
validator <- v_enum(choices = c("red", "green", "blue"))

# Valid inputs
validator("red")
validator("blue")

# Invalid inputs (would raise errors)
try(validator("yellow"))  # not in choices
#> Error : must be one of: red, green, blue
try(validator(c("red", "blue")))  # vector, not scalar
#> Error : must be a single character value
try(validator(1))  # numeric, not character
#> Error : must be a single character value