@@ -17,15 +17,13 @@ succinct validation of function arguments with clear error messaging.
1717 validation using tidy evaluation.
1818- [ ` walk_check() ` ] ( https://lj-jenkins.github.io/favr/reference/walk-check.md )
1919 for applying a check to each element of a vector.
20-
21- Numerous other ` check_*() ` functions are provided for specific types of
22- validation, including:
23-
24- Validate class and inheritance:
25-
2620- [ ` check_class() ` ] ( https://lj-jenkins.github.io/favr/reference/inheritance-checks.md )
2721 and
28- [ ` check_inherits() ` ] ( https://lj-jenkins.github.io/favr/reference/inheritance-checks.md ) .
22+ [ ` check_inherits() ` ] ( https://lj-jenkins.github.io/favr/reference/inheritance-checks.md )
23+ for class validation.
24+
25+ Numerous other strongly typed ` check_*() ` functions are provided for
26+ specific types of validation, including:
2927
3028Validate specific types:
3129
@@ -55,10 +53,6 @@ Validate specific S3 types:
5553 and
5654 [ ` check_list_of() ` ] ( https://lj-jenkins.github.io/favr/reference/s3-type-checks.md )
5755 for their respective [ vctrs] ( https://vctrs.r-lib.org ) classes.
58- - [ ` s3_vec_check() ` ] ( https://lj-jenkins.github.io/favr/reference/s3-check-builders.md )
59- and
60- [ ` s3_df_check() ` ] ( https://lj-jenkins.github.io/favr/reference/s3-check-builders.md )
61- for developers to create their own S3 type checks.
6256
6357Modify the behaviour of type-checking functions:
6458
@@ -85,16 +79,34 @@ Validate the lack of forbidden values:
8579
8680- [ ` check_no_na() ` ] ( https://lj-jenkins.github.io/favr/reference/forbidden-value-checks.md ) ,
8781 [ ` check_finite() ` ] ( https://lj-jenkins.github.io/favr/reference/forbidden-value-checks.md ) ,
82+ [ ` check_unique() ` ] ( https://lj-jenkins.github.io/favr/reference/forbidden-value-checks.md )
8883 and
8984 [ ` check_nzchar() ` ] ( https://lj-jenkins.github.io/favr/reference/forbidden-value-checks.md ) .
9085
86+ Validate object properties:
87+
88+ - [ ` check_length() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md ) ,
89+ [ ` check_nrow() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md ) ,
90+ [ ` check_ncol() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md ) ,
91+ [ ` check_size() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md ) ,
92+ [ ` check_non_empty() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md )
93+ and
94+ [ ` check_named() ` ] ( https://lj-jenkins.github.io/favr/reference/property-checks.md ) .
95+
9196Validate file and directory existence:
9297
9398- [ ` check_dir() ` ] ( https://lj-jenkins.github.io/favr/reference/path-checks.md ) ,
9499 [ ` check_file() ` ] ( https://lj-jenkins.github.io/favr/reference/path-checks.md )
95100 and
96101 [ ` check_ext() ` ] ( https://lj-jenkins.github.io/favr/reference/path-checks.md ) .
97102
103+ Build checks in the style of favr:
104+
105+ - [ ` s3_vec_check() ` ] ( https://lj-jenkins.github.io/favr/reference/s3-check-builders.md )
106+ and
107+ [ ` s3_df_check() ` ] ( https://lj-jenkins.github.io/favr/reference/s3-check-builders.md )
108+ for developers to create their own S3 type checks.
109+
98110## Installation
99111
100112Install the latest version of favr from CRAN.
@@ -140,10 +152,10 @@ abortifnot(
140152abortifnot(
141153 is.numeric(x ),
142154 is.numeric(y ),
143- message = " {.var x} and {.var y} must be {.cls character }."
155+ message = " {.var x} and {.var y} must be {.cls numeric }."
144156)
145157# > Error:
146- # > ! `x` and `y` must be <character >.
158+ # > ! `x` and `y` must be <numeric >.
147159```
148160
149161General validation with tidy evaluation:
@@ -170,21 +182,21 @@ Data-masked validation:
170182
171183``` r
172184
173- data <- data.frame (a = 1 : 3 , b = c(" a" , " b" , " c" ))
185+ data <- list (a = c(" a" , " b" , " c" ), b = 1 : 3 )
174186
175187# `check_with()` user-supplied messages are eval'd in the data mask context.
176188check_with(data ,
177- " {.var a} must be length {.val 5}, but is length {.val {length(a)}}. " = length (a ) == 5 ,
178- " {.var b} must all have 2 nchars. " = nchar (b ) == 2
189+ " {.var a} must all have 1 nchars. " = nchar (a ) == 1 ,
190+ " {.var b} must be length {.val 5}, but is length {.val {length(b)}}. " = length (b ) == 5
179191)
180192# > Error:
181- # > ! `a ` must be length "5", but is length 3.
193+ # > ! `b ` must be length "5", but is length 3.
182194
183- a <- c(" a" , " b" , " c" )
195+ b <- c(" a" , " b" , " c" )
184196
185- check_with(data , is.numeric(.data $ a ), is.numeric(.env $ a ))
197+ check_with(data , is.numeric(.data $ b ), is.numeric(.env $ b ))
186198# > Error:
187- # > ! `is.numeric(.env$a )` is not TRUE.
199+ # > ! `is.numeric(.env$b )` is not TRUE.
188200```
189201
190202Walking a check over a vector:
@@ -197,6 +209,20 @@ walk_check(x, is.numeric)
197209# > ! Check result for `.x[['my_el']]` (index: 3) is not TRUE.
198210```
199211
212+ Class validation:
213+
214+ ``` r
215+
216+ x <- structure(1 : 3 , class = " a_class" )
217+ check_class(x , " my_class" )
218+ # > Error:
219+ # > ! `x` must be class <my_class>, but is class <a_class>.
220+ class(x ) <- c(" b_class" , class(x ))
221+ check_inherits(x , " my_class" )
222+ # > Error:
223+ # > ! `x` must inherit from <my_class>, but is class <b_class/a_class>.
224+ ```
225+
200226Specific type validation:
201227
202228``` r
@@ -261,13 +287,16 @@ Ensure no forbidden values:
261287
262288``` r
263289
264- x <- c(1 , 2 , NA )
290+ x <- c(1 , 2 , 1 , NA )
265291check_no_na(x )
266292# > Error:
267293# > ! `x` must not contain NA values.
268294check_finite(x )
269295# > Error:
270296# > ! `x` must not contain non-finite values.
297+ check_unique(x )
298+ # > Error:
299+ # > ! `x` must have unique elements. Duplicates: 1.
271300
272301x <- c(" a" , " b" , " " )
273302check_nzchar(x )
@@ -279,6 +308,42 @@ check_nzchar(x, allow_all_ws = FALSE)
279308# > ! `x` must not contain all whitespace elements.
280309```
281310
311+ Check object properties:
312+
313+ ``` r
314+
315+ x <- c(1 , 2 , 3 )
316+ check_length(x , 2 )
317+ # > Error:
318+ # > ! `x` must be of length 2, not 3.
319+ check_size(x , at_most(1 ))
320+ # > Error:
321+ # > ! `x` must be of at most size 1, but it is of size 3.
322+ df <- data.frame (x = 1 : 3 , y = 1 : 3 )
323+ check_nrow(df , 2 )
324+ # > Error:
325+ # > ! `df` must have 2 rows, not 3.
326+ check_ncol(df , in_range(3 , 5 ))
327+ # > Error:
328+ # > ! `df` must have 3 to 5 columns, but it has 2.
329+ x <- numeric (0 )
330+ check_non_empty(x )
331+ # > Error:
332+ # > ! `x` must not be empty.
333+ x <- c(1 , 2 , 3 )
334+ check_named(x )
335+ # > Error:
336+ # > ! `x` must be named.
337+ names(x ) <- c(" a" , " b" , " a" )
338+ check_named(x , unique = TRUE )
339+ # > Error:
340+ # > ! `x` must have unique names. Duplicates: "a".
341+ names(x ) <- c(" a" , " b" , " " )
342+ check_named(x , allow_empty = FALSE )
343+ # > Error:
344+ # > ! `x` must not contain empty names.
345+ ```
346+
282347File/dir existence validation:
283348
284349``` r
@@ -350,8 +415,9 @@ check_my_class(x, n = in_range(1, 2))
350415### Notes
351416
352417favr relies heavily on the imported packages
353- [ rlang] ( https://rlang.r-lib.org ) and [ cli] ( https://cli.r-lib.org/ ) . For
354- data validation using user-defined schemas, see
418+ [ rlang] ( https://rlang.r-lib.org ) and [ cli] ( https://cli.r-lib.org/ ) .
419+
420+ For data validation using user-defined schemas, see
355421[ fluffy] ( https://lj-jenkins.github.io/fluffy/ ) .
356422
357423## Getting help
0 commit comments