Skip to content

Commit df00d05

Browse files
committed
fix: use regex to validate input digits
we want to keep input string and check if the string indeed contains only digits
1 parent be35b3e commit df00d05

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/api/models/forms.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from typing import Optional
23

34
from pydantic import BaseModel, model_validator, validator
@@ -16,31 +17,33 @@ class SingleForm(BaseModel):
1617
activity_permanence_status: Optional[str] = None
1718

1819
@validator("type_form")
19-
def validate_type_form(cls, v):
20+
def validate_type_form(cls, v: str) -> str:
2021
if (v is not None) and (v not in VALID_TYPE_FORM):
2122
raise ValueError(f"Invalid type_form '{v}', must be one of {VALID_TYPE_FORM}")
2223
return v
2324

2425
@validator("nature")
25-
def validate_nature(cls, v):
26-
if (v is not None) and (not v.isdigit() or len(v) != 2):
27-
raise ValueError("nature must be a two-digit number (e.g., '01')")
26+
def validate_nature(cls, v: str) -> str:
27+
if v is not None:
28+
if (not re.fullmatch(r"\d+", v)) or (len(v) != 2):
29+
raise ValueError("nature must be a two-digit number (e.g., '01')")
2830
return v
2931

3032
@validator("surface")
31-
def validate_surface(cls, v):
33+
def validate_surface(cls, v: str) -> str:
3234
if (v is not None) and (v not in VALID_SURFACE):
3335
raise ValueError(f"Invalid surface '{v}', must be one of {VALID_SURFACE}")
3436
return v
3537

3638
@validator("cj")
37-
def validate_cj(cls, v):
38-
if (v is not None) and (not v.isdigit() or len(v) != 2):
39-
raise ValueError("cj must be a 4-digit number (e.g., '5499')")
39+
def validate_cj(cls, v: str) -> str:
40+
if v is not None:
41+
if (not re.fullmatch(r"\d+", v)) or (len(v) != 4):
42+
raise ValueError("cj must be a 4-digit number (e.g., '5499')")
4043
return v
4144

4245
@validator("activity_permanence_status")
43-
def validate_activity_permanence_status(cls, v):
46+
def validate_activity_permanence_status(cls, v: str) -> str:
4447
if (v is not None) and (v not in VALID_ACTIV_PERM):
4548
raise ValueError(f"Invalid surface '{v}', must be one of {VALID_ACTIV_PERM}")
4649
return v

0 commit comments

Comments
 (0)