Skip to content

Commit

Permalink
feat: enhance password security with validation checks
Browse files Browse the repository at this point in the history
- Import the `re` module for regular expressions.
- Update the password field to require a minimum length of 8 characters.
- Add a password validation method to ensure the password contains at least one number.

Signed-off-by: kyo <[email protected]>
  • Loading branch information
kyomind committed Sep 23, 2024
1 parent aea8d4d commit 2d9840a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion user/schemas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import re

from ninja import Field, Schema
from pydantic import field_validator


class CreateUserRequest(Schema):
username: str = Field(examples=['Alice'])
email: str = Field(examples=['[email protected]'])
password: str = Field(examples=["password123"])
password: str = Field(min_length=8, examples=["password123"])
bio: str | None = Field(default=None, examples=['Hello, I am Alice.'])

@field_validator('password')
@classmethod
def validate_password_contains_number(cls, v: str) -> str:
"""
驗證密碼至少包含一個數字
"""
if not re.search(r'\d', v):
raise ValueError('密碼必須包含至少一個數字')
return v

0 comments on commit 2d9840a

Please sign in to comment.