diff --git a/NAMESPACE b/NAMESPACE index 2d9a043..e6403f1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(validate_value,default) +S3method(validate_value,email) +S3method(validate_value,id) +S3method(validate_value,natural_number_list) +S3method(validate_value,text) +S3method(validate_value,url) export(Dataset) export(Distribution) export(create_dataset) diff --git a/R/dataset.R b/R/dataset.R index 9d65dc3..2478fae 100644 --- a/R/dataset.R +++ b/R/dataset.R @@ -28,33 +28,33 @@ Dataset <- S7::new_class( properties = list( # ID des Datasets (wird serverseitig generiert) id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # Titel optional (wird nur bei Erstellung geprüft) title = prop_string( - validator = validate_text + validator = "text" ), # Organisation ID (required) organisation_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = FALSE ), # Optionale Beschreibung und Kontakt description = prop_string( - validator = validate_text, + validator = "text", max_length = 10000L ), contact_email = prop_string( - validator = validate_email + validator = "email" ), # Weblink landing_page = prop_string( - validator = validate_url + validator = "url" ), # Zeitpunkte (optional) @@ -66,24 +66,24 @@ Dataset <- S7::new_class( # Relations- und Katalog-IDs keyword_ids = prop_list( - validator = validate_natural_number_list + validator = "natural_number_list" ), zh_web_catalog_ids = prop_list( - validator = validate_natural_number_list + validator = "natural_number_list" ), relation_ids = prop_list( - validator = validate_natural_number_list + validator = "natural_number_list" ), see_also_ids = prop_list( - validator = validate_natural_number_list + validator = "natural_number_list" ), theme_ids = prop_list( - validator = validate_natural_number_list + validator = "natural_number_list" ), # Periodizität periodicity_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ) ), diff --git a/R/distribution.R b/R/distribution.R index 42fd871..03787f2 100644 --- a/R/distribution.R +++ b/R/distribution.R @@ -31,13 +31,13 @@ Distribution <- S7::new_class( # Title (required) title = prop_string( - validator = validate_text, + validator = "text", max_length = 1000L ), # Dataset ID (required) dataset_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = FALSE ), @@ -48,13 +48,13 @@ Distribution <- S7::new_class( # Sort order (optional) sort_order = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # Description (optional) description = prop_string( - validator = validate_text, + validator = "text", max_length = 10000L ), @@ -63,62 +63,54 @@ Distribution <- S7::new_class( # Access URL (optional) access_url = prop_string( - validator = validate_url + validator = "url" ), # Identifier (optional) - identifier = prop_string( - validator = validate_text - ), + identifier = prop_string(), # Right (optional) - right = prop_string( - validator = validate_text - ), + right = prop_string(), # Issued (optional) issued = prop_posixct(), # Byte size (optional) - byte_size = prop_numeric( - validator = validate_bytesize - ), + byte_size = prop_numeric(), # Status ID (optional) status_id = prop_numeric( default = 1, - validator = validate_id, + validator = "id", allow_na = TRUE ), # License ID (optional) license_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # Format ID (optional) format_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # Media Type ID (optional) media_type_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # Periodicity ID (optional) periodicity_id = prop_numeric( - validator = validate_id, + validator = "id", allow_na = TRUE ), # File Upload ID (optional) - file_upload_id = prop_string( - validator = validate_text - ) + file_upload_id = prop_string() ), constructor = function( title = S7::class_missing, diff --git a/R/properties.R b/R/properties.R index 45ddd6e..6347ea4 100644 --- a/R/properties.R +++ b/R/properties.R @@ -18,7 +18,7 @@ prop_string <- function(default = NULL, validator = NULL, ...) { } else { default }, - validator = function(value) validator(value, ...) + validator = function(value) validate(value, validator, ...) ) } @@ -31,12 +31,13 @@ prop_numeric <- function(default = NULL, validator = NULL, ...) { } else { default }, - validator = function(value) validator(value, ...) + validator = function(value) validate(value, validator, ...) + ) } -prop_list <- function(default = NULL, validator = NULL) { +prop_list <- function(default = NULL, validator = NULL, ...) { S7::new_property( class = S7::class_list, default = if (is.null(default)) { @@ -44,7 +45,7 @@ prop_list <- function(default = NULL, validator = NULL) { } else { default }, - validator = function(value) validator(value) + validator = function(value) validate(value, validator, ...) ) } diff --git a/R/validators.R b/R/validators.R index 83d4359..2ef488b 100644 --- a/R/validators.R +++ b/R/validators.R @@ -1,5 +1,25 @@ +validate <- function(value, validator, ...){ + class(value) <- validator + + validate_value(value, ...) + +} + + +validate_value <- function(value, ...){ + UseMethod("validate_value") +} + +#' @export +validate_value.default <- function(value, ...){ + NULL +} + + # Helper function to validate ID fields -validate_id <- function(value, allow_na = TRUE) { +#' @export +validate_value.id <- function(value, allow_na = TRUE) { + if (length(value) != 1) { return("must have exactly one value") } @@ -18,14 +38,15 @@ validate_id <- function(value, allow_na = TRUE) { return(NULL) } -validate_bytesize <- function(value){ - if (length(value) != 1) { - return("must have exactly one value") - } -} +# validate_bytesize <- function(value){ +# if (length(value) != 1) { +# return("must have exactly one value") +# } +# } +#' @export +validate_value.natural_number_list <- function(value, ...) { -validate_natural_number_list <- function(value) { if (length(value) > 0) { # Check if all elements are numeric if (!all(sapply(value, is.numeric))) { @@ -40,8 +61,9 @@ validate_natural_number_list <- function(value) { } } +#' @export +validate_value.text <- function(value, max_length = 1000L) { -validate_text <- function(value, max_length = 1000L) { # FIXME: is an empty string allowed? if (!is.na(value) && nzchar(value) && nchar(value) > max_length) { return(paste("can have a maximum of", max_length, "characters")) @@ -49,8 +71,9 @@ validate_text <- function(value, max_length = 1000L) { return(NULL) } +#' @export +validate_value.url <- function(value, ...) { -validate_url <- function(value) { if (!is.na(value) && nzchar(value)) { if (!grepl("^https?://[[:alnum:].-]+\\.[A-Za-z]{2,}(/[[:alnum:]._~%-]*)*$", value)) { return("must start with http:// or https:// and must have a valid domain") @@ -60,8 +83,8 @@ validate_url <- function(value) { } - -validate_email <- function(value) { +#' @export +validate_value.email <- function(value, ...) { if (!is.na(value) && nzchar(value)) { if (!grepl("^[^@]+@[^@]+\\.[^@]+$", value)) { return("must be a valid address.") diff --git a/tests/testthat/test-distribution.R b/tests/testthat/test-distribution.R new file mode 100644 index 0000000..c34bbd8 --- /dev/null +++ b/tests/testthat/test-distribution.R @@ -0,0 +1,25 @@ +test_that("the distribution object is correctly created", { + expect_no_error( + Distribution( + title = "Hello Distribution 1", + dataset_id = 6819, + stat_server_flag = FALSE, + zh_web_flag = FALSE, + ogd_flag = TRUE, + sort_order = 1, + description = "WOW this is a distribution!", + modified = "2025-03-31", + access_url = "https://test.ch", + identifier = "test", + right = "test", + issued = "2025-03-31", + byte_size = 12345, + status_id = 1, + license_id = 1, + format_id = 1, + media_type_id = 1, + periodicity_id = 1, + file_upload_id = "2" + ) + ) +})