validation

library(shinyQueryBuilder)
#> Loading required package: queryBuilder

Before the value is sent to Shiny server, rules provided by user are validated. Invalid configuration for the associated rule (e.g. between operator value exceeding valid bounds) is highlighted in the widget, what’s more the value is not sent to the application server.

Validation can be customized on filter level with validate argument, defines as a list of the following arguments:

queryFilter(
  "BirthDate", type = "Date", validation = list(format = "YYYY-MM-DD")
)
queryFilter(
  "Name", type = "character", input = "text", validation = list(format = "[A-Z][a-z]+")
)

queryFilter(
  "digit", type = "integer", validation = list(min = 0, max = 9, step = 1)
)
queryFilter(
  "weekday_abbr", type = "character", input = "textarea", validation = list(min = 3, max = 3)
)

queryFilter(
  "digit", type = "numeric",
  validation = list(
    min = 0, max = 9, step = 1,
    messages = list(
      min = "Minimum value allowed is 0",
      max = "Maximum value allowed is 9",
      step = "Only integers accepted"
    )
  )
)
queryFilter(
  "Name", type = "character", input = "text",
  validation = list(format = "[A-Z][a-z]+", messages = list(format = "Name should be capitalized"))
)

In order to define it, create the function definition as character string and pass it to js function:

queryFilter(
  "fav_letter",
  type = "character",
  input = "text",
  validation = list(
    callback = js(paste0(
      "function(value, Rule) {",
      "var result = true;",
      "is_valid = value.length == 1 && value.toUpperCase() == value;",
      "if (!is_valid) {result = 'Single capital letter allowed only'};",
      "return result;",
      "}",
      collapse = ""
    ))
  )
)

Check the below application to test validation cases:

library(shiny)
pkgload::load_all()

ui <- fluidPage(
  queryBuilderInput(
    "qb",
    filters = list(
      queryFilter(
        "BirthDate", type = "Date", validation = list(format = "YYYY-MM-DD")
      ),
      queryFilter(
        "digit", type = "numeric",
        validation = list(
          min = 0, max = 9, step = 1,
          messages = list(
            min = "Minimum value allowed is 0",
            max = "Maximum value allowed is 9",
            step = "Only integers accepted"
          )
        )
      ),
      queryFilter(
        "Name", type = "character", input = "text",
        validation = list(format = "[A-Z][a-z]+", messages = list(format = "Name should be capitalized"))
      ),
      queryFilter(
        "weekday_abbr", type = "character", input = "textarea", validation = list(min = 3, max = 3)
      ),
      queryFilter(
        "fav_letter",
        type = "character",
        input = "text",
        validation = list(
          callback = js(paste0(
            "function(value, Rule) {",
            "var result = true;",
            "is_valid = value.length == 1 && value.toUpperCase() == value;",
            "if (!is_valid) {result = 'Single capital letter allowed only'};",
            "return result;",
            "}",
            collapse = ""
          ))
        )
      )
    )
  ),
  shiny::verbatimTextOutput("expr")
)

server <- function(input, output, session) {
  output$expr <- renderPrint({
    print(queryToExpr(input$qb))
  })
}

shinyApp(ui, server, options = list(launch.browser = TRUE))