Skip to content

Commit 20d444d

Browse files
authored
Improve validate option messages (#219)
* simplify errorValueNotAllowed string building * strip function name prefix from aggregated error messages * update tests to match refactored error messages * update NEWS * address review comments
1 parent 60fc0a4 commit 20d444d

5 files changed

Lines changed: 36 additions & 38 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Minor improvements and bug fixes
44

5+
- `validateIsOption()` aggregated error messages no longer include internal function name prefixes (#219).
56
- Validation errors in R6 active bindings now show a readable caller name instead of the anonymous function body (#218).
67
- `validateIsOption()` no longer emits a warning when auto-converting whole-number `numeric` to `integer` (#218).
78

R/messages.R

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,26 +285,14 @@ messages <- list(
285285
))
286286
},
287287
errorValueNotAllowed = function(values, parentValues) {
288-
callingFunction <- .getCallingFunctionName()
289-
notIncludedValues <- values[!values %in% parentValues]
290-
notIncludedValuesStr <- paste(head(notIncludedValues, 5), collapse = ", ")
291-
notIncludedValuesStr <- ifelse(
292-
length(notIncludedValues) > 5,
293-
paste(notIncludedValuesStr, "..."),
294-
notIncludedValuesStr
295-
)
296-
parentValuesStr <- paste(
297-
head(parentValues, 5),
298-
collapse = ", "
299-
)
300-
parentValuesStr <- ifelse(
301-
length(parentValues) > 5,
302-
paste(parentValuesStr, "..."),
303-
parentValuesStr
304-
)
305-
cliFormat(
306-
"{length(notIncludedValues)} value{?s} ({.val {notIncludedValuesStr}}) not included in allowed values.",
307-
"Allowed values: {.val {parentValuesStr}}"
288+
notIncludedValues <- unique(values[!values %in% parentValues])
289+
notIncludedVec <- head(notIncludedValues, 5)
290+
parentVec <- head(unique(parentValues), 5)
291+
notIncludedSuffix <- if (length(notIncludedValues) > 5) " ..." else ""
292+
parentSuffix <- if (length(unique(parentValues)) > 5) " ..." else ""
293+
cliFormat(
294+
"{.val {notIncludedVec}}{notIncludedSuffix} not allowed.",
295+
"Allowed values: {.val {parentVec}}{parentSuffix}."
308296
)
309297
},
310298
errorTypeNotSupported = function(objectName, type, expectedType) {

R/validation-options.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ validateIsOption <- function(options, validOptions) {
360360
.validateValue(options[[name]], validOptions[[name]], name)
361361
TRUE
362362
},
363-
error = function(e) e$message
363+
error = function(e) sub("^[^`]*`\\: ", "", e$message)
364364
)
365365

366366
if (!isTRUE(result)) {

tests/testthat/test-validation-options.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,7 @@ test_that("validateIsOption() errors when option values exceed expected length",
435435
)
436436
expect_error(
437437
validateIsOption(testOptions, validOptions),
438-
regexp = messages$errorWrongLength(
439-
c(TRUE, FALSE),
440-
1,
441-
"includeInteractions"
442-
),
443-
fixed = TRUE
438+
"(includeInteractions).*(should be of length).*(1)"
444439
)
445440
})
446441

@@ -495,7 +490,11 @@ test_that("validateIsOption() flags out-of-range and invalid values", {
495490
)
496491
expect_error(
497492
validateIsOption(invalidOptions, validOptions),
498-
"(optimizationMethod).*(1 value).*(none).*(not included in allowed values)"
493+
regexp = messages$errorValueNotAllowed(
494+
"none",
495+
c("gradientDescent", "geneticAlgorithm")
496+
),
497+
fixed = TRUE
499498
)
500499
})
501500

@@ -544,8 +543,7 @@ test_that("validateIsOption() validates expectedLength", {
544543
optionsWrongLength <- list(methods = "a")
545544
expect_error(
546545
validateIsOption(optionsWrongLength, validOptionsWithLength),
547-
regexp = messages$errorWrongLength("a", 2, "methods"),
548-
fixed = TRUE
546+
"(methods).*(should be of length).*(2)"
549547
)
550548
})
551549

@@ -710,7 +708,11 @@ test_that("validateIsOption() detects values not in allowed values for data fram
710708
)
711709

712710
expect_true(grepl("gender", err))
713-
expect_true(grepl("not included in allowed values", err))
711+
expect_true(grepl(
712+
messages$errorValueNotAllowed(c("Other"), c("M", "F")),
713+
err,
714+
fixed = TRUE
715+
))
714716
})
715717

