From e143c6cd2f2b1ff73f6a67fc3224ecbf6f05853e Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 17:07:42 +0800 Subject: [PATCH 1/6] add validation to ruby model --- .../io/swagger/codegen/CodegenProperty.java | 1 + .../io/swagger/codegen/DefaultCodegen.java | 26 ++++++-- .../src/main/resources/ruby/README.mustache | 1 + .../src/main/resources/ruby/model.mustache | 28 +++++++- ...ith-fake-endpoints-models-for-testing.yaml | 11 ++++ samples/client/petstore/ruby/Gemfile.lock | 34 +++++----- samples/client/petstore/ruby/README.md | 3 +- .../client/petstore/ruby/docs/FormatTest.md | 6 +- samples/client/petstore/ruby/docs/Name.md | 1 + .../ruby/lib/petstore/models/format_test.rb | 66 +++++++++++++++++++ .../petstore/ruby/lib/petstore/models/name.rb | 16 +++-- .../petstore/ruby/spec/models/name_spec.rb | 10 +++ 12 files changed, 172 insertions(+), 31 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java index a7045738cc3b..42f9d304a3ca 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java @@ -42,6 +42,7 @@ public class CodegenProperty { public Map allowableValues; public CodegenProperty items; public Map vendorExtensions; + public Boolean needValidation; // true if pattern, maximum, etc are set (only used in the mustache template) @Override public int hashCode() diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 76729c8b38a7..3e59dad1d4ff 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -327,6 +327,16 @@ public class DefaultCodegen { this.ensureUniqueParams = ensureUniqueParams; } + /** + * Return the JSON schema pattern (http://json-schema.org/latest/json-schema-validation.html#anchor33) + * + * @param pattern the pattern (regular expression) + * @return properly-escaped pattern + */ + public String toJSONSchemaPattern(String pattern) { + return escapeText(pattern); + } + /** * Return the file name of the Api Test * @@ -1094,6 +1104,10 @@ public class DefaultCodegen { property.exclusiveMinimum = np.getExclusiveMinimum(); property.exclusiveMaximum = np.getExclusiveMaximum(); + // check if any validation rule defined + if (property.minimum != null || property.maximum != null || property.exclusiveMinimum != null || property.exclusiveMaximum != null) + property.needValidation = true; + // legacy support Map allowableValues = new HashMap(); if (np.getMinimum() != null) { @@ -1111,7 +1125,12 @@ public class DefaultCodegen { StringProperty sp = (StringProperty) p; property.maxLength = sp.getMaxLength(); property.minLength = sp.getMinLength(); - property.pattern = sp.getPattern(); + property.pattern = toJSONSchemaPattern(sp.getPattern()); + + // check if any validation rule defined + if (property.pattern != null || property.minLength != null || property.maxLength != null) + property.needValidation = true; + property.isString = true; if (sp.getEnum() != null) { List _enum = sp.getEnum(); @@ -1802,9 +1821,6 @@ public class DefaultCodegen { if (model.complexType != null) { imports.add(model.complexType); } - p.maxLength = qp.getMaxLength(); - p.minLength = qp.getMinLength(); - p.pattern = qp.getPattern(); p.maximum = qp.getMaximum(); p.exclusiveMaximum = qp.isExclusiveMaximum(); @@ -1812,7 +1828,7 @@ public class DefaultCodegen { p.exclusiveMinimum = qp.isExclusiveMinimum(); p.maxLength = qp.getMaxLength(); p.minLength = qp.getMinLength(); - p.pattern = qp.getPattern(); + p.pattern = toJSONSchemaPattern(qp.getPattern()); p.maxItems = qp.getMaxItems(); p.minItems = qp.getMinItems(); p.uniqueItems = qp.isUniqueItems(); diff --git a/modules/swagger-codegen/src/main/resources/ruby/README.mustache b/modules/swagger-codegen/src/main/resources/ruby/README.mustache index 7f54eef9e69f..f4bafd9b0ea3 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/README.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/README.mustache @@ -31,6 +31,7 @@ Then either install the gem locally: ```shell gem install ./{{{gemName}}}-{{{gemVersion}}}.gem ``` +(for development, run `gem install --dev ./{{{gemName}}}-{{{gemVersion}}}.gem` to install the development dependencies) or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/). diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 5b7b617481f4..df23db195f3c 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -47,7 +47,9 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} end {{/vars}} end -{{#vars}}{{#isEnum}} + + {{#vars}} + {{#isEnum}} # Custom attribute writer method checking allowed values (enum). # @param [Object] {{{name}}} Object to be assigned def {{{name}}}=({{{name}}}) @@ -57,7 +59,29 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} end @{{{name}}} = {{{name}}} end -{{/isEnum}}{{/vars}} + + {{/isEnum}} + {{^isEnum}} + {{#needValidation}} + # Custom attribute writer method with validation + # @param [Object] {{{name}}} Value to be assigned + def {{{name}}}=({{{name}}}) + {{#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 + + {{/needValidation}} + {{/isEnum}} + {{/vars}} # Checks equality by comparing each attribute. # @param [Object] Object to be compared def ==(o) diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 2a90f5de0660..985b365802ba 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -761,22 +761,33 @@ definitions: properties: integer: type: integer + maximum: 100 + minimum: 10 int32: type: integer format: int32 + maximum: 200 + minimum: 20 int64: type: integer format: int64 number: + maximum: 543.2 + minimum: 32.1 type: number float: type: number format: float + maximum: 987.6 + minimum: 54.3 double: type: number format: double + maximum: 123.4 + minimum: 67.8 string: type: string + pattern: /[a‑z]/i byte: type: string format: byte diff --git a/samples/client/petstore/ruby/Gemfile.lock b/samples/client/petstore/ruby/Gemfile.lock index 98c02ee91636..cb8128052abe 100644 --- a/samples/client/petstore/ruby/Gemfile.lock +++ b/samples/client/petstore/ruby/Gemfile.lock @@ -22,29 +22,31 @@ GEM ethon (0.8.1) ffi (>= 1.3.0) ffi (1.9.8) + hashdiff (0.3.0) json (1.8.3) - rspec (3.2.0) - rspec-core (~> 3.2.0) - rspec-expectations (~> 3.2.0) - rspec-mocks (~> 3.2.0) - rspec-core (3.2.2) - rspec-support (~> 3.2.0) - rspec-expectations (3.2.0) + rspec (3.4.0) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.2.0) - rspec-mocks (3.2.1) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.2.0) - rspec-support (3.2.2) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) safe_yaml (1.0.4) sys-uname (0.9.2) ffi (>= 1.0.0) typhoeus (1.0.1) ethon (>= 0.8.0) - vcr (2.9.3) - webmock (1.21.0) + vcr (3.0.1) + webmock (1.24.5) addressable (>= 2.3.6) crack (>= 0.3.2) + hashdiff PLATFORMS ruby @@ -55,6 +57,6 @@ DEPENDENCIES autotest-growl (~> 0.2, >= 0.2.16) autotest-rails-pure (~> 4.1, >= 4.1.2) petstore! - rspec (~> 3.2, >= 3.2.0) - vcr (~> 2.9, >= 2.9.3) - webmock (~> 1.6, >= 1.6.2) + rspec (~> 3.4, >= 3.4.0) + vcr (~> 3.0, >= 3.0.1) + webmock (~> 1.24, >= 1.24.3) diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 4dc28289fa39..0d53e95030ad 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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-20T18:46:00.664+08:00 +- Build date: 2016-04-25T16:31:40.260+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation @@ -26,6 +26,7 @@ Then either install the gem locally: ```shell gem install ./petstore-1.0.0.gem ``` +(for development, run `gem install --dev ./petstore-1.0.0.gem` to install the development dependencies) or publish the gem to a gem hosting service, e.g. [RubyGems](https://rubygems.org/). diff --git a/samples/client/petstore/ruby/docs/FormatTest.md b/samples/client/petstore/ruby/docs/FormatTest.md index 2b64ed317b29..7197a7a6584f 100644 --- a/samples/client/petstore/ruby/docs/FormatTest.md +++ b/samples/client/petstore/ruby/docs/FormatTest.md @@ -10,10 +10,10 @@ Name | Type | Description | Notes **float** | **Float** | | [optional] **double** | **Float** | | [optional] **string** | **String** | | [optional] -**byte** | **String** | | [optional] +**byte** | **String** | | **binary** | **String** | | [optional] -**date** | **Date** | | [optional] +**date** | **Date** | | **date_time** | **DateTime** | | [optional] -**password** | **String** | | [optional] +**password** | **String** | | diff --git a/samples/client/petstore/ruby/docs/Name.md b/samples/client/petstore/ruby/docs/Name.md index 2864a67fbd3d..4858862c725c 100644 --- a/samples/client/petstore/ruby/docs/Name.md +++ b/samples/client/petstore/ruby/docs/Name.md @@ -5,5 +5,6 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **name** | **Integer** | | **snake_case** | **Integer** | | [optional] +**property** | **String** | | [optional] diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 87b6d4e4695c..9a6f8c19c9c5 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -124,6 +124,72 @@ module Petstore end end + # Custom attribute writer method with validation + # @param [Object] integer Value to be assigned + def integer=(integer) + 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 > 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 > 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 > 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 > 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) + @string = string + end + # Checks equality by comparing each attribute. # @param [Object] Object to be compared def ==(o) diff --git a/samples/client/petstore/ruby/lib/petstore/models/name.rb b/samples/client/petstore/ruby/lib/petstore/models/name.rb index 9818aa41cc49..c19beea86cbb 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/name.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/name.rb @@ -23,11 +23,14 @@ module Petstore attr_accessor :snake_case + attr_accessor :property + # Attribute mapping from ruby-style variable name to JSON key. def self.attribute_map { :'name' => :'name', - :'snake_case' => :'snake_case' + :'snake_case' => :'snake_case', + :'property' => :'property' } end @@ -35,7 +38,8 @@ module Petstore def self.swagger_types { :'name' => :'Integer', - :'snake_case' => :'Integer' + :'snake_case' => :'Integer', + :'property' => :'String' } end @@ -53,6 +57,9 @@ module Petstore if attributes[:'snake_case'] self.snake_case = attributes[:'snake_case'] end + if attributes[:'property'] + self.property = attributes[:'property'] + end end # Checks equality by comparing each attribute. @@ -61,7 +68,8 @@ module Petstore return true if self.equal?(o) self.class == o.class && name == o.name && - snake_case == o.snake_case + snake_case == o.snake_case && + property == o.property end # @see the `==` method @@ -73,7 +81,7 @@ module Petstore # Calculates hash code according to all attributes. # @return [Fixnum] Hash code def hash - [name, snake_case].hash + [name, snake_case, property].hash end # Builds the object from hash diff --git a/samples/client/petstore/ruby/spec/models/name_spec.rb b/samples/client/petstore/ruby/spec/models/name_spec.rb index 9eef65de2fa6..e4942b173b9f 100644 --- a/samples/client/petstore/ruby/spec/models/name_spec.rb +++ b/samples/client/petstore/ruby/spec/models/name_spec.rb @@ -56,5 +56,15 @@ describe 'Name' do end end + describe 'test attribute "property"' do + it 'should work' do + # assertion here + # should be_a() + # should be_nil + # should == + # should_not == + end + end + end From ab986a72286d8a6c0057811d67db6c311f9a455b Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 17:36:32 +0800 Subject: [PATCH 2/6] add more validation test for ruby model --- .../src/main/resources/ruby/model.mustache | 18 +++++++ ...ith-fake-endpoints-models-for-testing.yaml | 2 + samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/models/format_test.rb | 52 +++++++++++++++++++ .../petstore/ruby/spec/api_client_spec.rb | 2 +- 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index df23db195f3c..681f05341007 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -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 diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 985b365802ba..b5df273cfdbb 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -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' diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 0d53e95030ad..eaaf6890616e 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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 diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 9a6f8c19c9c5..6019d5a7996b 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -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) diff --git a/samples/client/petstore/ruby/spec/api_client_spec.rb b/samples/client/petstore/ruby/spec/api_client_spec.rb index a02d4a2d7607..3e57a4323917 100644 --- a/samples/client/petstore/ruby/spec/api_client_spec.rb +++ b/samples/client/petstore/ruby/spec/api_client_spec.rb @@ -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 From 3c36f1df379154a6e4d7e4a5eb4d53a4ac2d9ad0 Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 17:45:32 +0800 Subject: [PATCH 3/6] use ArgumentError in ruby model --- .../src/main/resources/ruby/model.mustache | 12 +++--- samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/models/format_test.rb | 38 +++++++++---------- .../ruby/lib/petstore/models/order.rb | 2 +- .../petstore/ruby/lib/petstore/models/pet.rb | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 681f05341007..df3dba8cf47b 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -55,7 +55,7 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} def {{{name}}}=({{{name}}}) allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] if {{{name}}} && !allowed_values.include?({{{name}}}) - fail "invalid value for '{{{name}}}', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}" end @{{{name}}} = {{{name}}} end @@ -67,30 +67,30 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} # @param [Object] {{{name}}} Value to be assigned def {{{name}}}=({{{name}}}) if {{{name}}}.nil? - fail "{{{name}}} cannot be nil" + fail ArgumentError, "{{{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}}}" + fail ArgumentError, "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}}}" + fail ArgumentError, "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}}}" + fail ArgumentError, "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}}}" + fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}" end {{/minimum}} diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index eaaf6890616e..15ba952dc79a 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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-25T17:21:35.959+08:00 +- Build date: 2016-04-25T17:38:59.414+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 6019d5a7996b..f15a8abab03c 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -128,15 +128,15 @@ module Petstore # @param [Object] integer Value to be assigned def integer=(integer) if integer.nil? - fail "integer cannot be nil" + fail ArgumentError, "integer cannot be nil" end if integer > 100.0 - fail "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 if integer < 10.0 - fail "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 @integer = integer @@ -146,15 +146,15 @@ module Petstore # @param [Object] int32 Value to be assigned def int32=(int32) if int32.nil? - fail "int32 cannot be nil" + fail ArgumentError, "int32 cannot be nil" end if int32 > 200.0 - fail "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 if int32 < 20.0 - fail "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 @int32 = int32 @@ -164,15 +164,15 @@ module Petstore # @param [Object] number Value to be assigned def number=(number) if number.nil? - fail "number cannot be nil" + fail ArgumentError, "number cannot be nil" end if number > 543.2 - fail "invalid value for 'number', must be smaller than or equal to 543.2" + fail ArgumentError, "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" + fail ArgumentError, "invalid value for 'number', must be greater than or equal to 32.1" end @number = number @@ -182,15 +182,15 @@ module Petstore # @param [Object] float Value to be assigned def float=(float) if float.nil? - fail "float cannot be nil" + fail ArgumentError, "float cannot be nil" end if float > 987.6 - fail "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 if float < 54.3 - fail "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 @float = float @@ -200,15 +200,15 @@ module Petstore # @param [Object] double Value to be assigned def double=(double) if double.nil? - fail "double cannot be nil" + fail ArgumentError, "double cannot be nil" end if double > 123.4 - fail "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 if double < 67.8 - fail "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 @double = double @@ -218,7 +218,7 @@ module Petstore # @param [Object] string Value to be assigned def string=(string) if string.nil? - fail "string cannot be nil" + fail ArgumentError, "string cannot be nil" end @string = string @@ -228,15 +228,15 @@ module Petstore # @param [Object] password Value to be assigned def password=(password) if password.nil? - fail "password cannot be nil" + fail ArgumentError, "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" + fail ArgumentError, "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" + fail ArgumentError, "invalid value for 'password', the character length must be great than or equal to 10" end @password = password diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index 81f2601e9d93..a7107947d7b6 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -90,7 +90,7 @@ module Petstore def status=(status) allowed_values = ["placed", "approved", "delivered"] if status && !allowed_values.include?(status) - fail "invalid value for 'status', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}" end @status = status end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index 095f91ca7c35..70ad05b780e9 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -92,7 +92,7 @@ module Petstore def status=(status) allowed_values = ["available", "pending", "sold"] if status && !allowed_values.include?(status) - fail "invalid value for 'status', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}" end @status = status end From e17a6205067e9bc127b13ece298826a01fe9cc57 Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 19:18:05 +0800 Subject: [PATCH 4/6] add methods to validate the ruby object --- .../io/swagger/codegen/CodegenProperty.java | 2 +- .../io/swagger/codegen/DefaultCodegen.java | 4 +- .../src/main/resources/ruby/model.mustache | 117 ++++++++++++++++-- samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/models/animal.rb | 17 +++ .../ruby/lib/petstore/models/api_response.rb | 15 +++ .../petstore/ruby/lib/petstore/models/cat.rb | 18 +++ .../ruby/lib/petstore/models/category.rb | 14 +++ .../petstore/ruby/lib/petstore/models/dog.rb | 18 +++ .../ruby/lib/petstore/models/format_test.rb | 112 +++++++++++++++-- .../lib/petstore/models/model_200_response.rb | 13 ++ .../ruby/lib/petstore/models/model_return.rb | 13 ++ .../petstore/ruby/lib/petstore/models/name.rb | 19 +++ .../ruby/lib/petstore/models/order.rb | 24 +++- .../petstore/ruby/lib/petstore/models/pet.rb | 32 ++++- .../lib/petstore/models/special_model_name.rb | 13 ++ .../petstore/ruby/lib/petstore/models/tag.rb | 14 +++ .../petstore/ruby/lib/petstore/models/user.rb | 20 +++ 18 files changed, 439 insertions(+), 28 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java index 42f9d304a3ca..199d51ce19e0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java @@ -42,7 +42,7 @@ public class CodegenProperty { public Map allowableValues; public CodegenProperty items; public Map vendorExtensions; - public Boolean needValidation; // true if pattern, maximum, etc are set (only used in the mustache template) + public Boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template) @Override public int hashCode() diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 3e59dad1d4ff..7e6c43374ac0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1106,7 +1106,7 @@ public class DefaultCodegen { // check if any validation rule defined if (property.minimum != null || property.maximum != null || property.exclusiveMinimum != null || property.exclusiveMaximum != null) - property.needValidation = true; + property.hasValidation = true; // legacy support Map allowableValues = new HashMap(); @@ -1129,7 +1129,7 @@ public class DefaultCodegen { // check if any validation rule defined if (property.pattern != null || property.minLength != null || property.maxLength != null) - property.needValidation = true; + property.hasValidation = true; property.isString = true; if (sp.getEnum() != null) { diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index df3dba8cf47b..337845754a79 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -39,12 +39,109 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} {{#vars}} if attributes[:'{{{baseName}}}'] - {{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array) + {{#isContainer}} + if (value = attributes[:'{{{baseName}}}']).is_a?(Array) self.{{{name}}} = value - end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}}{{#defaultValue}} + end + {{/isContainer}} + {{^isContainer}} + self.{{{name}}} = attributes[:'{{{baseName}}}'] + {{/isContainer}} + {{#defaultValue}} else - self.{{{name}}} = {{{defaultValue}}}{{/defaultValue}} + self.{{{name}}} = {{{defaultValue}}} + {{/defaultValue}} end + + {{/vars}} + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + {{#isEnum}} + allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] + if {{{name}}} && !allowed_values.include?({{{name}}}) + invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.") + end + + {{/isEnum}} + {{#hasValidation}} + if {{{name}}}.nil? + fail ArgumentError, "{{{name}}} cannot be nil" + end + + {{#minLength}} + if {{{name}}}.to_s.length > {{{maxLength}}} + invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.") + end + + {{/minLength}} + {{#maxLength}} + if {{{name}}}.to_s.length < {{{minLength}}} + invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.") + end + + {{/maxLength}} + {{#maximum}} + if {{{name}}} > {{{maximum}}} + invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.") + end + + {{/maximum}} + {{#minimum}} + if {{{name}}} < {{{minimum}}} + invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.") + end + + {{/minimum}} + {{/hasValidation}} + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + {{#vars}} + {{#required}} + if @{{{name}}}.nil? + return false + end + + {{/required}} + {{#isEnum}} + allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] + if {{{name}}} && !allowed_values.include?({{{name}}}) + return false + end + {{/isEnum}} + {{#hasValidation}} + {{#minLength}} + if {{{name}}}.to_s.length > {{{maxLength}}} + return false + end + + {{/minLength}} + {{#maxLength}} + if {{{name}}}.to_s.length < {{{minLength}}} + return false + end + + {{/maxLength}} + {{#maximum}} + if {{{name}}} > {{{maximum}}} + return false + end + + {{/maximum}} + {{#minimum}} + if {{{name}}} < {{{minimum}}} + return false + end + + {{/minimum}} + {{/hasValidation}} {{/vars}} end @@ -55,14 +152,14 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} def {{{name}}}=({{{name}}}) allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] if {{{name}}} && !allowed_values.include?({{{name}}}) - fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}." end @{{{name}}} = {{{name}}} end {{/isEnum}} {{^isEnum}} - {{#needValidation}} + {{#hasValidation}} # Custom attribute writer method with validation # @param [Object] {{{name}}} Value to be assigned def {{{name}}}=({{{name}}}) @@ -72,32 +169,32 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} {{#minLength}} if {{{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 {{/minLength}} {{#maxLength}} if {{{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 {{/maxLength}} {{#maximum}} if {{{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 {{/maximum}} {{#minimum}} if {{{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 {{/minimum}} @{{{name}}} = {{{name}}} end - {{/needValidation}} + {{/hasValidation}} {{/isEnum}} {{/vars}} # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 15ba952dc79a..96231298d8b4 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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-25T17:38:59.414+08:00 +- Build date: 2016-04-25T19:16:37.992+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation diff --git a/samples/client/petstore/ruby/lib/petstore/models/animal.rb b/samples/client/petstore/ruby/lib/petstore/models/animal.rb index 342d3b2464d9..2f26f9b4bb0e 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/animal.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/animal.rb @@ -45,6 +45,23 @@ module Petstore if attributes[:'className'] self.class_name = attributes[:'className'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if @class_name.nil? + return false + end + end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/api_response.rb b/samples/client/petstore/ruby/lib/petstore/models/api_response.rb index 683083be4b2c..da0418eda508 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/api_response.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/api_response.rb @@ -53,12 +53,27 @@ module Petstore if attributes[:'code'] self.code = attributes[:'code'] end + if attributes[:'type'] self.type = attributes[:'type'] end + if attributes[:'message'] self.message = attributes[:'message'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/cat.rb b/samples/client/petstore/ruby/lib/petstore/models/cat.rb index 4489e6abb85f..0f8c34f0896e 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/cat.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/cat.rb @@ -49,9 +49,27 @@ module Petstore if attributes[:'className'] self.class_name = attributes[:'className'] end + if attributes[:'declawed'] self.declawed = attributes[:'declawed'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if @class_name.nil? + return false + end + end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/category.rb b/samples/client/petstore/ruby/lib/petstore/models/category.rb index 0f6d61d83afa..33e4a539fb3e 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/category.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/category.rb @@ -49,9 +49,23 @@ module Petstore if attributes[:'id'] self.id = attributes[:'id'] end + if attributes[:'name'] self.name = attributes[:'name'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/dog.rb b/samples/client/petstore/ruby/lib/petstore/models/dog.rb index 5173f9a01ec3..66fd396e753b 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/dog.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/dog.rb @@ -49,9 +49,27 @@ module Petstore if attributes[:'className'] self.class_name = attributes[:'className'] end + if attributes[:'breed'] self.breed = attributes[:'breed'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if @class_name.nil? + return false + end + end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index f15a8abab03c..79bcce513d82 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -89,39 +89,127 @@ module Petstore if attributes[:'integer'] self.integer = attributes[:'integer'] end + if attributes[:'int32'] self.int32 = attributes[:'int32'] end + if attributes[:'int64'] self.int64 = attributes[:'int64'] end + if attributes[:'number'] self.number = attributes[:'number'] end + if attributes[:'float'] self.float = attributes[:'float'] end + if attributes[:'double'] self.double = attributes[:'double'] end + if attributes[:'string'] self.string = attributes[:'string'] end + if attributes[:'byte'] self.byte = attributes[:'byte'] end + if attributes[:'binary'] self.binary = attributes[:'binary'] end + if attributes[:'date'] self.date = attributes[:'date'] end + if attributes[:'dateTime'] self.date_time = attributes[:'dateTime'] end + if attributes[:'password'] self.password = attributes[:'password'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if integer > 100.0 + return false + end + + if integer < 10.0 + return false + end + + if int32 > 200.0 + return false + end + + if int32 < 20.0 + return false + end + + if @number.nil? + return false + end + + if number > 543.2 + return false + end + + if number < 32.1 + return false + end + + if float > 987.6 + return false + end + + if float < 54.3 + return false + end + + if double > 123.4 + return false + end + + if double < 67.8 + return false + end + + if @byte.nil? + return false + end + + if @date.nil? + return false + end + + if @password.nil? + return false + end + + if password.to_s.length > 64 + return false + end + + if password.to_s.length < 10 + return false + end + end # Custom attribute writer method with validation @@ -132,11 +220,11 @@ module Petstore end if 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 if 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 @integer = integer @@ -150,11 +238,11 @@ module Petstore end if 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 if 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 @int32 = int32 @@ -168,11 +256,11 @@ module Petstore end if number > 543.2 - fail ArgumentError, "invalid value for 'number', must be smaller than or equal to 543.2" + fail ArgumentError, "invalid value for 'number', must be smaller than or equal to 543.2." end if number < 32.1 - fail ArgumentError, "invalid value for 'number', must be greater than or equal to 32.1" + fail ArgumentError, "invalid value for 'number', must be greater than or equal to 32.1." end @number = number @@ -186,11 +274,11 @@ module Petstore end if 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 if 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 @float = float @@ -204,11 +292,11 @@ module Petstore end if 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 if 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 @double = double @@ -232,11 +320,11 @@ module Petstore end if password.to_s.length > 64 - fail ArgumentError, "invalid value for 'password', the character length must be smaller than or equal to 64" + fail ArgumentError, "invalid value for 'password', the character length must be smaller than or equal to 64." end if password.to_s.length < 10 - fail ArgumentError, "invalid value for 'password', the character length must be great than or equal to 10" + fail ArgumentError, "invalid value for 'password', the character length must be great than or equal to 10." end @password = password diff --git a/samples/client/petstore/ruby/lib/petstore/models/model_200_response.rb b/samples/client/petstore/ruby/lib/petstore/models/model_200_response.rb index 9cacbdb1298d..71b4501d99e8 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/model_200_response.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/model_200_response.rb @@ -46,6 +46,19 @@ module Petstore if attributes[:'name'] self.name = attributes[:'name'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/model_return.rb b/samples/client/petstore/ruby/lib/petstore/models/model_return.rb index 31adf497fa21..bb015662bc48 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/model_return.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/model_return.rb @@ -46,6 +46,19 @@ module Petstore if attributes[:'return'] self._return = attributes[:'return'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/name.rb b/samples/client/petstore/ruby/lib/petstore/models/name.rb index c19beea86cbb..bec29f9c69eb 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/name.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/name.rb @@ -54,12 +54,31 @@ module Petstore if attributes[:'name'] self.name = attributes[:'name'] end + if attributes[:'snake_case'] self.snake_case = attributes[:'snake_case'] end + if attributes[:'property'] self.property = attributes[:'property'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if @name.nil? + return false + end + end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index a7107947d7b6..823ad3d9f60d 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -66,23 +66,45 @@ module Petstore if attributes[:'id'] self.id = attributes[:'id'] end + if attributes[:'petId'] self.pet_id = attributes[:'petId'] end + if attributes[:'quantity'] self.quantity = attributes[:'quantity'] end + if attributes[:'shipDate'] self.ship_date = attributes[:'shipDate'] end + if attributes[:'status'] self.status = attributes[:'status'] end + if attributes[:'complete'] self.complete = attributes[:'complete'] else self.complete = false end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + allowed_values = ["placed", "approved", "delivered"] + if status && !allowed_values.include?(status) + return false + end end # Custom attribute writer method checking allowed values (enum). @@ -90,7 +112,7 @@ module Petstore def status=(status) allowed_values = ["placed", "approved", "delivered"] if status && !allowed_values.include?(status) - fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}." end @status = status end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index 70ad05b780e9..898a29a4d285 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -66,25 +66,55 @@ module Petstore if attributes[:'id'] self.id = attributes[:'id'] end + if attributes[:'category'] self.category = attributes[:'category'] end + if attributes[:'name'] self.name = attributes[:'name'] end + if attributes[:'photoUrls'] if (value = attributes[:'photoUrls']).is_a?(Array) self.photo_urls = value end end + if attributes[:'tags'] if (value = attributes[:'tags']).is_a?(Array) self.tags = value end end + if attributes[:'status'] self.status = attributes[:'status'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + if @name.nil? + return false + end + + if @photo_urls.nil? + return false + end + + allowed_values = ["available", "pending", "sold"] + if status && !allowed_values.include?(status) + return false + end end # Custom attribute writer method checking allowed values (enum). @@ -92,7 +122,7 @@ module Petstore def status=(status) allowed_values = ["available", "pending", "sold"] if status && !allowed_values.include?(status) - fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}" + fail ArgumentError, "invalid value for 'status', must be one of #{allowed_values}." end @status = status end diff --git a/samples/client/petstore/ruby/lib/petstore/models/special_model_name.rb b/samples/client/petstore/ruby/lib/petstore/models/special_model_name.rb index 4c859270d1be..94aa62981aab 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/special_model_name.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/special_model_name.rb @@ -45,6 +45,19 @@ module Petstore if attributes[:'$special[property.name]'] self.special_property_name = attributes[:'$special[property.name]'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/tag.rb b/samples/client/petstore/ruby/lib/petstore/models/tag.rb index e5c80ee22285..3ef49f71eaa0 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/tag.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/tag.rb @@ -49,9 +49,23 @@ module Petstore if attributes[:'id'] self.id = attributes[:'id'] end + if attributes[:'name'] self.name = attributes[:'name'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. diff --git a/samples/client/petstore/ruby/lib/petstore/models/user.rb b/samples/client/petstore/ruby/lib/petstore/models/user.rb index e7ce160dfa24..b9e7b91b08bb 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/user.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/user.rb @@ -74,27 +74,47 @@ module Petstore if attributes[:'id'] self.id = attributes[:'id'] end + if attributes[:'username'] self.username = attributes[:'username'] end + if attributes[:'firstName'] self.first_name = attributes[:'firstName'] end + if attributes[:'lastName'] self.last_name = attributes[:'lastName'] end + if attributes[:'email'] self.email = attributes[:'email'] end + if attributes[:'password'] self.password = attributes[:'password'] end + if attributes[:'phone'] self.phone = attributes[:'phone'] end + if attributes[:'userStatus'] self.user_status = attributes[:'userStatus'] end + + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properies with the reasons + def list_invalid_properties + invalid_properties = Array.new + return invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? end # Checks equality by comparing each attribute. From 0e58265eb5a70cd966c269a45acf80f921579baa Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 21:52:37 +0800 Subject: [PATCH 5/6] use instance variable in validation rule --- .../src/main/resources/ruby/model.mustache | 24 +++++++++---------- samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/models/format_test.rb | 24 +++++++++---------- .../ruby/lib/petstore/models/order.rb | 2 +- .../petstore/ruby/lib/petstore/models/pet.rb | 2 +- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 337845754a79..e3b5c3851d22 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -62,36 +62,36 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} invalid_properties = Array.new {{#isEnum}} allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] - if {{{name}}} && !allowed_values.include?({{{name}}}) + if @{{{name}}} && !allowed_values.include?(@{{{name}}}) invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.") end {{/isEnum}} {{#hasValidation}} - if {{{name}}}.nil? + if @{{{name}}}.nil? fail ArgumentError, "{{{name}}} cannot be nil" end {{#minLength}} - if {{{name}}}.to_s.length > {{{maxLength}}} + if @{{{name}}}.to_s.length > {{{maxLength}}} invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.") end {{/minLength}} {{#maxLength}} - if {{{name}}}.to_s.length < {{{minLength}}} + if @{{{name}}}.to_s.length < {{{minLength}}} invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.") end {{/maxLength}} {{#maximum}} - if {{{name}}} > {{{maximum}}} + if @{{{name}}} > {{{maximum}}} invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.") end {{/maximum}} {{#minimum}} - if {{{name}}} < {{{minimum}}} + if @{{{name}}} < {{{minimum}}} invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.") end @@ -112,31 +112,31 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} {{/required}} {{#isEnum}} allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] - if {{{name}}} && !allowed_values.include?({{{name}}}) + if @{{{name}}} && !allowed_values.include?(@{{{name}}}) return false end {{/isEnum}} {{#hasValidation}} {{#minLength}} - if {{{name}}}.to_s.length > {{{maxLength}}} + if @{{{name}}}.to_s.length > {{{maxLength}}} return false end {{/minLength}} {{#maxLength}} - if {{{name}}}.to_s.length < {{{minLength}}} + if @{{{name}}}.to_s.length < {{{minLength}}} return false end {{/maxLength}} {{#maximum}} - if {{{name}}} > {{{maximum}}} + if @{{{name}}} > {{{maximum}}} return false end {{/maximum}} {{#minimum}} - if {{{name}}} < {{{minimum}}} + if @{{{name}}} < {{{minimum}}} return false end @@ -151,7 +151,7 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} # @param [Object] {{{name}}} Object to be assigned def {{{name}}}=({{{name}}}) allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] - if {{{name}}} && !allowed_values.include?({{{name}}}) + if {{{name}}} && !allowed_values.include?(@{{{name}}}) fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}." end @{{{name}}} = {{{name}}} diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 96231298d8b4..a7dd757d991d 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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-25T19:16:37.992+08:00 +- Build date: 2016-04-25T21:47:45.004+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 79bcce513d82..02a996abe509 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -146,19 +146,19 @@ module Petstore # Check to see if the all the properties in the model are valid # @return true if the model is valid def valid? - if integer > 100.0 + if @integer > 100.0 return false end - if integer < 10.0 + if @integer < 10.0 return false end - if int32 > 200.0 + if @int32 > 200.0 return false end - if int32 < 20.0 + if @int32 < 20.0 return false end @@ -166,27 +166,27 @@ module Petstore return false end - if number > 543.2 + if @number > 543.2 return false end - if number < 32.1 + if @number < 32.1 return false end - if float > 987.6 + if @float > 987.6 return false end - if float < 54.3 + if @float < 54.3 return false end - if double > 123.4 + if @double > 123.4 return false end - if double < 67.8 + if @double < 67.8 return false end @@ -202,11 +202,11 @@ module Petstore return false end - if password.to_s.length > 64 + if @password.to_s.length > 64 return false end - if password.to_s.length < 10 + if @password.to_s.length < 10 return false end diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index 823ad3d9f60d..1ae2a898024b 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -102,7 +102,7 @@ module Petstore # @return true if the model is valid def valid? allowed_values = ["placed", "approved", "delivered"] - if status && !allowed_values.include?(status) + if @status && !allowed_values.include?(status) return false end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index 898a29a4d285..abd1252a9350 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -112,7 +112,7 @@ module Petstore end allowed_values = ["available", "pending", "sold"] - if status && !allowed_values.include?(status) + if @status && !allowed_values.include?(status) return false end end From 4854b79a318baab025f97c57fa4e9b9de153cf42 Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 25 Apr 2016 22:23:23 +0800 Subject: [PATCH 6/6] add pattern check to ruby model --- .../src/main/resources/ruby/model.mustache | 22 +++++++++++++++++-- ...ith-fake-endpoints-models-for-testing.yaml | 2 +- samples/client/petstore/ruby/README.md | 2 +- .../ruby/lib/petstore/models/format_test.rb | 8 +++++++ .../ruby/lib/petstore/models/order.rb | 2 +- .../petstore/ruby/lib/petstore/models/pet.rb | 2 +- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index e3b5c3851d22..b95b55bcee93 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -62,7 +62,7 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} invalid_properties = Array.new {{#isEnum}} allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] - if @{{{name}}} && !allowed_values.include?(@{{{name}}}) + if @{{{name}}} && !allowed_values.include?({{{name}}}) invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.") end @@ -96,6 +96,12 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} end {{/minimum}} + {{#pattern}} + if @{{{name}}} !~ Regexp.new({{{pattern}}}) + invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.") + end + + {{/pattern}} {{/hasValidation}} return invalid_properties end @@ -141,6 +147,12 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} end {{/minimum}} + {{#pattern}} + if @{{{name}}} !~ Regexp.new({{{pattern}}}) + return false + end + + {{/pattern}} {{/hasValidation}} {{/vars}} end @@ -151,7 +163,7 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} # @param [Object] {{{name}}} Object to be assigned def {{{name}}}=({{{name}}}) allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] - if {{{name}}} && !allowed_values.include?(@{{{name}}}) + if {{{name}}} && !allowed_values.include?({{{name}}}) fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}." end @{{{name}}} = {{{name}}} @@ -191,6 +203,12 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}} end {{/minimum}} + {{#pattern}} + if @{{{name}}} !~ Regexp.new({{{pattern}}}) + fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}." + end + + {{/pattern}} @{{{name}}} = {{{name}}} end diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index b5df273cfdbb..d77eaac6efd0 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -787,7 +787,7 @@ definitions: minimum: 67.8 string: type: string - pattern: /[a‑z]/i + pattern: /[a-z]/i byte: type: string format: byte diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index a7dd757d991d..9882a3021ec7 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -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-25T21:47:45.004+08:00 +- Build date: 2016-04-25T22:22:56.750+08:00 - Build package: class io.swagger.codegen.languages.RubyClientCodegen ## Installation diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 02a996abe509..e4373bb955f1 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -190,6 +190,10 @@ module Petstore return false end + if @string !~ Regexp.new(/[a-z]/i) + return false + end + if @byte.nil? return false end @@ -309,6 +313,10 @@ module Petstore fail ArgumentError, "string cannot be nil" end + if @string !~ Regexp.new(/[a-z]/i) + fail ArgumentError, "invalid value for 'string', must conform to the pattern /[a-z]/i." + end + @string = string end diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index 1ae2a898024b..b243b70cfb87 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -102,7 +102,7 @@ module Petstore # @return true if the model is valid def valid? allowed_values = ["placed", "approved", "delivered"] - if @status && !allowed_values.include?(status) + if @status && !allowed_values.include?(@status) return false end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index abd1252a9350..cdd312f1dad6 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -112,7 +112,7 @@ module Petstore end allowed_values = ["available", "pending", "sold"] - if @status && !allowed_values.include?(status) + if @status && !allowed_values.include?(@status) return false end end