Builds a rule for validate_rules(): a
predicate over one column, or over the whole data frame, with a severity
and a message. Rules live alongside a schema in a provenance record,
which keeps the project-specific checks in data rather than scattered
through code.
Usage
rule(
name,
expr,
column = NULL,
severity = c("warning", "fatal"),
message = NULL
)Details
expr is a function. Given column, it receives that column
and must return a logical vector the same length (TRUE = the row passes)
or a single logical for a whole-column property. Given no column,
it receives the whole data frame and must return a single logical.
See also
validate_rules(),
validate_schema() for the structural
checks that need no rules.
Examples
# A column predicate, applied row-wise.
rule("age_non_negative", function(v) v >= 0, column = "age")
#> <rule> age_non_negative [warning] on `age`
# A whole-column property.
rule("id_unique", function(v) !anyDuplicated(v), column = "id",
severity = "fatal")
#> <rule> id_unique [fatal] on `id`
# A table-level rule spanning two columns.
rule("dates_ordered", function(df) all(df$start <= df$end))
#> <rule> dates_ordered [warning] table-level