[python-nextgen] Fix validation of list of enums (#14809)

* [python-nextgen] check enum arrady values better

* [python-nextgen] re-generate exapmles for #14809
This commit is contained in:
Tomáš Karásek
2023-03-18 13:27:26 +01:00
committed by GitHub
parent e1719f2b7b
commit aa066ab6fa
18 changed files with 43 additions and 56 deletions

View File

@@ -34,18 +34,17 @@ class EnumArrays(BaseModel):
def just_symbol_validate_enum(cls, v):
if v is None:
return v
if v not in ('>=', '$'):
raise ValueError("must validate the enum values ('>=', '$')")
raise ValueError("must be one of enum values ('>=', '$')")
return v
@validator('array_enum')
def array_enum_validate_enum(cls, v):
if v is None:
return v
if v not in ('fish', 'crab'):
raise ValueError("must validate the enum values ('fish', 'crab')")
for i in v:
if i not in ('fish', 'crab'):
raise ValueError("each list item must be one of ('fish', 'crab')")
return v
class Config:

View File

@@ -45,42 +45,38 @@ class EnumTest(BaseModel):
def enum_string_validate_enum(cls, v):
if v is None:
return v
if v not in ('UPPER', 'lower', ''):
raise ValueError("must validate the enum values ('UPPER', 'lower', '')")
raise ValueError("must be one of enum values ('UPPER', 'lower', '')")
return v
@validator('enum_string_required')
def enum_string_required_validate_enum(cls, v):
if v not in ('UPPER', 'lower', ''):
raise ValueError("must validate the enum values ('UPPER', 'lower', '')")
raise ValueError("must be one of enum values ('UPPER', 'lower', '')")
return v
@validator('enum_integer_default')
def enum_integer_default_validate_enum(cls, v):
if v is None:
return v
if v not in (1, 5, 14):
raise ValueError("must validate the enum values (1, 5, 14)")
raise ValueError("must be one of enum values (1, 5, 14)")
return v
@validator('enum_integer')
def enum_integer_validate_enum(cls, v):
if v is None:
return v
if v not in (1, -1):
raise ValueError("must validate the enum values (1, -1)")
raise ValueError("must be one of enum values (1, -1)")
return v
@validator('enum_number')
def enum_number_validate_enum(cls, v):
if v is None:
return v
if v not in (1.1, -1.2):
raise ValueError("must validate the enum values (1.1, -1.2)")
raise ValueError("must be one of enum values (1.1, -1.2)")
return v
class Config:

View File

@@ -36,9 +36,8 @@ class MapTest(BaseModel):
def map_of_enum_string_validate_enum(cls, v):
if v is None:
return v
if v not in ('UPPER', 'lower'):
raise ValueError("must validate the enum values ('UPPER', 'lower')")
raise ValueError("must be one of enum values ('UPPER', 'lower')")
return v
class Config:

View File

@@ -38,9 +38,8 @@ class Order(BaseModel):
def status_validate_enum(cls, v):
if v is None:
return v
if v not in ('placed', 'approved', 'delivered'):
raise ValueError("must validate the enum values ('placed', 'approved', 'delivered')")
raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')")
return v
class Config:

View File

@@ -40,9 +40,8 @@ class Pet(BaseModel):
def status_validate_enum(cls, v):
if v is None:
return v
if v not in ('available', 'pending', 'sold'):
raise ValueError("must validate the enum values ('available', 'pending', 'sold')")
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return v
class Config:

View File

@@ -36,9 +36,8 @@ class SpecialName(BaseModel):
def var_schema_validate_enum(cls, v):
if v is None:
return v
if v not in ('available', 'pending', 'sold'):
raise ValueError("must validate the enum values ('available', 'pending', 'sold')")
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return v
class Config:

View File

@@ -235,4 +235,4 @@ class ModelTests(unittest.TestCase):
self.pet.status = "error"
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
self.assertTrue("must validate the enum values ('available', 'pending', 'sold')" in str(e))
self.assertTrue("must be one of enum values ('available', 'pending', 'sold')" in str(e))