valid: Validate objects within constructor functions

validR Documentation

Validate objects within constructor functions

Description

valid() is a generic intended to validate objects from a class within the constructor function for that class. Typical usage should be of the form:

constructor <- function (x, ...) {
  out <- structure(x, class = 'className')
  valid(out)
  out
}

Usage

valid(x, ...)

Arguments

x

An object of the class to be validated.

Details

valid is a generic function with no default. For it to work you need to write a method for the class, usually as part of the class definition. The valid.className method should return TRUE if all conditions are met, and halt with informative error messages otherwise:

valid.className <- fuction (x) {
  conditions <- with(x, c(
    given == expected,
     .
     .
     .
    )
  )
  if(all(conditions)) return(TRUE)
  stop('Informative error message')
}

Value

TRUE if successful.


rmvegasm/schron documentation built on June 3, 2022, 7:14 a.m.