Skip to content

Commit

Permalink
[python] Check if the given input is a container (Array or Map) when …
Browse files Browse the repository at this point in the history
…validating enum values (#19316)

* checks if input is Array or Map in validate_enum

* update samples
  • Loading branch information
vcutrona authored Oct 10, 2024
1 parent ea4b17c commit 45fa438
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,22 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}

{{/isNullable}}
{{/required}}
{{#isContainer}}
{{#isArray}}
for i in value:
if i not in set([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]):
raise ValueError("each list item must be one of ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
{{^isArray}}
{{#isMap}}
for i in value.values():
if i not in set([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]):
raise ValueError("dict values must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
if value not in set([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]):
raise ValueError("must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
{{/isContainer}}
return value
{{/isEnum}}
{{/vars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def map_of_enum_string_validate_enum(cls, value):
if value is None:
return value

if value not in set(['UPPER', 'lower']):
raise ValueError("must be one of enum values ('UPPER', 'lower')")
for i in value.values():
if i not in set(['UPPER', 'lower']):
raise ValueError("dict values must be one of enum values ('UPPER', 'lower')")
return value

model_config = ConfigDict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def map_of_enum_string_validate_enum(cls, value):
if value is None:
return value

if value not in set(['UPPER', 'lower']):
raise ValueError("must be one of enum values ('UPPER', 'lower')")
for i in value.values():
if i not in set(['UPPER', 'lower']):
raise ValueError("dict values must be one of enum values ('UPPER', 'lower')")
return value

model_config = ConfigDict(
Expand Down

0 comments on commit 45fa438

Please sign in to comment.