Merge pull request #3944 from wing328/python_fix_required_check

[Python] fix python required property check and validation for optional properties
This commit is contained in:
wing328 2016-10-07 17:25:51 +08:00 committed by GitHub
commit 04070b32c1
7 changed files with 39 additions and 23 deletions

View File

@ -88,37 +88,37 @@ class {{classname}}(object):
{{/isContainer}} {{/isContainer}}
{{/isEnum}} {{/isEnum}}
{{^isEnum}} {{^isEnum}}
{{#hasValidation}}
{{#required}} {{#required}}
if {{name}} is None: if {{name}} is None:
raise ValueError("Invalid value for `{{name}}`, must not be `None`") raise ValueError("Invalid value for `{{name}}`, must not be `None`")
{{/required}} {{/required}}
{{#hasValidation}}
{{#maxLength}} {{#maxLength}}
if len({{name}}) > {{maxLength}}: if {{name}} is not None and len({{name}}) > {{maxLength}}:
raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`")
{{/maxLength}} {{/maxLength}}
{{#minLength}} {{#minLength}}
if len({{name}}) < {{minLength}}: if {{name}} is not None and len({{name}}) < {{minLength}}:
raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`")
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
if {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}:
raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`")
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
if {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: if {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}:
raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`")
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
if not re.search('{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): if {{name}} is not None and 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}}}`") raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`")
{{/pattern}} {{/pattern}}
{{#maxItems}} {{#maxItems}}
if len({{name}}) > {{maxItems}}: if {{name}} is not None and len({{name}}) > {{maxItems}}:
raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`") raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`")
{{/maxItems}} {{/maxItems}}
{{#minItems}} {{#minItems}}
if len({{name}}) < {{minItems}}: if {{name}} is not None and len({{name}}) < {{minItems}}:
raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`") raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`")
{{/minItems}} {{/minItems}}
{{/hasValidation}} {{/hasValidation}}

View File

@ -75,6 +75,8 @@ class Animal(object):
:param class_name: The class_name of this Animal. :param class_name: The class_name of this Animal.
:type: str :type: str
""" """
if class_name is None:
raise ValueError("Invalid value for `class_name`, must not be `None`")
self._class_name = class_name self._class_name = class_name

View File

@ -78,6 +78,8 @@ class Cat(object):
:param class_name: The class_name of this Cat. :param class_name: The class_name of this Cat.
:type: str :type: str
""" """
if class_name is None:
raise ValueError("Invalid value for `class_name`, must not be `None`")
self._class_name = class_name self._class_name = class_name

View File

@ -78,6 +78,8 @@ class Dog(object):
:param class_name: The class_name of this Dog. :param class_name: The class_name of this Dog.
:type: str :type: str
""" """
if class_name is None:
raise ValueError("Invalid value for `class_name`, must not be `None`")
self._class_name = class_name self._class_name = class_name

View File

@ -108,9 +108,9 @@ class FormatTest(object):
:param integer: The integer of this FormatTest. :param integer: The integer of this FormatTest.
:type: int :type: int
""" """
if integer > 100.0: if integer is not None and integer > 100.0:
raise ValueError("Invalid value for `integer`, must be a value less than or equal to `100.0`") raise ValueError("Invalid value for `integer`, must be a value less than or equal to `100.0`")
if integer < 10.0: if integer is not None and integer < 10.0:
raise ValueError("Invalid value for `integer`, must be a value greater than or equal to `10.0`") raise ValueError("Invalid value for `integer`, must be a value greater than or equal to `10.0`")
self._integer = integer self._integer = integer
@ -135,9 +135,9 @@ class FormatTest(object):
:param int32: The int32 of this FormatTest. :param int32: The int32 of this FormatTest.
:type: int :type: int
""" """
if int32 > 200.0: if int32 is not None and int32 > 200.0:
raise ValueError("Invalid value for `int32`, must be a value less than or equal to `200.0`") raise ValueError("Invalid value for `int32`, must be a value less than or equal to `200.0`")
if int32 < 20.0: if int32 is not None and int32 < 20.0:
raise ValueError("Invalid value for `int32`, must be a value greater than or equal to `20.0`") raise ValueError("Invalid value for `int32`, must be a value greater than or equal to `20.0`")
self._int32 = int32 self._int32 = int32
@ -185,11 +185,11 @@ class FormatTest(object):
:param number: The number of this FormatTest. :param number: The number of this FormatTest.
:type: float :type: float
""" """
if not number: if number is None:
raise ValueError("Invalid value for `number`, must not be `None`") raise ValueError("Invalid value for `number`, must not be `None`")
if number > 543.2: if number is not None and number > 543.2:
raise ValueError("Invalid value for `number`, must be a value less than or equal to `543.2`") raise ValueError("Invalid value for `number`, must be a value less than or equal to `543.2`")
if number < 32.1: if number is not None and number < 32.1:
raise ValueError("Invalid value for `number`, must be a value greater than or equal to `32.1`") raise ValueError("Invalid value for `number`, must be a value greater than or equal to `32.1`")
self._number = number self._number = number
@ -214,9 +214,9 @@ class FormatTest(object):
:param float: The float of this FormatTest. :param float: The float of this FormatTest.
:type: float :type: float
""" """
if float > 987.6: if float is not None and float > 987.6:
raise ValueError("Invalid value for `float`, must be a value less than or equal to `987.6`") raise ValueError("Invalid value for `float`, must be a value less than or equal to `987.6`")
if float < 54.3: if float is not None and float < 54.3:
raise ValueError("Invalid value for `float`, must be a value greater than or equal to `54.3`") raise ValueError("Invalid value for `float`, must be a value greater than or equal to `54.3`")
self._float = float self._float = float
@ -241,9 +241,9 @@ class FormatTest(object):
:param double: The double of this FormatTest. :param double: The double of this FormatTest.
:type: float :type: float
""" """
if double > 123.4: if double is not None and double > 123.4:
raise ValueError("Invalid value for `double`, must be a value less than or equal to `123.4`") raise ValueError("Invalid value for `double`, must be a value less than or equal to `123.4`")
if double < 67.8: if double is not None and double < 67.8:
raise ValueError("Invalid value for `double`, must be a value greater than or equal to `67.8`") raise ValueError("Invalid value for `double`, must be a value greater than or equal to `67.8`")
self._double = double self._double = double
@ -268,7 +268,7 @@ class FormatTest(object):
:param string: The string of this FormatTest. :param string: The string of this FormatTest.
:type: str :type: str
""" """
if not re.search('[a-z]', string, flags=re.IGNORECASE): if string is not None and not re.search('[a-z]', string, flags=re.IGNORECASE):
raise ValueError("Invalid value for `string`, must be a follow pattern or equal to `/[a-z]/i`") raise ValueError("Invalid value for `string`, must be a follow pattern or equal to `/[a-z]/i`")
self._string = string self._string = string
@ -293,6 +293,8 @@ class FormatTest(object):
:param byte: The byte of this FormatTest. :param byte: The byte of this FormatTest.
:type: str :type: str
""" """
if byte is None:
raise ValueError("Invalid value for `byte`, must not be `None`")
self._byte = byte self._byte = byte
@ -339,6 +341,8 @@ class FormatTest(object):
:param date: The date of this FormatTest. :param date: The date of this FormatTest.
:type: date :type: date
""" """
if date is None:
raise ValueError("Invalid value for `date`, must not be `None`")
self._date = date self._date = date
@ -408,11 +412,11 @@ class FormatTest(object):
:param password: The password of this FormatTest. :param password: The password of this FormatTest.
:type: str :type: str
""" """
if not password: if password is None:
raise ValueError("Invalid value for `password`, must not be `None`") raise ValueError("Invalid value for `password`, must not be `None`")
if len(password) > 64: if password is not None and len(password) > 64:
raise ValueError("Invalid value for `password`, length must be less than or equal to `64`") raise ValueError("Invalid value for `password`, length must be less than or equal to `64`")
if len(password) < 10: if password is not None and len(password) < 10:
raise ValueError("Invalid value for `password`, length must be greater than or equal to `10`") raise ValueError("Invalid value for `password`, length must be greater than or equal to `10`")
self._password = password self._password = password

View File

@ -81,6 +81,8 @@ class Name(object):
:param name: The name of this Name. :param name: The name of this Name.
:type: int :type: int
""" """
if name is None:
raise ValueError("Invalid value for `name`, must not be `None`")
self._name = name self._name = name

View File

@ -133,6 +133,8 @@ class Pet(object):
:param name: The name of this Pet. :param name: The name of this Pet.
:type: str :type: str
""" """
if name is None:
raise ValueError("Invalid value for `name`, must not be `None`")
self._name = name self._name = name
@ -156,6 +158,8 @@ class Pet(object):
:param photo_urls: The photo_urls of this Pet. :param photo_urls: The photo_urls of this Pet.
:type: list[str] :type: list[str]
""" """
if photo_urls is None:
raise ValueError("Invalid value for `photo_urls`, must not be `None`")
self._photo_urls = photo_urls self._photo_urls = photo_urls