validate() is a variant of assert() that is specifically intended to be used inside of an S4 validity method definition.

validate(..., msg = NULL)



Any number of R expressions that return logical(1), each of which should evaluate to TRUE. Rather than combining expressions with &&, separate them by commas so that better error messages can be generated.


NULL or character(1). Custom message to return.


TRUE on success, or character(1) on failure, containing an error message.


Like assert(), validate() returns TRUE on success. However, on failure it returns a character instead of a stop() call. This is the current recommended practice for defining S4 validity methods inside of a setValidity() call. Refer to the documentation in the methods package, specifically validObject() for detailed information on S4 validity methods.


Updated 2021-08-19.

See also


## TRUE ==== validate( is.atomic("example"), is.character("example") )
#> [1] TRUE
## FALSE ==== validate( isFlag("xxx"), isPositive(-1) )
#> [1] "[1] isFlag(\"xxx\") is not TRUE.\nCause: '\"xxx\"' is not a boolean flag (TRUE/FALSE).\n[2] isPositive(-1) is not TRUE.\nCause: -1.000000000000000e+00: too low\nIf supported, 'updateObject()' may help resolve these issues."