Skip to content

Commit

Permalink
feat: 增加对枚举字段option设置的校验
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Nov 20, 2023
1 parent 97c7835 commit a6a2b6c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/bk-user/bkuser/apps/tenant/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,22 @@ class TenantUserCustomFieldOptions(BaseModel):
"""用户自定义字段-options字段"""

options: List[Option]

@model_validator(mode="after")
def validate_attrs(self) -> "TenantUserCustomFieldOptions":
option_ids = [obj.id for obj in self.options]

limit_count = 2

def determine_duplicate_values(values: List[str]) -> bool:
# 判断重复值
return any(values.count(item) >= limit_count for item in values)

if determine_duplicate_values(option_ids):
raise ValueError(_("枚举ID设置有重复值"))

option_values = [obj.value for obj in self.options]
if determine_duplicate_values(option_values):
raise ValueError(_("枚举ID设置有重复值"))

return self

0 comments on commit a6a2b6c

Please sign in to comment.