From 498ff75c805f3bab298cc2ab876442fbb04a81cb Mon Sep 17 00:00:00 2001 From: Will Hipson Date: Wed, 7 May 2025 11:22:30 -0300 Subject: [PATCH 1/2] fixed issues with many cols and cols named e --- .Rhistory | 8 ++++---- DESCRIPTION | 2 +- NEWS.md | 8 ++++++++ R/check_schema.R | 4 ++-- tests/testthat/test-check_schema.R | 29 +++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/.Rhistory b/.Rhistory index d2861f5..5d4a3e6 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,7 +1,3 @@ -identical(x, x_s) -devtools::load_all(".") -expect_true(is_incrementing(1:5)) # TRUE -expect_true(is_incrementing(letters[1:5])) expect_false(is_incrementing(c(4, 3, 0))) expect_true(is_incrementing(1, NA, NA, 3)) expect_true(is_incrementing(1, 3, NA, NA)) @@ -510,3 +506,7 @@ devtools::check() devtools::submit_cran() renv::install("httr") devtools::submit_cran() +devtools::check() +devtools::check() +devtools::submit_cran() +install.packages("schematic") diff --git a/DESCRIPTION b/DESCRIPTION index 161171b..9965eee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: schematic Title: Tidy Schema Validation for Data Frames -Version: 0.1.0 +Version: 0.1.1 Authors@R: person("Will", "Hipson", email = "will.e.hipson@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0002-3931-2189")) Description: Validate data.frames against schemas to ensure that data matches expectations. Define schemas using 'tidyselect' and predicate functions for type consistency, nullability, and more. Schema failure messages can be tailored for non-technical users and are ideal for user-facing applications such as in 'shiny' or 'plumber'. Maintainer: Will Hipson diff --git a/NEWS.md b/NEWS.md index 6209c58..2bea4ea 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# schematic 0.1.1 + +### Bug fixes + +- Fixed issue where lots of column names in the message were being replaced with '...'. + +- Fixed issue where checking on a column named `e` triggered error in `check_schema()`. + # schematic 0.1.0 ### CRAN release diff --git a/R/check_schema.R b/R/check_schema.R index 1ddaca2..d0620d4 100644 --- a/R/check_schema.R +++ b/R/check_schema.R @@ -46,11 +46,11 @@ check_schema <- function(data, schema) { cols <- tryCatch({ tidyselect::eval_select(selector, data) |> names() - }, error = function(e) { + }, error = function(`___e`) { # avoid conflicts with columns named 'e' still_exists <- tidyselect::eval_select(selector, data, strict = FALSE) |> names() check_cols <- clean_c_wrapper( - rlang::as_label(selector) + paste(deparse(selector), collapse = "") ) |> strsplit(",\\s*") check_cols <- check_cols[[1]] diff --git a/tests/testthat/test-check_schema.R b/tests/testthat/test-check_schema.R index 457a42e..ed28789 100644 --- a/tests/testthat/test-check_schema.R +++ b/tests/testthat/test-check_schema.R @@ -93,5 +93,34 @@ test_that("rules with an error are handled", { ), regexp = "Error in predicate" ) +}) + +test_that("lots of columns in message are still reported", { + sch <- schema( + c(departure_gate, + arrival_gate, + flight_date, + flight_id, + svc_type, + route, + ac_type, + status + ) ~ is.character + ) + expect_error( + check_schema(data.frame(), sch), + regexp = "Columns `departure_gate`" + ) +}) + +test_that("lots of columns in message are still reported", { + sch <- schema( + e ~ is.character + ) + + expect_error( + check_schema(data.frame(a = 1), sch), + regexp = "Column `e`" + ) }) From e103f495638785f96e5673ab3747e4e95f05ac26 Mon Sep 17 00:00:00 2001 From: Will Hipson Date: Wed, 7 May 2025 11:40:01 -0300 Subject: [PATCH 2/2] fix issue where modifiers fail with custom predicates --- NEWS.md | 2 ++ R/modifiers.R | 2 ++ tests/testthat/test-modifiers.R | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/NEWS.md b/NEWS.md index 2bea4ea..923e581 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ - Fixed issue where checking on a column named `e` triggered error in `check_schema()`. +- Fixed issue where modifiers wouldn't work with custom predicates. + # schematic 0.1.0 ### CRAN release diff --git a/R/modifiers.R b/R/modifiers.R index 23c6153..4ef0553 100644 --- a/R/modifiers.R +++ b/R/modifiers.R @@ -13,6 +13,7 @@ #' is_incrementing_null <- mod_nullable(is_incrementing) #' is_incrementing_null(x) # TRUE mod_nullable <- function(pred) { + pred <- rlang::as_function(pred) function(x) { if (all(is.na(x))) return(TRUE) pred(x[!is.na(x)]) @@ -34,6 +35,7 @@ mod_nullable <- function(pred) { #' is_incrementing_inf <- mod_infinitable(is_incrementing) #' is_incrementing_inf(x) # TRUE mod_infinitable <- function(pred) { + pred <- rlang::as_function(pred) function(x) { if (all(is.na(x))) return(TRUE) pred(x[!is.infinite(x)]) diff --git a/tests/testthat/test-modifiers.R b/tests/testthat/test-modifiers.R index 2a45a45..9a034d9 100644 --- a/tests/testthat/test-modifiers.R +++ b/tests/testthat/test-modifiers.R @@ -8,3 +8,19 @@ test_that("mod_infinitable", { is_incrementing_inf <- mod_infinitable(is_incrementing) expect_true(is_incrementing_inf(x)) }) + +test_that("mod_nullable works with custom function", { + sch <- schema( + c(a, b) ~ mod_nullable(~all(.x %in% c("a", "b", "c"))) + ) + + expect_no_error({ + check_schema( + data.frame( + a = c(NA, "a", "c"), + b = c("a", "b", "c") + ), + sch + ) + }) +})