fix url validator, add tests (#13628)

This commit is contained in:
William Cheng 2022-10-07 23:07:53 +08:00 committed by GitHub
parent 3ae37e27a5
commit 0602d8f8a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 38 deletions

View File

@ -86,8 +86,8 @@
stopifnot(is.character(`{{name}}`), length(`{{name}}`) == 1)
{{/isDateTime}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", `{{name}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `{{name}}`))
}
{{/isUri}}
@ -142,8 +142,8 @@
stopifnot(is.character(`{{name}}`), length(`{{name}}`) == 1)
{{/isDateTime}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", `{{name}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `{{name}}`))
}
{{/isUri}}
@ -251,8 +251,8 @@
}
{{/isEnum}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`{{baseName}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{baseName}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`{{baseName}}`))
}
{{/isUri}}
@ -383,8 +383,8 @@
}
{{/isEnum}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`{{name}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`{{name}}`))
}
{{/isUri}}
@ -444,8 +444,8 @@
stopifnot(is.character(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
{{/isDateTime}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", input_json$`{{name}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(input_json$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", input_json$`{{name}}`))
}
{{/isUri}}
@ -524,8 +524,8 @@
}
{{/pattern}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", self$`{{{name}}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(self$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
return(FALSE)
}
{{/isUri}}
@ -590,8 +590,8 @@
}
{{/pattern}}
{{#isUri}}
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", self$`{{name}}`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(self$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
invalid_fields["{{{name}}}"] <- "Invalid value for `{{{name}}}`, must be URL."
}
{{/isUri}}

View File

@ -43,8 +43,8 @@ Date <- R6::R6Class(
}
if (!missing(`url_property`)) {
stopifnot(is.character(`url_property`), length(`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", `url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `url_property`))
}
self$`url_property` <- `url_property`
@ -103,8 +103,8 @@ Date <- R6::R6Class(
self$`percent_description` <- this_object$`percent_description`
}
if (!is.null(this_object$`url_property`)) {
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -172,8 +172,8 @@ Date <- R6::R6Class(
this_object <- jsonlite::fromJSON(input_json)
self$`className` <- this_object$`className`
self$`percent_description` <- this_object$`percent_description`
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -204,8 +204,8 @@ Date <- R6::R6Class(
# check the required field `url_property`
if (!is.null(input_json$`url_property`)) {
stopifnot(is.character(input_json$`url_property`), length(input_json$`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", input_json$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(input_json$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", input_json$`url_property`))
}
} else {

View File

@ -593,6 +593,9 @@ test_that("Tests anyOf", {
})
test_that("Tests URL validation", {
valid_json <- '{"className":"date","percent_description":"abc","url_property":"https://stackoverflow.com/a/1/b/2"}'
Date$public_methods$validateJSON(valid_json) # shouldn't throw exception
valid_json <- '{"className":"date","percent_description":"abc","url_property":"https://abc.com/a/1/b/2"}'
Date$public_methods$validateJSON(valid_json) # shouldn't throw exception

View File

@ -38,8 +38,8 @@ Date <- R6::R6Class(
}
if (!missing(`url_property`)) {
stopifnot(is.character(`url_property`), length(`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", `url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `url_property`))
}
self$`url_property` <- `url_property`
@ -89,8 +89,8 @@ Date <- R6::R6Class(
self$`percent_description` <- this_object$`percent_description`
}
if (!is.null(this_object$`url_property`)) {
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -146,8 +146,8 @@ Date <- R6::R6Class(
this_object <- jsonlite::fromJSON(input_json)
self$`className` <- this_object$`className`
self$`percent_description` <- this_object$`percent_description`
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -171,8 +171,8 @@ Date <- R6::R6Class(
# check the required field `url_property`
if (!is.null(input_json$`url_property`)) {
stopifnot(is.character(input_json$`url_property`), length(input_json$`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", input_json$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(input_json$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", input_json$`url_property`))
}
} else {

View File

@ -43,8 +43,8 @@ Date <- R6::R6Class(
}
if (!missing(`url_property`)) {
stopifnot(is.character(`url_property`), length(`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", `url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `url_property`))
}
self$`url_property` <- `url_property`
@ -103,8 +103,8 @@ Date <- R6::R6Class(
self$`percent_description` <- this_object$`percent_description`
}
if (!is.null(this_object$`url_property`)) {
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -172,8 +172,8 @@ Date <- R6::R6Class(
this_object <- jsonlite::fromJSON(input_json)
self$`className` <- this_object$`className`
self$`percent_description` <- this_object$`percent_description`
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", this_object$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`url_property`))
}
self$`url_property` <- this_object$`url_property`
@ -204,8 +204,8 @@ Date <- R6::R6Class(
# check the required field `url_property`
if (!is.null(input_json$`url_property`)) {
stopifnot(is.character(input_json$`url_property`), length(input_json$`url_property`) == 1)
# validate URL using https://github.com/cran/librarian/blob/master/R/internal_functions.R#L131 credit: Desi Quintans
if (!any(grepl("(https?|ftp)://[^\\s/$.?#].[^\\s]*", input_json$`url_property`))) {
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(input_json$`url_property`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", input_json$`url_property`))
}
} else {