716718
test_that("validateIsOption() rejects NA when not allowed in data frame columns", {

tests/testthat/test-validation-vector.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,35 +210,40 @@ test_that("validateVectorValues() correctly identifies unpermitted values", {
210210
type = "numeric",
211211
allowedValues = c(1, 2)
212212
),
213-
"(1 value).*(3).*(not included in allowed values).*(Allowed values:).*(1, 2)"
213+
regexp = messages$errorValueNotAllowed(3, c(1, 2)),
214+
fixed = TRUE
214215
)
215216
expect_error(
216217
validateVectorValues(
217218
x = c("a", "b", "z"),
218219
type = "character",
219220
allowedValues = c("a", "b")
220221
),
221-
"(1 value).*(z).*(not included in allowed values).*(Allowed values:).*(a, b)"
222+
regexp = messages$errorValueNotAllowed("z", c("a", "b")),
223+
fixed = TRUE
222224
)
223225
expect_error(
224226
validateVectorValues(
225227
x = LETTERS[1:25],
226228
type = "character",
227229
allowedValues = LETTERS[1:10]
228230
),
229-
"(15 values).*(K, L, M, N, O \\.\\.\\.).*(not included in allowed values).*(Allowed values:).*(A, B, C, D, E \\.\\.\\.)"
231+
regexp = messages$errorValueNotAllowed(LETTERS[1:25], LETTERS[1:10]),
232+
fixed = TRUE
230233
)
231234
expect_error(
232235
validateVectorValues(x = TRUE, type = "logical", allowedValues = FALSE),
233-
"(1 value).*(TRUE).*(not included in allowed values).*(Allowed values:).*(FALSE)"
236+
regexp = messages$errorValueNotAllowed(TRUE, FALSE),
237+
fixed = TRUE
234238
)
235239
expect_error(
236240
validateVectorValues(
237241
x = as.factor(c(1, 2, 3)),
238242
type = "factor",
239243
allowedValues = as.factor(c(1, 2))
240244
),
241-
"(1 value).*(3).*(not included in allowed values).*(Allowed values:).*(1, 2)"
245+
regexp = messages$errorValueNotAllowed(as.factor(3), as.factor(c(1, 2))),
246+
fixed = TRUE
242247
)
243248
expect_error(
244249
validateVectorValues(
@@ -247,7 +252,8 @@ test_that("validateVectorValues() correctly identifies unpermitted values", {
247252
allowedValues = c(1, 2),
248253
naAllowed = FALSE
249254
),
250-
"(1 value).*(NA).*(not included in allowed values).*(Allowed values:).*(1, 2)"
255+
regexp = messages$errorValueNotAllowed(NA, c(1, 2)),
256+
fixed = TRUE
251257
)
252258
expect_error(
253259
validateVectorValues(
@@ -256,7 +262,8 @@ test_that("validateVectorValues() correctly identifies unpermitted values", {
256262
allowedValues = c(1, 2, NA),
257263
naAllowed = FALSE
258264
),
259-
"(1 value).*(NA).*(not included in allowed values).*(Allowed values:).*(1, 2)"
265+
regexp = messages$errorValueNotAllowed(NA, c(1, 2)),
266+
fixed = TRUE
260267
)
261268
})
262269

0 commit comments

Comments
 (0)