Skip to content

Commit

Permalink
✨ add validator compat
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Feb 17, 2024
1 parent 451fa68 commit 94f49ca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nonebot/adapters/github/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def payload_to_event(
)
except Exception as e:
log("WARNING", f"Failed to parse webhook payload {event_id}", e)
return Event.parse_obj(
{"id": event_id, "name": event_name, "payload": event_payload}
return type_validate_python(
Event, {"id": event_id, "name": event_name, "payload": event_payload}
)

@classmethod
Expand Down
15 changes: 15 additions & 0 deletions nonebot/adapters/github/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Literal

from nonebot.compat import PYDANTIC_V2

__all__ = ("field_validator",)

if PYDANTIC_V2:
from pydantic import field_validator as field_validator
else:
from pydantic import validator

def field_validator(
__field: str, *fields: str, mode: Literal["before", "after"] = "after"
):
return validator(__field, *fields, pre=mode == "before", allow_reuse=True)
7 changes: 5 additions & 2 deletions nonebot/adapters/github/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any, List, Union, Optional

from pydantic import Field, BaseModel, validator
from pydantic import Field, BaseModel

from .compat import field_validator


class OAuthApp(BaseModel):
Expand All @@ -24,7 +26,8 @@ class GitHubApp(BaseModel):
def id(self) -> str:
return self.app_id

@validator("private_key", pre=True)
@field_validator("private_key", mode="before")
@classmethod
def concat_key(cls, value: object) -> Any:
return "\n".join(value) if isinstance(value, list) else value

Expand Down

0 comments on commit 94f49ca

Please sign in to comment.