Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore invalid criteria in SearchBuilder server-side processing #1114

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions R/searchbuilder.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# server-side processing for the SearchBuilder extension
# https://datatables.net/extensions/searchbuilder/

# returns NULL if none of the search criteria are valid
sbEvaluateSearch = function(search, data) {
# https://datatables.net/reference/option/searchBuilder.preDefined
stopifnot(search$logic %in% c('AND', 'OR'))
Reduce(
switch(search$logic, AND = `&`, OR = `|`),
lapply(search$criteria, sbEvaluateCriteria, data)
dropNULL(lapply(search$criteria, sbEvaluateCriteria, data))
)
}

Expand All @@ -15,18 +16,25 @@ sbEvaluateCriteria = function(criteria, data) {
if ('logic' %in% names(criteria)) {
# this is a sub-group
sbEvaluateSearch(criteria, data)
} else {
} else if (sbHasValidCondition(criteria)) {
# this is a criteria
cond = criteria$condition
type = criteria$type
x = data[[criteria$origData %||% criteria$data]]
v = sbParseValue(sbExtractValue(criteria), type)
sbEvaluateCondition(cond, type, x, v)
} else {
# just return NULL for criteria with an invalid condition
}
}

# check that condition is present and values are non-empty
sbHasValidCondition = function(criteria) {
'condition' %in% names(criteria) && all(sapply(criteria, nzchar))
}

sbExtractValue = function(criteria) {
if (criteria$condition %in% c('between', '!between')) {
if ('value2' %in% names(criteria)) {
# array values are passed in a funny way to R
c(criteria$value1, criteria$value2)
} else {
Expand Down
3 changes: 2 additions & 1 deletion R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ dataTablesFilter = function(data, params) {

# apply SearchBuilder query if present
if (!is.null(s <- q$searchBuilder)) {
i = which(sbEvaluateSearch(s, data))
r = sbEvaluateSearch(s, data)
if (!is.null(r)) i = which(r)
}

# search by columns
Expand Down
4 changes: 4 additions & 0 deletions tests/testit/test-searchbuilder.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ assert('SearchBuilder complex queries work', {
)
(setequal(which(res), c(2:4, 7)))
})

assert('SearchBuilder skips evaluating invalid conditions', {
(is.null(sbEvaluateCriteria(list(data = 'Sepal.Length'), iris)))
})