fix validation for optional parameters/properties for ruby

This commit is contained in:
wing328 2016-08-01 16:50:11 +08:00
parent 0f8e1f8105
commit 8c357dd1d6
6 changed files with 118 additions and 63 deletions

View File

@ -230,7 +230,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("LICENSE", "", "LICENSE"));
// test files should not be overwritten // test files should not be overwritten
writeOptional(outputFolder, new SupportingFile("rspec.mustache", "", ".rspec")); writeOptional(outputFolder, new SupportingFile("rspec.mustache", "", ".rspec"));

View File

@ -57,31 +57,31 @@ module {{moduleName}}
{{/required}} {{/required}}
{{#hasValidation}} {{#hasValidation}}
{{#maxLength}} {{#maxLength}}
if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}} if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be smaller than or equal to {{{maxLength}}}.' fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be smaller than or equal to {{{maxLength}}}.'
end end
{{/maxLength}} {{/maxLength}}
{{#minLength}} {{#minLength}}
if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}} if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be great than or equal to {{{minLength}}}.' fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be great than or equal to {{{minLength}}}.'
end end
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} > {{{maximum}}} if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} > {{{maximum}}}
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{{maximum}}}.' fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{{maximum}}}.'
end end
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} < {{{minimum}}} if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} < {{{minimum}}}
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than or equal to {{{minimum}}}.' fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than or equal to {{{minimum}}}.'
end end
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
if {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}}) if {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ Regexp.new({{{pattern}}})
fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.' fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.'
end end

View File

@ -77,42 +77,46 @@
# @return Array for valid properies with the reasons # @return Array for valid properies with the reasons
def list_invalid_properties def list_invalid_properties
invalid_properties = Array.new invalid_properties = Array.new
{{#vars}}
{{#hasValidation}} {{#hasValidation}}
{{#required}}
if @{{{name}}}.nil? if @{{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil" invalid_properties.push("invalid value for '{{{name}}}', {{{name}}} cannot be nil.")
end end
{{/required}}
{{#maxLength}} {{#maxLength}}
if @{{{name}}}.to_s.length > {{{maxLength}}} if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.") invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.")
end end
{{/maxLength}} {{/maxLength}}
{{#minLength}} {{#minLength}}
if @{{{name}}}.to_s.length < {{{minLength}}} if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.") invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.")
end end
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
if @{{{name}}} > {{{maximum}}} if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} > {{{maximum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.") invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.")
end end
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
if @{{{name}}} < {{{minimum}}} if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} < {{{minimum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.") invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.")
end end
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}}) if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}})
invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.") invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.")
end end
{{/pattern}} {{/pattern}}
{{/hasValidation}} {{/hasValidation}}
{{/vars}}
return invalid_properties return invalid_properties
end end
@ -131,19 +135,19 @@
{{/isEnum}} {{/isEnum}}
{{#hasValidation}} {{#hasValidation}}
{{#maxLength}} {{#maxLength}}
return false if @{{{name}}}.to_s.length > {{{maxLength}}} return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length > {{{maxLength}}}
{{/maxLength}} {{/maxLength}}
{{#minLength}} {{#minLength}}
return false if @{{{name}}}.to_s.length < {{{minLength}}} return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}}.to_s.length < {{{minLength}}}
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
return false if @{{{name}}} > {{{maximum}}} return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} > {{{maximum}}}
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
return false if @{{{name}}} < {{{minimum}}} return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} < {{{minimum}}}
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
return false if @{{{name}}} !~ Regexp.new({{{pattern}}}) return false if {{^required}}!@{{{name}}}.nil? && {{/required}}@{{{name}}} !~ Regexp.new({{{pattern}}})
{{/pattern}} {{/pattern}}
{{/hasValidation}} {{/hasValidation}}
{{/vars}} {{/vars}}
@ -170,36 +174,38 @@
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] {{{name}}} Value to be assigned # @param [Object] {{{name}}} Value to be assigned
def {{{name}}}=({{{name}}}) def {{{name}}}=({{{name}}})
{{#required}}
if {{{name}}}.nil? if {{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil" fail ArgumentError, "{{{name}}} cannot be nil"
end end
{{/required}}
{{#maxLength}} {{#maxLength}}
if {{{name}}}.to_s.length > {{{maxLength}}} if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length > {{{maxLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}." fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
end end
{{/maxLength}} {{/maxLength}}
{{#minLength}} {{#minLength}}
if {{{name}}}.to_s.length < {{{minLength}}} if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}}.to_s.length < {{{minLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}." fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
end end
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
if {{{name}}} > {{{maximum}}} if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} > {{{maximum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}." fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}."
end end
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
if {{{name}}} < {{{minimum}}} if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} < {{{minimum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}." fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}."
end end
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}}) if {{^required}}!{{{name}}}.nil? && {{/required}}{{{name}}} !~ Regexp.new({{{pattern}}})
fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}." fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}."
end end

View File

@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/
- API version: 1.0.0 - API version: 1.0.0
- Package version: 1.0.0 - Package version: 1.0.0
- Build date: 2016-07-26T14:38:12.507+08:00 - Build date: 2016-08-01T16:37:23.828+08:00
- Build package: class io.swagger.codegen.languages.RubyClientCodegen - Build package: class io.swagger.codegen.languages.RubyClientCodegen
## Installation ## Installation

View File

@ -157,31 +157,31 @@ module Petstore
# verify the required parameter 'byte' is set # verify the required parameter 'byte' is set
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters" if byte.nil? fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters" if byte.nil?
if opts[:'integer'] > 100.0 if !opts[:'integer'].nil? && opts[:'integer'] > 100.0
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.0.' fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.0.'
end end
if opts[:'integer'] < 10.0 if !opts[:'integer'].nil? && opts[:'integer'] < 10.0
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.0.' fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.0.'
end end
if opts[:'int32'] > 200.0 if !opts[:'int32'].nil? && opts[:'int32'] > 200.0
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.0.' fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.0.'
end end
if opts[:'int32'] < 20.0 if !opts[:'int32'].nil? && opts[:'int32'] < 20.0
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.0.' fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.0.'
end end
if opts[:'float'] > 987.6 if !opts[:'float'].nil? && opts[:'float'] > 987.6
fail ArgumentError, 'invalid value for "opts[:"float"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 987.6.' fail ArgumentError, 'invalid value for "opts[:"float"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 987.6.'
end end
if opts[:'password'].to_s.length > 64 if !opts[:'password'].nil? && opts[:'password'].to_s.length > 64
fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be smaller than or equal to 64.' fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be smaller than or equal to 64.'
end end
if opts[:'password'].to_s.length < 10 if !opts[:'password'].nil? && opts[:'password'].to_s.length < 10
fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be great than or equal to 10.' fail ArgumentError, 'invalid value for "opts[:"password"]" when calling FakeApi.test_endpoint_parameters, the character length must be great than or equal to 10.'
end end

View File

@ -157,24 +157,89 @@ module Petstore
# @return Array for valid properies with the reasons # @return Array for valid properies with the reasons
def list_invalid_properties def list_invalid_properties
invalid_properties = Array.new invalid_properties = Array.new
if !@integer.nil? && @integer > 100.0
invalid_properties.push("invalid value for 'integer', must be smaller than or equal to 100.0.")
end
if !@integer.nil? && @integer < 10.0
invalid_properties.push("invalid value for 'integer', must be greater than or equal to 10.0.")
end
if !@int32.nil? && @int32 > 200.0
invalid_properties.push("invalid value for 'int32', must be smaller than or equal to 200.0.")
end
if !@int32.nil? && @int32 < 20.0
invalid_properties.push("invalid value for 'int32', must be greater than or equal to 20.0.")
end
if @number.nil?
invalid_properties.push("invalid value for 'number', number cannot be nil.")
end
if @number > 543.2
invalid_properties.push("invalid value for 'number', must be smaller than or equal to 543.2.")
end
if @number < 32.1
invalid_properties.push("invalid value for 'number', must be greater than or equal to 32.1.")
end
if !@float.nil? && @float > 987.6
invalid_properties.push("invalid value for 'float', must be smaller than or equal to 987.6.")
end
if !@float.nil? && @float < 54.3
invalid_properties.push("invalid value for 'float', must be greater than or equal to 54.3.")
end
if !@double.nil? && @double > 123.4
invalid_properties.push("invalid value for 'double', must be smaller than or equal to 123.4.")
end
if !@double.nil? && @double < 67.8
invalid_properties.push("invalid value for 'double', must be greater than or equal to 67.8.")
end
if !@string.nil? && @string !~ Regexp.new(/[a-z]/i)
invalid_properties.push("invalid value for 'string', must conform to the pattern /[a-z]/i.")
end
if @password.nil?
invalid_properties.push("invalid value for 'password', password cannot be nil.")
end
if @password.to_s.length > 64
invalid_properties.push("invalid value for 'password', the character length must be smaller than or equal to 64.")
end
if @password.to_s.length < 10
invalid_properties.push("invalid value for 'password', the character length must be great than or equal to 10.")
end
return invalid_properties return invalid_properties
end end
# Check to see if the all the properties in the model are valid # Check to see if the all the properties in the model are valid
# @return true if the model is valid # @return true if the model is valid
def valid? def valid?
return false if @integer > 100.0 return false if !@integer.nil? && @integer > 100.0
return false if @integer < 10.0 return false if !@integer.nil? && @integer < 10.0
return false if @int32 > 200.0 return false if !@int32.nil? && @int32 > 200.0
return false if @int32 < 20.0 return false if !@int32.nil? && @int32 < 20.0
return false if @number.nil? return false if @number.nil?
return false if @number > 543.2 return false if @number > 543.2
return false if @number < 32.1 return false if @number < 32.1
return false if @float > 987.6 return false if !@float.nil? && @float > 987.6
return false if @float < 54.3 return false if !@float.nil? && @float < 54.3
return false if @double > 123.4 return false if !@double.nil? && @double > 123.4
return false if @double < 67.8 return false if !@double.nil? && @double < 67.8
return false if @string !~ Regexp.new(/[a-z]/i) return false if !@string.nil? && @string !~ Regexp.new(/[a-z]/i)
return false if @byte.nil? return false if @byte.nil?
return false if @date.nil? return false if @date.nil?
return false if @password.nil? return false if @password.nil?
@ -186,15 +251,12 @@ module Petstore
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] integer Value to be assigned # @param [Object] integer Value to be assigned
def integer=(integer) def integer=(integer)
if integer.nil?
fail ArgumentError, "integer cannot be nil"
end
if integer > 100.0 if !integer.nil? && integer > 100.0
fail ArgumentError, "invalid value for 'integer', must be smaller than or equal to 100.0." fail ArgumentError, "invalid value for 'integer', must be smaller than or equal to 100.0."
end end
if integer < 10.0 if !integer.nil? && integer < 10.0
fail ArgumentError, "invalid value for 'integer', must be greater than or equal to 10.0." fail ArgumentError, "invalid value for 'integer', must be greater than or equal to 10.0."
end end
@ -204,15 +266,12 @@ module Petstore
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] int32 Value to be assigned # @param [Object] int32 Value to be assigned
def int32=(int32) def int32=(int32)
if int32.nil?
fail ArgumentError, "int32 cannot be nil"
end
if int32 > 200.0 if !int32.nil? && int32 > 200.0
fail ArgumentError, "invalid value for 'int32', must be smaller than or equal to 200.0." fail ArgumentError, "invalid value for 'int32', must be smaller than or equal to 200.0."
end end
if int32 < 20.0 if !int32.nil? && int32 < 20.0
fail ArgumentError, "invalid value for 'int32', must be greater than or equal to 20.0." fail ArgumentError, "invalid value for 'int32', must be greater than or equal to 20.0."
end end
@ -240,15 +299,12 @@ module Petstore
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] float Value to be assigned # @param [Object] float Value to be assigned
def float=(float) def float=(float)
if float.nil?
fail ArgumentError, "float cannot be nil"
end
if float > 987.6 if !float.nil? && float > 987.6
fail ArgumentError, "invalid value for 'float', must be smaller than or equal to 987.6." fail ArgumentError, "invalid value for 'float', must be smaller than or equal to 987.6."
end end
if float < 54.3 if !float.nil? && float < 54.3
fail ArgumentError, "invalid value for 'float', must be greater than or equal to 54.3." fail ArgumentError, "invalid value for 'float', must be greater than or equal to 54.3."
end end
@ -258,15 +314,12 @@ module Petstore
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] double Value to be assigned # @param [Object] double Value to be assigned
def double=(double) def double=(double)
if double.nil?
fail ArgumentError, "double cannot be nil"
end
if double > 123.4 if !double.nil? && double > 123.4
fail ArgumentError, "invalid value for 'double', must be smaller than or equal to 123.4." fail ArgumentError, "invalid value for 'double', must be smaller than or equal to 123.4."
end end
if double < 67.8 if !double.nil? && double < 67.8
fail ArgumentError, "invalid value for 'double', must be greater than or equal to 67.8." fail ArgumentError, "invalid value for 'double', must be greater than or equal to 67.8."
end end
@ -276,11 +329,8 @@ module Petstore
# Custom attribute writer method with validation # Custom attribute writer method with validation
# @param [Object] string Value to be assigned # @param [Object] string Value to be assigned
def string=(string) def string=(string)
if string.nil?
fail ArgumentError, "string cannot be nil"
end
if @string !~ Regexp.new(/[a-z]/i) if !string.nil? && string !~ Regexp.new(/[a-z]/i)
fail ArgumentError, "invalid value for 'string', must conform to the pattern /[a-z]/i." fail ArgumentError, "invalid value for 'string', must conform to the pattern /[a-z]/i."
end end