Skip to contents

Creates a callable validation object that accumulates checks via the base pipe operator |>. The resulting object behaves like a function: call it with a value to validate.

Usage

restrict(name)

Arguments

name

character(1) name used in error messages (e.g. "newdata").

Value

A restriction object (callable function) with no validation steps.

Calling convention

Validators accept value as the first argument, plus context via named arguments in ... or as a named list in .ctx:

require_pred(out, newdata = df)
require_pred(out, .ctx = list(newdata = df))

Named arguments in ... take precedence over .ctx entries with the same name. If a step declares dependencies (e.g. require_length_matches(~ nrow(newdata))), the validator checks that all required context is present before running any steps and errors early if not.

By default a validator is fail-fast: the first failing step stops with its error. Pass .on_fail = "all" to run every step and report all violations in one aggregated error:

require_pred(out, .on_fail = "all")

For a non-throwing result, use is_valid() or validation_errors().

Examples

# Define a validator
require_positive <- restrict("x") |>
  require_numeric(no_na = TRUE) |>
  require_between(lower = 0, exclusive_lower = TRUE)

# Use it
require_positive(5)   # passes silently

# Compose with pipe
require_score <- restrict("score") |>
  require_numeric() |>
  require_length(1L) |>
  require_between(lower = 0, upper = 100)