fix r model validations, add tests (#13181)

This commit is contained in:
William Cheng 2022-08-15 00:27:10 +08:00 committed by GitHub
parent 9e20b01b12
commit c4dd42e7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 165 additions and 113 deletions

View File

@ -367,46 +367,46 @@
{{^isNullable}}
{{#required}}
# check if the required `{{{name}}}` is null
if (is.null(`{{{name}}}`)) {
FALSE
if (is.null(self$`{{{name}}}`)) {
return(FALSE)
}
{{/required}}
{{/isNullable}}
{{#hasValidation}}
{{#maxLength}}
if (nchar(`{{{name}}}`) > {{maxLength}}) {
FALSE
if (nchar(self$`{{{name}}}`) > {{maxLength}}) {
return(FALSE)
}
{{/maxLength}}
{{#minLength}}
if (nchar(`{{{name}}}`) < {{minLength}}) {
FALSE
if (nchar(self$`{{{name}}}`) < {{minLength}}) {
return(FALSE)
}
{{/minLength}}
{{#maximum}}
if (`{{{name}}}` >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
FALSE
if (self$`{{{name}}}` >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
return(FALSE)
}
{{/maximum}}
{{#minimum}}
if (`{{{name}}}` <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
FALSE
if (self$`{{{name}}}` <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
return(FALSE)
}
{{/minimum}}
{{#pattern}}
if (!str_detect(`{{{name}}}`, "{{{pattern}}}")) {
FALSE
if (!str_detect(self$`{{{name}}}`, "{{{pattern}}}")) {
return(FALSE)
}
{{/pattern}}
{{#maxItems}}
if (length(`{{{name}}}`) > {{maxItems}}) {
FALSE
if (length(self$`{{{name}}}`) > {{maxItems}}) {
return(FALSE)
}
{{/maxItems}}
{{#minItems}}
if (length(`{{{name}}}`) < {{minItems}}) {
FALSE
if (length(self$`{{{name}}}`) < {{minItems}}) {
return(FALSE)
}
{{/minItems}}
@ -427,46 +427,46 @@
{{^isNullable}}
{{#required}}
# check if the required `{{{name}}}` is null
if (is.null(`{{{name}}}`)) {
invalid_fields[`{{{name}}}`] = "Non-nullable required field `{{{name}}}` cannot be null."
if (is.null(self$`{{{name}}}`)) {
invalid_fields["{{{name}}}"] = "Non-nullable required field `{{{name}}}` cannot be null."
}
{{/required}}
{{/isNullable}}
{{#hasValidation}}
{{#maxLength}}
if (nchar(`{{{name}}}`) > {{maxLength}}) {
invalid_fields[`{{{name}}}`] = "Invalid length for `{{{name}}}`, must be smaller than or equal to {{maxLength}}."
if (nchar(self$`{{{name}}}`) > {{maxLength}}) {
invalid_fields["{{{name}}}"] = "Invalid length for `{{{name}}}`, must be smaller than or equal to {{maxLength}}."
}
{{/maxLength}}
{{#minLength}}
if (nchar(`{{{name}}}`) < {{minLength}}) {
invalid_fields[`{{{name}}}`] = "Invalid length for `{{{name}}}`, must be bigger than or equal to {{minLength}}."
if (nchar(self$`{{{name}}}`) < {{minLength}}) {
invalid_fields["{{{name}}}"] = "Invalid length for `{{{name}}}`, must be bigger than or equal to {{minLength}}."
}
{{/minLength}}
{{#maximum}}
if (`{{{name}}}` >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
invalid_fields[`{{{name}}}`] = "Invalid value for `{{{name}}}`, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}."
if (self$`{{{name}}}` >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
invalid_fields["{{{name}}}"] = "Invalid value for `{{{name}}}`, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}."
}
{{/maximum}}
{{#minimum}}
if (`{{{name}}}` <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
invalid_fields[`{{{name}}}`] = "Invalid value for `{{{name}}}`, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}."
if (self$`{{{name}}}` <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
invalid_fields["{{{name}}}"] = "Invalid value for `{{{name}}}`, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}."
}
{{/minimum}}
{{#pattern}}
if (!str_detect(`{{{name}}}`, "{{{pattern}}}")) {
invalid_fields[`{{{name}}}`] = "Invalid value for `{{{name}}}`, must conform to the pattern {{{pattern}}}."
if (!str_detect(self$`{{{name}}}`, "{{{pattern}}}")) {
invalid_fields["{{{name}}}"] = "Invalid value for `{{{name}}}`, must conform to the pattern {{{pattern}}}."
}
{{/pattern}}
{{#maxItems}}
if (length(`{{{name}}}`) > {{maxItems}}) {
invalid_fields[`{{{name}}}`] = "Invalid length for `{{{name}}}`, number of items must be less than or equal to {{maxItems}}."
if (length(self$`{{{name}}}`) > {{maxItems}}) {
invalid_fields["{{{name}}}"] = "Invalid length for `{{{name}}}`, number of items must be less than or equal to {{maxItems}}."
}
{{/maxItems}}
{{#minItems}}
if (length(`{{{name}}}`) < {{minItems}}) {
invalid_fields[`{{{name}}}`] = "Invalid length for `{{{param}}}`, number of items must be greater than or equal to {{minItems}}."
if (length(self$`{{{name}}}`) < {{minItems}}) {
invalid_fields["{{{name}}}"] = "Invalid length for `{{{param}}}`, number of items must be greater than or equal to {{minItems}}."
}
{{/minItems}}
@ -475,4 +475,4 @@
invalid_fields
}
)
)
)

View File

@ -234,3 +234,4 @@ AllofTagApiResponse <- R6::R6Class(
}
)
)

View File

@ -157,8 +157,8 @@ Animal <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -173,11 +173,12 @@ Animal <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -163,13 +163,13 @@ BasquePig <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
# check if the required `color` is null
if (is.null(`color`)) {
FALSE
if (is.null(self$`color`)) {
return(FALSE)
}
TRUE
@ -184,16 +184,17 @@ BasquePig <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
# check if the required `color` is null
if (is.null(`color`)) {
invalid_fields[`color`] = "Non-nullable required field `color` cannot be null."
if (is.null(self$`color`)) {
invalid_fields["color"] = "Non-nullable required field `color` cannot be null."
}
invalid_fields
}
)
)

View File

@ -181,8 +181,8 @@ Cat <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -197,11 +197,12 @@ Cat <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -142,3 +142,4 @@ CatAllOf <- R6::R6Class(
}
)
)

View File

@ -150,8 +150,8 @@ Category <- R6::R6Class(
#' @return true if the values in all fields are valid.
#' @export
isValid = function() {
if (!str_detect(`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
FALSE
if (!str_detect(self$`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
return(FALSE)
}
TRUE
@ -165,11 +165,12 @@ Category <- R6::R6Class(
#' @export
getInvalidFields = function() {
invalid_fields <- list()
if (!str_detect(`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
invalid_fields[`name`] = "Invalid value for `name`, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."
if (!str_detect(self$`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
invalid_fields["name"] = "Invalid value for `name`, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."
}
invalid_fields
}
)
)

View File

@ -163,13 +163,13 @@ DanishPig <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
# check if the required `size` is null
if (is.null(`size`)) {
FALSE
if (is.null(self$`size`)) {
return(FALSE)
}
TRUE
@ -184,16 +184,17 @@ DanishPig <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
# check if the required `size` is null
if (is.null(`size`)) {
invalid_fields[`size`] = "Non-nullable required field `size` cannot be null."
if (is.null(self$`size`)) {
invalid_fields["size"] = "Non-nullable required field `size` cannot be null."
}
invalid_fields
}
)
)

View File

@ -181,8 +181,8 @@ Dog <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -197,11 +197,12 @@ Dog <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -142,3 +142,4 @@ DogAllOf <- R6::R6Class(
}
)
)

View File

@ -188,3 +188,4 @@ ModelApiResponse <- R6::R6Class(
}
)
)

View File

@ -167,3 +167,4 @@ NestedOneOf <- R6::R6Class(
}
)
)

View File

@ -257,3 +257,4 @@ Order <- R6::R6Class(
}
)
)

View File

@ -260,13 +260,13 @@ Pet <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `name` is null
if (is.null(`name`)) {
FALSE
if (is.null(self$`name`)) {
return(FALSE)
}
# check if the required `photoUrls` is null
if (is.null(`photoUrls`)) {
FALSE
if (is.null(self$`photoUrls`)) {
return(FALSE)
}
TRUE
@ -281,16 +281,17 @@ Pet <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `name` is null
if (is.null(`name`)) {
invalid_fields[`name`] = "Non-nullable required field `name` cannot be null."
if (is.null(self$`name`)) {
invalid_fields["name"] = "Non-nullable required field `name` cannot be null."
}
# check if the required `photoUrls` is null
if (is.null(`photoUrls`)) {
invalid_fields[`photoUrls`] = "Non-nullable required field `photoUrls` cannot be null."
if (is.null(self$`photoUrls`)) {
invalid_fields["photoUrls"] = "Non-nullable required field `photoUrls` cannot be null."
}
invalid_fields
}
)
)

View File

@ -257,3 +257,4 @@ Special <- R6::R6Class(
}
)
)

View File

@ -165,3 +165,4 @@ Tag <- R6::R6Class(
}
)
)

View File

@ -166,3 +166,4 @@ UpdatePetRequest <- R6::R6Class(
}
)
)

View File

@ -303,3 +303,4 @@ User <- R6::R6Class(
}
)
)

View File

@ -242,6 +242,24 @@ test_that("Tests special item names", {
})
test_that ("Tests validations", {
invalid_pet <- Pet$new()
expect_false(invalid_pet$isValid())
invalid_fields <- invalid_pet$getInvalidFields()
expect_equal(invalid_fields[["name"]], "Non-nullable required field `name` cannot be null.")
expect_equal(invalid_fields[["photoUrls"]], "Non-nullable required field `photoUrls` cannot be null.")
# fix invalid fields
invalid_pet$name <- "valid pet"
invalid_pet$photoUrls <- list("photo_test", "second test")
expect_true(invalid_pet$isValid())
expect_equal(invalid_pet$getInvalidFields(), list())
})
test_that("Tests oneOf", {
basque_pig_json <-
'{"className": "BasquePig", "color": "red"}'

View File

@ -234,3 +234,4 @@ AllofTagApiResponse <- R6::R6Class(
}
)
)

View File

@ -157,8 +157,8 @@ Animal <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -173,11 +173,12 @@ Animal <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -163,13 +163,13 @@ BasquePig <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
# check if the required `color` is null
if (is.null(`color`)) {
FALSE
if (is.null(self$`color`)) {
return(FALSE)
}
TRUE
@ -184,16 +184,17 @@ BasquePig <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
# check if the required `color` is null
if (is.null(`color`)) {
invalid_fields[`color`] = "Non-nullable required field `color` cannot be null."
if (is.null(self$`color`)) {
invalid_fields["color"] = "Non-nullable required field `color` cannot be null."
}
invalid_fields
}
)
)

View File

@ -181,8 +181,8 @@ Cat <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -197,11 +197,12 @@ Cat <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -142,3 +142,4 @@ CatAllOf <- R6::R6Class(
}
)
)

View File

@ -150,8 +150,8 @@ Category <- R6::R6Class(
#' @return true if the values in all fields are valid.
#' @export
isValid = function() {
if (!str_detect(`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
FALSE
if (!str_detect(self$`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
return(FALSE)
}
TRUE
@ -165,11 +165,12 @@ Category <- R6::R6Class(
#' @export
getInvalidFields = function() {
invalid_fields <- list()
if (!str_detect(`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
invalid_fields[`name`] = "Invalid value for `name`, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."
if (!str_detect(self$`name`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
invalid_fields["name"] = "Invalid value for `name`, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."
}
invalid_fields
}
)
)

View File

@ -163,13 +163,13 @@ DanishPig <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
# check if the required `size` is null
if (is.null(`size`)) {
FALSE
if (is.null(self$`size`)) {
return(FALSE)
}
TRUE
@ -184,16 +184,17 @@ DanishPig <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
# check if the required `size` is null
if (is.null(`size`)) {
invalid_fields[`size`] = "Non-nullable required field `size` cannot be null."
if (is.null(self$`size`)) {
invalid_fields["size"] = "Non-nullable required field `size` cannot be null."
}
invalid_fields
}
)
)

View File

@ -181,8 +181,8 @@ Dog <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(`className`)) {
FALSE
if (is.null(self$`className`)) {
return(FALSE)
}
TRUE
@ -197,11 +197,12 @@ Dog <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(`className`)) {
invalid_fields[`className`] = "Non-nullable required field `className` cannot be null."
if (is.null(self$`className`)) {
invalid_fields["className"] = "Non-nullable required field `className` cannot be null."
}
invalid_fields
}
)
)

View File

@ -142,3 +142,4 @@ DogAllOf <- R6::R6Class(
}
)
)

View File

@ -188,3 +188,4 @@ ModelApiResponse <- R6::R6Class(
}
)
)

View File

@ -167,3 +167,4 @@ NestedOneOf <- R6::R6Class(
}
)
)

View File

@ -257,3 +257,4 @@ Order <- R6::R6Class(
}
)
)

View File

@ -260,13 +260,13 @@ Pet <- R6::R6Class(
#' @export
isValid = function() {
# check if the required `name` is null
if (is.null(`name`)) {
FALSE
if (is.null(self$`name`)) {
return(FALSE)
}
# check if the required `photoUrls` is null
if (is.null(`photoUrls`)) {
FALSE
if (is.null(self$`photoUrls`)) {
return(FALSE)
}
TRUE
@ -281,16 +281,17 @@ Pet <- R6::R6Class(
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `name` is null
if (is.null(`name`)) {
invalid_fields[`name`] = "Non-nullable required field `name` cannot be null."
if (is.null(self$`name`)) {
invalid_fields["name"] = "Non-nullable required field `name` cannot be null."
}
# check if the required `photoUrls` is null
if (is.null(`photoUrls`)) {
invalid_fields[`photoUrls`] = "Non-nullable required field `photoUrls` cannot be null."
if (is.null(self$`photoUrls`)) {
invalid_fields["photoUrls"] = "Non-nullable required field `photoUrls` cannot be null."
}
invalid_fields
}
)
)

View File

@ -257,3 +257,4 @@ Special <- R6::R6Class(
}
)
)

View File

@ -165,3 +165,4 @@ Tag <- R6::R6Class(
}
)
)

View File

@ -166,3 +166,4 @@ UpdatePetRequest <- R6::R6Class(
}
)
)

View File

@ -303,3 +303,4 @@ User <- R6::R6Class(
}
)
)