-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
325 lines (268 loc) · 7.6 KB
/
Copy pathREADME.Rmd
File metadata and controls
325 lines (268 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# fluffy <a href="https://lj-jenkins.github.io/fluffy/"><img src="man/figures/logo.png" align="right" height="138" alt="fluffy website" /></a>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=fluffy)
[](https://CRAN.R-project.org/package=fluffy)
[](https://github.com/LJ-Jenkins/fluffy/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
Validate R objects against user-defined schemas, with informative errors and data transformation.
## Installation
Install the latest release of fluffy from CRAN:
```{r, eval = FALSE}
install.packages("fluffy")
```
You can install the development version of fluffy from GitHub:
```{r, eval = FALSE}
# install.packages("pak")
pak::pak("LJ-Jenkins/fluffy")
```
## Basic Usage
```{r basic usage, error=TRUE}
library(fluffy)
df <- data.frame(x = 1:3, y = c(" a", "b ", " c"))
v <- Validator(
data = df,
schema = list(
type = "data.frame",
min_nrow = 1,
x = list(
type = "numeric",
max_val = 5
),
y = list(
type = "character",
apply = function(x) trimws(x),
nzchar = TRUE
)
)
)
#- Specified data is transformed
v@data
#- Overall validity
v@valid
#- Structured errors property
v@errors
#- Informative errors that reflect the perceived data structure
Validator(
data = list(1, a = "a", b = 10, x = -1),
schema = list(
type = "data.frame",
list(type = "character"),
a = list(min_nchar = 2),
b = list(min_length = 2, max_val = 5),
x = list(positive = TRUE)
),
error = TRUE
)
#- Transformed data can be accessed during the validation
Validator(
data = list(a = 1, b = 1),
schema = list(
a = list(apply = "function(x) x + 1"),
b = list(apply = function(x, .data, ...) if (.data[["a"]] > 1) x + 1)
)
)@data
#- Extensible
s <- Schema(list(double_if_five_else_error = TRUE))
s@valid
s@errors
s <- add_rule(
s,
name = "double_if_five_else_error",
validator_fn = function(field, schema_field, ...) {
if (schema_field) {
if (length(field) != 1L) {
list(error = "Field must be length 1.")
} else if (field != 5) {
list(error = "Does not equal 5.")
} else {
list(data = field * 2)
}
}
},
schema_fn = function(schema_field, ...) {
if (!isTRUE(schema_field) && !isFALSE(schema_field)) {
"Must be a boolean."
}
},
rule_type = "transform"
)
s@valid
v <- Validator(data = 5, schema = s)
v@valid
v@data
Validator(data = 1, schema = s, error = TRUE)
#- Works on numerous non-empty R object types, with data elements able to be
# validated if they can be accessed with `[[`.
Validator(
call("mean", 1:10),
list(
type = "call",
list(type = "name"),
list(predicate = "function(x) identical(x, 1:10)")
)
)@valid
Validator(expression(x + 1), list(type = "expression"))@valid
Validator(table(x = 1), list(type = "table"))@valid
Validator(new.env(), list(type = "environment"))@valid
e <- new.env()
e$a <- 1L
e$b <- "Hi"
Validator(
e,
list(
type = "environment",
a = list(type = "integer"),
b = list(type = "character")
)
)@valid
```
## Overview
fluffy provides three [S7](https://CRAN.R-project.org/package=S7) classes: `Registry`, `Schema`, and `Validator`.
`Registry` defines rules and stores all built-in fluffy rule names and definitions.
```{r Registry}
r <- Registry()
S7::prop_names(r)
```
`Schema` takes a user-defined nested list schema, validates the schema, and reorders the schema according to the order defined in the `Registry`. By default `Schema` creates a `Registry` if one is not passed to the function.
```{r Schema}
s <- Schema(list(type = "integer", default = 1L))
s@schema
S7::prop_names(s)
```
`Validator` takes data and a user-defined `Schema`, and applies each `Schema` field against the data. It does this in four passes:
1. control rules: rules that can alter control flow and stop other rules operating, e.g., `required`.
2. transform rules: rules that can modify the data, e.g., `apply` and `coerce`.
3. validate rules: rules that check the data against the schema, e.g., `type` and `min_val`.
4. finalize rules: rules that only operate if all other rules in the schema node passed validation without error, e.g., `apply_last` and `coerce_last`.
A list given as a schema will be passed to `Schema()` on ingest.
```{r Validator}
v <- Validator(
data = list(a = 1, b = "Hello"),
schema = list(
a = list(
type = "numeric",
min_val = 0,
max_val = 5
),
b = list(
type = "character",
apply = "\\(x) paste(x, 'World!')"
),
c = list(
required = FALSE,
type = "data.frame"
),
d = list(
default = 10L
)
)
)
v@data
S7::prop_names(v)
```
Using list schemas but virtually any R type for validation, fluffy can be used on a range of data types once loaded into R.
```{r data types example, error=TRUE}
yaml_schema <- yaml::yaml.load(
"
type: 'list'
a:
type: 'character'
b:
type: 'list'
a:
type: 'numeric'
b:
type: 'character'
min_nchar: 3
"
)
yaml_data <- yaml::yaml.load(
"
a: 1
b:
a: 1
b: 'Hi'
"
)
Validator(yaml_data, yaml_schema, error = TRUE)
json_schema <- jsonlite::fromJSON(
'{
"type": "list",
"a": {
"type": "numeric",
"min_length": 2
},
"b": {
"type": "list",
"a": {
"type": "numeric",
"max_val": 5
},
"b": {
"type": "character"
}
}
}'
)
json_data <- jsonlite::fromJSON(
'{
"a": 1,
"b": {
"a": 10,
"b": "Hi"
}
}'
)
Validator(json_data, json_schema, error = TRUE)
# rectangular data, from `readr` readme
# works for any data.frame data, e.g., sav, dta, xls, xlsx, csv, tsv, etc.
rect_schema <- list(
type = "data.frame",
chicken = list(
type = "character",
nzchar = TRUE
),
sex = list(
coerce = "factor",
levels = c("rooster", "hen")
),
eggs_laid = list(
type = "integer",
positive = TRUE
),
motto = list(
type = "character",
nzchar = TRUE
)
)
rect_data <- readr::read_csv(
readr::readr_example("chickens.csv"),
show_col_types = FALSE
)
Validator(rect_data, rect_schema, error = TRUE)
```
## Vignettes
For detailed information on using fluffy, see the vignettes:
* [Builtin rules](https://lj-jenkins.github.io/fluffy/articles/validation-rules.html)
* [Creating Schemas and Validating Data](https://lj-jenkins.github.io/fluffy/articles/validating-data.html)
* [Adding custom rules](https://lj-jenkins.github.io/fluffy/articles/custom-rules.html)
## Notes
fluffy was inspired by and modelled on Python's [Cerberus](https://github.com/pyeve/cerberus) (hence the name!).
Error printing in fluffy was modelled on [lobstr](https://lobstr.r-lib.org/)'s tree function.
fluffy was originally called 'RV' but was renamed to distinguish itself from the [rv package manager](https://github.com/A2-ai/rv).
## Getting help
If you encounter a clear bug, please file an issue with a minimal reproducible example on [GitHub](https://github.com/LJ-Jenkins/fluffy/issues).
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](https://lj-jenkins.github.io/fluffy/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.