Update deprecated pydantic validator (#1960)

update deprecated pydantic validator
This commit is contained in:
chyok 2024-05-26 22:17:58 +08:00 • committed by GitHub
commit 9f2a0d1552
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 14 deletions

View file

@ -2,7 +2,7 @@ from datetime import datetime, timezone
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from uuid import UUID, uuid4 from uuid import UUID, uuid4
from pydantic import field_validator, validator from pydantic import field_validator
from sqlmodel import Column, DateTime, Field, Relationship, SQLModel, func from sqlmodel import Column, DateTime, Field, Relationship, SQLModel, func
if TYPE_CHECKING: if TYPE_CHECKING:
@ -40,6 +40,7 @@ class ApiKeyCreate(ApiKeyBase):
created_at: Optional[datetime] = Field(default_factory=utc_now) created_at: Optional[datetime] = Field(default_factory=utc_now)
@field_validator("created_at", mode="before") @field_validator("created_at", mode="before")
@classmethod
def set_created_at(cls, v): def set_created_at(cls, v):
return v or utc_now() return v or utc_now()
@ -52,10 +53,11 @@ class UnmaskedApiKeyRead(ApiKeyBase):
class ApiKeyRead(ApiKeyBase): class ApiKeyRead(ApiKeyBase):
id: UUID id: UUID
api_key: str = Field() api_key: str = Field(schema_extra={"validate_default": True})
user_id: UUID = Field() user_id: UUID = Field()
@validator("api_key", always=True) @field_validator("api_key")
@classmethod
def mask_api_key(cls, v): def mask_api_key(cls, v):
# This validator will always run, and will mask the API key # This validator will always run, and will mask the API key
return f"{v[:8]}{'*' * (len(v) - 8)}" return f"{v[:8]}{'*' * (len(v) - 8)}"

View file

@ -4,7 +4,7 @@ from typing import Literal
from loguru import logger from loguru import logger
from passlib.context import CryptContext from passlib.context import CryptContext
from pydantic import Field, SecretStr, validator from pydantic import Field, SecretStr, field_validator
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
from langflow.services.settings.constants import DEFAULT_SUPERUSER, DEFAULT_SUPERUSER_PASSWORD from langflow.services.settings.constants import DEFAULT_SUPERUSER, DEFAULT_SUPERUSER_PASSWORD
@ -62,23 +62,25 @@ class AuthSettings(BaseSettings):
# the default values # the default values
# so we need to validate the superuser and superuser_password # so we need to validate the superuser and superuser_password
# fields # fields
@validator("SUPERUSER", "SUPERUSER_PASSWORD", pre=True) @field_validator("SUPERUSER", "SUPERUSER_PASSWORD", mode="before")
def validate_superuser(cls, value, values): @classmethod
if values.get("AUTO_LOGIN"): def validate_superuser(cls, value, info):
if info.data.get("AUTO_LOGIN"):
if value != DEFAULT_SUPERUSER: if value != DEFAULT_SUPERUSER:
value = DEFAULT_SUPERUSER value = DEFAULT_SUPERUSER
logger.debug("Resetting superuser to default value") logger.debug("Resetting superuser to default value")
if values.get("SUPERUSER_PASSWORD") != DEFAULT_SUPERUSER_PASSWORD: if info.data.get("SUPERUSER_PASSWORD") != DEFAULT_SUPERUSER_PASSWORD:
values["SUPERUSER_PASSWORD"] = DEFAULT_SUPERUSER_PASSWORD info.data["SUPERUSER_PASSWORD"] = DEFAULT_SUPERUSER_PASSWORD
logger.debug("Resetting superuser password to default value") logger.debug("Resetting superuser password to default value")
return value return value
return value return value
@validator("SECRET_KEY", pre=True) @field_validator("SECRET_KEY", mode="before")
def get_secret_key(cls, value, values): @classmethod
config_dir = values.get("CONFIG_DIR") def get_secret_key(cls, value, info):
config_dir = info.data.get("CONFIG_DIR")
if not config_dir: if not config_dir:
logger.debug("No CONFIG_DIR provided, not saving secret key") logger.debug("No CONFIG_DIR provided, not saving secret key")

View file

@ -1,7 +1,7 @@
from typing import List, Optional from typing import List, Optional
from uuid import UUID from uuid import UUID
from pydantic import BaseModel, validator from pydantic import BaseModel, field_validator
class TagResponse(BaseModel): class TagResponse(BaseModel):
@ -37,7 +37,8 @@ class ListComponentResponse(BaseModel):
private: Optional[bool] = None private: Optional[bool] = None
# tags comes as a TagsIdResponse but we want to return a list of TagResponse # tags comes as a TagsIdResponse but we want to return a list of TagResponse
@validator("tags", pre=True) @field_validator("tags", mode="before")
@classmethod
def tags_to_list(cls, v): def tags_to_list(cls, v):
# Check if all values are have id and name # Check if all values are have id and name
# if so, return v else transform to TagResponse # if so, return v else transform to TagResponse