Skip to contents

Marks a validator as accepting NULL: a NULL value passes without running any step, and every other value is validated by all steps as usual. Use it for optional arguments such as weights = NULL.

Usage

allow_null(restriction)

Arguments

restriction

a restriction object.

Value

The modified restriction object.

Details

The marker is order-independent: it can sit anywhere in the pipe. A NULL value returns before the context check, so context a step needs (e.g. data in require_length_matches(~ nrow(data))) is not required when the value is NULL. A validator that allows NULL also accepts a NULL (or absent) element when it is lifted with require_col(), require_each(), require_fields() or require_valid().

Examples

weights_v <- restrict("weights") |>
  require_numeric(no_na = TRUE) |>
  require_positive() |>
  allow_null()
weights_v(NULL)         # passes
weights_v(c(1, 2))      # passes
is_valid(weights_v, -1) # FALSE
#> [1] FALSE