Skip to content
Open
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
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 12 additions & 12 deletions R/dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
),
Expand Down
36 changes: 14 additions & 22 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
),

Expand All @@ -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
),

Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions R/properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ prop_string <- function(default = NULL, validator = NULL, ...) {
} else {
default
},
validator = function(value) validator(value, ...)
validator = function(value) validate(value, validator, ...)
)
}

Expand All @@ -31,20 +31,21 @@ 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)) {
list()
} else {
default
},
validator = function(value) validator(value)
validator = function(value) validate(value, validator, ...)
)
}

Expand Down
45 changes: 34 additions & 11 deletions R/validators.R
Original file line number Diff line number Diff line change
@@ -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")
}
Expand All @@ -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))) {
Expand All @@ -40,17 +61,19 @@ 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"))
}
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")
Expand All @@ -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.")
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-distribution.R
Original file line number Diff line number Diff line change
@@ -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"
)
)
})