[ruby] Validate array correctly

It never throws error with any values in array. Every element should be validated against the enum.
This commit is contained in:
Hamed R. Nik 2016-11-03 17:02:45 +00:00
parent c2ca59e677
commit af4b32178f
2 changed files with 13 additions and 6 deletions

View File

@ -50,9 +50,16 @@ module {{moduleName}}
{{/required}} {{/required}}
{{^required}} {{^required}}
{{#isEnum}} {{#isEnum}}
{{#collectionFormat}}
if opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all?{|item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item)}
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
end
{{/collectionFormat}}
{{^collectionFormat}}
if opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}']) if opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}' fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
end end
{{/collectionFormat}}
{{/isEnum}} {{/isEnum}}
{{/required}} {{/required}}
{{#hasValidation}} {{#hasValidation}}

View File

@ -275,20 +275,20 @@ module Petstore
if @api_client.config.debugging if @api_client.config.debugging
@api_client.config.logger.debug "Calling API: FakeApi.test_enum_parameters ..." @api_client.config.logger.debug "Calling API: FakeApi.test_enum_parameters ..."
end end
if opts[:'enum_form_string_array'] && !['>', '$'].include?(opts[:'enum_form_string_array']) if opts[:'enum_form_string_array'] && !opts[:'enum_form_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_form_string_array", must be one of >, $' fail ArgumentError, 'invalid value for "enum_form_string_array", must include one of >, $'
end end
if opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string']) if opts[:'enum_form_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_form_string'])
fail ArgumentError, 'invalid value for "enum_form_string", must be one of _abc, -efg, (xyz)' fail ArgumentError, 'invalid value for "enum_form_string", must be one of _abc, -efg, (xyz)'
end end
if opts[:'enum_header_string_array'] && !['>', '$'].include?(opts[:'enum_header_string_array']) if opts[:'enum_header_string_array'] && !opts[:'enum_header_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_header_string_array", must be one of >, $' fail ArgumentError, 'invalid value for "enum_header_string_array", must include one of >, $'
end end
if opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string']) if opts[:'enum_header_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_header_string'])
fail ArgumentError, 'invalid value for "enum_header_string", must be one of _abc, -efg, (xyz)' fail ArgumentError, 'invalid value for "enum_header_string", must be one of _abc, -efg, (xyz)'
end end
if opts[:'enum_query_string_array'] && !['>', '$'].include?(opts[:'enum_query_string_array']) if opts[:'enum_query_string_array'] && !opts[:'enum_query_string_array'].all?{|item| ['>', '$'].include?(item)}
fail ArgumentError, 'invalid value for "enum_query_string_array", must be one of >, $' fail ArgumentError, 'invalid value for "enum_query_string_array", must include one of >, $'
end end
if opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string']) if opts[:'enum_query_string'] && !['_abc', '-efg', '(xyz)'].include?(opts[:'enum_query_string'])
fail ArgumentError, 'invalid value for "enum_query_string", must be one of _abc, -efg, (xyz)' fail ArgumentError, 'invalid value for "enum_query_string", must be one of _abc, -efg, (xyz)'