Skip to content

upd validator for pydanticv2 #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions fastapi_jwt_auth/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
validator,
StrictBool,
StrictInt,
StrictStr
StrictStr,
field_validator
)

class LoadConfig(BaseModel):
Expand Down Expand Up @@ -44,37 +45,37 @@ class LoadConfig(BaseModel):
authjwt_refresh_csrf_header_name: Optional[StrictStr] = "X-CSRF-Token"
authjwt_csrf_methods: Optional[Sequence[StrictStr]] = {'POST','PUT','PATCH','DELETE'}

@validator('authjwt_access_token_expires')
@field_validator('authjwt_access_token_expires')
def validate_access_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_access_token_expires' only accept value False (bool)")
return v

@validator('authjwt_refresh_token_expires')
@field_validator('authjwt_refresh_token_expires')
def validate_refresh_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_refresh_token_expires' only accept value False (bool)")
return v

@validator('authjwt_denylist_token_checks', each_item=True)
@field_validator('authjwt_denylist_token_checks', mode='before')
def validate_denylist_token_checks(cls, v):
if v not in ['access','refresh']:
raise ValueError("The 'authjwt_denylist_token_checks' must be between 'access' or 'refresh'")
return v

@validator('authjwt_token_location', each_item=True)
@field_validator('authjwt_token_location', mode='before')
def validate_token_location(cls, v):
if v not in ['headers','cookies']:
raise ValueError("The 'authjwt_token_location' must be between 'headers' or 'cookies'")
return v

@validator('authjwt_cookie_samesite')
@field_validator('authjwt_cookie_samesite')
def validate_cookie_samesite(cls, v):
if v not in ['strict','lax','none']:
raise ValueError("The 'authjwt_cookie_samesite' must be between 'strict', 'lax', 'none'")
return v

@validator('authjwt_csrf_methods', each_item=True)
@field_validator('authjwt_csrf_methods', mode='before')
def validate_csrf_methods(cls, v):
if v.upper() not in {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}:
raise ValueError("The 'authjwt_csrf_methods' must be between http request methods")
Expand Down