
Create a Validator Object
Validator.RdAn S7 class that validates R objects against a Schema.
The Validator takes a schema (an object of class Schema or a list
that can be converted to a Schema) and validates input data against
the rules defined in the schema.
Arguments
- data
non-empty
list,data.frame, oratomicvector to be validated against theschema.- schema
Schema object or a non-empty
listdefining the schema. See the Schema constructor for details on the schema structure and rules.- error
single logical value. If
TRUE, an error is thrown when the validation fails.- x
object to be tested.
Value
An S7 Validator object with the following properties:
dataThe validated input data, with any changes that were made during validation.
SchemaAn object of class Schema. This also contains the Registry used for validation, which can be accessed via
@Schema@Registry.errors(Read only) A list mirroring the data or schema structure (depending on
match_schemaandmatch_data), withNULLfor valid fields and character error messages for invalid ones. Extra fields showing missigness may be present when bothmatch_schemaandmatch_dataareTRUE..validator_cache(Internal, read only) An environment for caching validation results.
errorBoolean; whether to error upon failure (invalid data or schema).
valid(Read only) Boolean;
TRUEif schema is valid and all data validation rules pass,FALSEotherwise (invalid data or schema).
Details
The validation process checks each field in the schema against the data,
creating an errors list that mirrors the schema structure with NULL for
valid fields and character error messages for invalid fields.
These validation results are stored in the @errors property, and
the overall validity is indicated by the @valid property. If the
@error property/argument is set to TRUE, any validation failure will
result in an error being thrown. To customise the truncation of error
messages, see the @Schema@error_print_opts property.
The Validator is re-evaluated upon any change to the object properties.
If an invalid schema is provided, the validation will fail immediately with
the validation error indicating that the schema is invalid. To see the
errors in the schema validation, see the Validator@Schema@errors property.
See the Schema class for details on the schema class structure, and the Registry class for details on the available validation rules.
For full details see the validating data vignette.
See also
add_rule for adding rules to a registry.
Examples
v <- Validator(
data = list(name = "Alice", age = 30),
schema = list(
name = list(type = "character", required = TRUE),
age = list(type = "numeric", min_val = 0, max_val = 150)
)
)
v@valid # TRUE
#> [1] TRUE
# Schema object can be given directly
s <- Schema(list(a = list(type = "numeric"), b = list(type = "character")))
v <- Validator(list("Hello", 42), s)
v@valid # FALSE
#> [1] FALSE
v@errors
#> $a
#> $a$type
#> [1] "No data for field."
#>
#>
#> $b
#> $b$type
#> [1] "No data for field."
#>
#>
# To error on invalid schema or data
try(Validator(list("Hello", 42), s, error = TRUE))
#> Error : <RV::Validator> object is invalid:
#> - Data validation failed with the following errors:
#> ├─ a
#> │ └─ type: No data for field.
#> └─ b
#> └─ type: No data for field.
# Invalid schemas show their errors
try(Validator(list(42), list(type = 123), error = TRUE))
#> Error : <RV::Validator> object is invalid:
#> - Schema validation failed with the following errors:
#> └─ type: Must be a function or a string.