Merge branch 'validation' of https://github.com/scottrw93/swagger-codegen into scottrw93-validation

This commit is contained in:
wing328
2016-05-09 10:51:23 +08:00
23 changed files with 285 additions and 6 deletions

View File

@@ -21,6 +21,7 @@ from __future__ import absolute_import
import sys
import os
import re
# python 2 and python 3 compatibility library
from six import iteritems
@@ -94,6 +95,31 @@ class {{classname}}(object):
if ('{{paramName}}' not in params) or (params['{{paramName}}'] is None):
raise ValueError("Missing the required parameter `{{paramName}}` when calling `{{operationId}}`")
{{/required}}
{{/allParams}}
{{#allParams}}
{{#hasValidation}}
{{#maxLength}}
if '{{paramName}}' in params and len(params['{{paramName}}']) > {{maxLength}}:
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`")
{{/maxLength}}
{{#minLength}}
if '{{paramName}}' in params and len(params['{{paramName}}']) < {{minLength}}:
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`")
{{/minLength}}
{{#maximum}}
if '{{paramName}}' in params and params['{{paramName}}'] > {{maximum}}:
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than or equal to `{{maximum}}`")
{{/maximum}}
{{#minimum}}
if '{{paramName}}' in params and params['{{paramName}}'] < {{minimum}}:
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than or equal to `{{minimum}}`")
{{/minimum}}
{{#pattern}}
if '{{paramName}}' in params and not re.search('{{vendorExtensions.x-regex}}', params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}):
raise ValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{pattern}}`")
{{/pattern}}
{{/hasValidation}}
{{/allParams}}
resource_path = '{{path}}'.replace('{format}', 'json')

View File

@@ -22,6 +22,7 @@ Copyright 2016 SmartBear Software
{{#model}}
from pprint import pformat
from six import iteritems
import re
class {{classname}}(object):
@@ -79,7 +80,36 @@ class {{classname}}(object):
"Invalid value for `{{name}}`, must be one of {0}"
.format(allowed_values)
)
{{/isEnum}}self._{{name}} = {{name}}
{{/isEnum}}
{{^isEnum}}
{{#hasValidation}}
if not {{name}}:
raise ValueError("Invalid value for `{{name}}`, must not be `None`")
{{#maxLength}}
if len({{name}}) > {{maxLength}}:
raise ValueError("Invalid value for `{{name}}`, length must be less than `{{maxLength}}`")
{{/maxLength}}
{{#minLength}}
if len({{name}}) < {{minLength}}:
raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`")
{{/minLength}}
{{#maximum}}
if {{name}} > {{maximum}}:
raise ValueError("Invalid value for `{{name}}`, must be a value less than or equal to `{{maximum}}`")
{{/maximum}}
{{#minimum}}
if {{name}} < {{minimum}}:
raise ValueError("Invalid value for `{{name}}`, must be a value greater than or equal to `{{minimum}}`")
{{/minimum}}
{{#pattern}}
if not re.search('{{vendorExtensions.x-regex}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}):
raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{pattern}}`")
{{/pattern}}
{{/hasValidation}}
{{/isEnum}}
self._{{name}} = {{name}}
{{/vars}}
def to_dict(self):