Skip to content
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

fix: relax validation for timestamp and uri #39

Open
wants to merge 1 commit into
base: main
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
34 changes: 2 additions & 32 deletions tokenlists/typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from itertools import chain
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from pydantic import AnyUrl
from pydantic import BaseModel as _BaseModel
from pydantic import ConfigDict, PastDatetime, field_validator

Expand Down Expand Up @@ -39,16 +38,6 @@ class TokenInfo(BaseModel):
tags: Optional[List[TagId]] = None
extensions: Optional[Dict[str, Any]] = None

@field_validator("logoURI")
def validate_uri(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return v

if "://" not in v or not AnyUrl(v):
raise ValueError(f"'{v}' is not a valid URI")

return v

@field_validator("extensions", mode="before")
def parse_extensions(cls, v: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
# 1. Check extension depth first
Expand Down Expand Up @@ -150,7 +139,7 @@ def __str__(self) -> str:

class TokenList(BaseModel):
name: str
timestamp: PastDatetime
timestamp: Union[PastDatetime, str]
version: TokenListVersion
tokens: List[TokenInfo]
keywords: Optional[List[str]] = None
Expand All @@ -177,22 +166,3 @@ def __init__(self, *args, **kwargs):
)

model_config = ConfigDict(frozen=False)

@field_validator("logoURI")
def validate_uri(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return v

if "://" not in v or not AnyUrl(v):
raise ValueError(f"'{v}' is not a valid URI")

return v

def model_dump(self, *args, **kwargs) -> Dict:
data = super().model_dump(*args, **kwargs)

if kwargs.get("mode", "").lower() == "json":
# NOTE: This was the easiest way to make sure this property returns isoformat
data["timestamp"] = self.timestamp.isoformat()

return data
Loading