Skip to contents

Validates that all elements of a value fall within a specified range. The value can be numeric, Date, POSIXct, difftime or an ordered factor.

Usage

require_between(
  restriction,
  lower = -Inf,
  upper = Inf,
  exclusive_lower = FALSE,
  exclusive_upper = FALSE
)

Arguments

restriction

a restriction object.

lower

lower bound (default -Inf, no lower bound).

upper

upper bound (default Inf, no upper bound).

exclusive_lower

logical; if TRUE, lower bound is exclusive.

exclusive_upper

logical; if TRUE, upper bound is exclusive.

Value

The modified restriction object.

Details

The value and the bounds must be the same kind: a Date bound against a numeric value fails with a type error. Ordered-factor bounds are single values of a factor with the same levels as the validated value. Other input fails with a type error. NA elements are skipped; chain require_no_na() to reject them.

Examples

period <- restrict("start") |>
  require_between(as.Date("2020-01-01"), as.Date("2020-12-31"))
period(as.Date("2020-06-15"))
try(period(as.Date("2021-01-01")))
#> Error : start: must be in [2020-01-01, 2020-12-31]
#>   Found: 2021-01-01