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

View File

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

View File

@ -593,6 +593,9 @@ test_that("Tests anyOf", {
}) })
test_that("Tests URL validation", { 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"}' 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 Date$public_methods$validateJSON(valid_json) # shouldn't throw exception

View File

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

View File

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