add more validation test for ruby model

This commit is contained in:
wing328 2016-04-25 17:36:32 +08:00
parent e143c6cd2f
commit ab986a7228
5 changed files with 74 additions and 2 deletions

View File

@ -66,15 +66,33 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
# Custom attribute writer method with validation
# @param [Object] {{{name}}} Value to be assigned
def {{{name}}}=({{{name}}})
if {{{name}}}.nil?
fail "{{{name}}} cannot be nil"
end
{{#minLength}}
if {{{name}}}.to_s.length > {{{maxLength}}}
fail "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}"
end
{{/minLength}}
{{#maxLength}}
if {{{name}}}.to_s.length < {{{minLength}}}
fail "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}"
end
{{/maxLength}}
{{#maximum}}
if {{{name}}} > {{{maximum}}}
fail "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}"
end
{{/maximum}}
{{#minimum}}
if {{{name}}} < {{{minimum}}}
fail "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}"
end
{{/minimum}}
@{{{name}}} = {{{name}}}
end

View File

@ -803,6 +803,8 @@ definitions:
password:
type: string
format: password
maxLength: 64
minLength: 10
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'

View File

@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/
- API version: 1.0.0
- Package version: 1.0.0
- Build date: 2016-04-25T16:31:40.260+08:00
- Build date: 2016-04-25T17:21:35.959+08:00
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
## Installation

View File

@ -127,69 +127,121 @@ module Petstore
# Custom attribute writer method with validation
# @param [Object] integer Value to be assigned
def integer=(integer)
if integer.nil?
fail "integer cannot be nil"
end
if integer > 100.0
fail "invalid value for 'integer', must be smaller than or equal to 100.0"
end
if integer < 10.0
fail "invalid value for 'integer', must be greater than or equal to 10.0"
end
@integer = integer
end
# Custom attribute writer method with validation
# @param [Object] int32 Value to be assigned
def int32=(int32)
if int32.nil?
fail "int32 cannot be nil"
end
if int32 > 200.0
fail "invalid value for 'int32', must be smaller than or equal to 200.0"
end
if int32 < 20.0
fail "invalid value for 'int32', must be greater than or equal to 20.0"
end
@int32 = int32
end
# Custom attribute writer method with validation
# @param [Object] number Value to be assigned
def number=(number)
if number.nil?
fail "number cannot be nil"
end
if number > 543.2
fail "invalid value for 'number', must be smaller than or equal to 543.2"
end
if number < 32.1
fail "invalid value for 'number', must be greater than or equal to 32.1"
end
@number = number
end
# Custom attribute writer method with validation
# @param [Object] float Value to be assigned
def float=(float)
if float.nil?
fail "float cannot be nil"
end
if float > 987.6
fail "invalid value for 'float', must be smaller than or equal to 987.6"
end
if float < 54.3
fail "invalid value for 'float', must be greater than or equal to 54.3"
end
@float = float
end
# Custom attribute writer method with validation
# @param [Object] double Value to be assigned
def double=(double)
if double.nil?
fail "double cannot be nil"
end
if double > 123.4
fail "invalid value for 'double', must be smaller than or equal to 123.4"
end
if double < 67.8
fail "invalid value for 'double', must be greater than or equal to 67.8"
end
@double = double
end
# Custom attribute writer method with validation
# @param [Object] string Value to be assigned
def string=(string)
if string.nil?
fail "string cannot be nil"
end
@string = string
end
# Custom attribute writer method with validation
# @param [Object] password Value to be assigned
def password=(password)
if password.nil?
fail "password cannot be nil"
end
if password.to_s.length > 64
fail "invalid value for 'password', the character length must be smaller than or equal to 64"
end
if password.to_s.length < 10
fail "invalid value for 'password', the character length must be great than or equal to 10"
end
@password = password
end
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)

View File

@ -206,7 +206,7 @@ describe Petstore::ApiClient do
end
it "fails for invalid collection format" do
proc { api_client.build_collection_param(param, :INVALID) }.should raise_error
proc { api_client.build_collection_param(param, :INVALID) }.should raise_error(RuntimeError, 'unknown collection format: :INVALID')
end
end