Skip to contents

RV has 30 validation rules and 10 cross rules.

Validation rules

Registry()@rule_names
#>  [1] "required"       "default"        "coerce"         "apply"         
#>  [5] "type"           "inherits"       "allowed"        "forbidden"     
#>  [9] "unique"         "positive"       "negative"       "finite"        
#> [13] "allow_na"       "min_val"        "max_val"        "min_length"    
#> [17] "max_length"     "min_nrow"       "max_nrow"       "min_nchar"     
#> [21] "max_nchar"      "nzchar"         "regex"          "labelled"      
#> [25] "levels"         "ordered_levels" "dependency"     "dependencies"  
#> [29] "predicate"      "apply_last"

The builtin rules are categorised by their type: ‘control’, ‘transform’ and ‘validate’. When the Validator is run, rules are applied in three passes according to these categories, with a special final pass for the ‘apply_last’ rule.

Rules within each category are applied in the order they appear in their respective Registry property.

r <- Registry()
r@control_rules
#> [1] "required" "default"
r@transform_rules
#> [1] "coerce" "apply"
r@validate_rules
#>  [1] "type"           "inherits"       "allowed"        "forbidden"     
#>  [5] "unique"         "positive"       "negative"       "finite"        
#>  [9] "allow_na"       "min_val"        "max_val"        "min_length"    
#> [13] "max_length"     "min_nrow"       "max_nrow"       "min_nchar"     
#> [17] "max_nchar"      "nzchar"         "regex"          "labelled"      
#> [21] "levels"         "ordered_levels" "dependency"     "dependencies"  
#> [25] "predicate"

Each validation rule has two functions associated with it:

  • A function that validates the given schema value.
Schema(list(type = 1L), error = TRUE)
#> Error:
#> ! <RV::Schema> object is invalid:
#> - Schema validation failed with the following errors:
#> └─ type: Must be a function or a string.
  • A function that uses the schema value to validate data.
Validator(
  data = 1L,
  schema = list(type = "character"),
  error = TRUE
)
#> Error:
#> ! <RV::Validator> object is invalid:
#> - Data validation failed with the following errors:
#> └─ type: Is not type `character`.

Cross rules

Cross rules operate within the Schema, checking that the values of two or more schema rules don’t clash. They are not exhaustive, but apply to a number of common scenarios.

Schema(
  list(
    min_length = 5,
    max_length = 1
  ),
  error = TRUE
)
#> Error:
#> ! <RV::Schema> object is invalid:
#> - Schema validation failed with the following errors:
#> ├─ min_length: `min_length` must be smaller than `max_length`.
#> └─ max_length: `min_length` must be smaller than `max_length`.

Rule information

Validation rules

Rule
Schema operation
Data operation
Control flow
Checks schema value is: Checks/transforms data element: Stops other rules if:
required boolean. exists. FALSE and element not present.
default non-empty. exists and inserts default if not. default is used.
apply function or a valid string. applies function.
coerce 1 arg function or a valid string. coerces.
type 1 arg function or a valid string. type.
inherits character vector. inherits from specified classes.
allowed non-empty vector. only values in allowed set.
forbidden non-empty vector. no values in forbidden set.
unique TRUE. no duplicates.
positive TRUE. is positive (or zero).
negative TRUE. is negative (or zero).
finite TRUE. is finite.
allow_na TRUE. no NA values.
min_val finite numeric value. values at least min_val.
max_val finite numeric value. values at most max_val.
min_length positive integerish value. length at least min_length.
max_length positive integerish value. length at most max_length.
min_nrow positive integerish value. nrow at least min_nrow.
max_nrow positive integerish value. nrow at most max_nrow.
min_nchar positive integerish value. nchar at least min_nchar.
max_nchar positive integerish value. nchar at most max_nchar.
nzchar boolean. no empty strings.
regex string. matches regex pattern.
labelled boolean. is labelled (has labels attribute).
levels character vector. has levels matching levels in any order.
ordered_levels character vector. has levels matching ordered_levels in order.
dependency character vector, or integerish vector, or list of string/integerish scalars. dependency field present.
dependencies list of character vectors, or integerish vectors, or lists of string/integerish scalars. dependency fields present.
predicate function or a valid string. satisfies predicate function.
apply_last function or a valid string. applies function in no errors in node.

Cross rules

Cross rule
Schema operation
Checks in a schema node that:
dependency_and_dependencies dependency and dependencies rules aren’t both present.
required_and_default if required is TRUE that a default value is not provided.
positive_and_negative positive and negative rules aren’t both present.
min_val_larger_than_max_val min_val is smaller than max_val.
min_length_larger_than_max_length min_length is smaller than max_length.
min_nrow_larger_than_max_nrow min_nrow is smaller than max_nrow.
min_nchar_larger_than_max_nchar min_nchar is smaller than max_nchar.
allowed_and_forbidden_overlap values in allowed and forbidden do not overlap.
allowed_type_mismatch values in allowed are of the type specified in type.
forbidden_type_mismatch values in forbidden are of the type specified in type.
Note

To quickly see the builtin rules at the terminal, use show_RV_builtins().