This function validates a value. Before validation, it checks the validation expression and optionally performs replacements, where the replacement strings (delimited by the validation replacement delimiters, by default, {{ and }}) are replaced by the first valid corresponding value from the replacementSources (working through those sources consecutively, i.e. only looking in the second one of the first one doesn't contain a valid value for the relevant replacement string, valid being defined as a value that is not NULL or NA and has, after being trimmed of whitespace, a nonzero length).

validate_value(
  VALUE,
  validations,
  replacementSources,
  errorMessages,
  convert_bipiped = TRUE
)

Arguments

VALUE

The value to validate.

validations

The validations, a vector or list that will be consecutively searched for the first valid value (valid being defined as a value that is not NULL or NA and has, after being trimmed of whitespace, a nonzero length). That value has to be either as an R expression, or a character value (i.e. a length 1 character vector) that is a valid R expression, optionally after having performed the specified replacements.

replacementSources

A list of named lists (or 1-row data frames) that will be searched, consecutively, for values to replace the replacement strings with.

errorMessages

The errormessage to return if validation fails, of which the first valid one will be returned (valid being defined as a value that is not NULL or NA and has, after being trimmed of whitespace, a nonzero length).

convert_bipiped

Whether to first run bipiped_value_to_vector() on the replacment values.

Value

The message resulting from the validation (i.e. an error or "").

Details

To change the validation replacement delimiters, use preregr::opts$set(validation_replacementDelimiters = c("{{", "}}"));.

Examples

### Set some validation variables
validationStatement <-
  paste(
    "is.na(VALUE) ||",
    "(VALUE %in% {{validValues}}) ||",
    "(VALUE %in% {{testField}})"
  );
replacementSources <-
  list(
    list(validValues = '"testValue" || "anotherValue"'),
    list(testField = "Yet another testvalue")
  );
errorMessages <-
  "No valid test value passed!";

### Run a passing validation
preregr::validate_value(
  "testValue",
  validations = validationStatement,
  replacementSources = replacementSources,
  errorMessages = errorMessages
);
#> [1] ""


### Run a failing validation
preregr::validate_value(
  "A testvalue that won't pass",
  validations = validationStatement,
  replacementSources = replacementSources,
  errorMessages = errorMessages
);
#> [1] "No valid test value passed!"