fix python required property check and validation for optional properties

This commit is contained in:
wing328
2016-10-07 15:50:57 +08:00
parent 0ca60352e2
commit 65f925f45c
7 changed files with 39 additions and 23 deletions

View File

@@ -88,37 +88,37 @@ class {{classname}}(object):
{{/isContainer}}
{{/isEnum}}
{{^isEnum}}
{{#hasValidation}}
{{#required}}
if {{name}} is None:
raise ValueError("Invalid value for `{{name}}`, must not be `None`")
{{/required}}
{{#hasValidation}}
{{#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}}`")
{{/maxLength}}
{{#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}}`")
{{/minLength}}
{{#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}}`")
{{/maximum}}
{{#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}}`")
{{/minimum}}
{{#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}}}`")
{{/pattern}}
{{#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}}`")
{{/maxItems}}
{{#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}}`")
{{/minItems}}
{{/hasValidation}}