Skip to content

Commit 06af561

Browse files
author
alex-omophub
committed
Enhance input validation for codings in FhirResource
- Added checks to ensure that `codings` and `coding` parameters are lists of lists, providing clear error messages when validation fails. - Updated tests to validate the new input requirements for `resolve_batch` and `resolve_codeable_concept` methods, ensuring robustness against incorrect input formats.
1 parent 95f97b4 commit 06af561

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

R/fhir.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ FhirResource <- R6::R6Class(
9292
recommendations_limit = 5L,
9393
include_quality = FALSE) {
9494
stopifnot(is.list(codings), length(codings) >= 1, length(codings) <= 100)
95+
if (!all(vapply(codings, is.list, logical(1)))) {
96+
cli::cli_abort(c(
97+
"{.arg codings} must be a list of coding lists.",
98+
"i" = "Each element should be a list with {.field system}, {.field code}, etc.",
99+
"i" = "Example: {.code list(list(system = \"http://snomed.info/sct\", code = \"44054006\"))}"
100+
))
101+
}
95102

96103
body <- list(codings = codings)
97104
if (!is.null(resource_type)) body$resource_type <- resource_type
@@ -127,6 +134,13 @@ FhirResource <- R6::R6Class(
127134
recommendations_limit = 5L,
128135
include_quality = FALSE) {
129136
stopifnot(is.list(coding), length(coding) >= 1, length(coding) <= 20)
137+
if (!all(vapply(coding, is.list, logical(1)))) {
138+
cli::cli_abort(c(
139+
"{.arg coding} must be a list of coding lists.",
140+
"i" = "Each element should be a list with {.field system} and {.field code}.",
141+
"i" = "Example: {.code list(list(system = \"http://snomed.info/sct\", code = \"44054006\"))}"
142+
))
143+
}
130144

131145
body <- list(coding = coding)
132146
if (!is.null(text)) body$text <- text

tests/testthat/test-fhir.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,28 @@ test_that("fhir$resolve_batch validates codings length", {
203203
expect_error(resource$resolve_batch(list()))
204204
})
205205

206+
test_that("fhir$resolve_batch rejects a single coding (not list-of-lists)", {
207+
base_req <- httr2::request("https://api.omophub.com/v1")
208+
resource <- FhirResource$new(base_req)
209+
210+
# A single coding object (not wrapped in an outer list) should fail
211+
expect_error(
212+
resource$resolve_batch(list(system = "http://snomed.info/sct", code = "44054006")),
213+
"list of coding lists"
214+
)
215+
})
216+
217+
test_that("fhir$resolve_codeable_concept rejects a single coding (not list-of-lists)", {
218+
base_req <- httr2::request("https://api.omophub.com/v1")
219+
resource <- FhirResource$new(base_req)
220+
221+
# A single coding object (not wrapped in an outer list) should fail
222+
expect_error(
223+
resource$resolve_codeable_concept(list(system = "http://snomed.info/sct", code = "44054006")),
224+
"list of coding lists"
225+
)
226+
})
227+
206228
# ==============================================================================
207229
# resolve_codeable_concept()
208230
# ==============================================================================

0 commit comments

Comments
 (0)