Skip to content

Commit

Permalink
fix: relax validation for timestamp and uri
Browse files Browse the repository at this point in the history
  • Loading branch information
banteg committed Apr 8, 2024
1 parent f556715 commit ca73faa
Showing 1 changed file with 2 additions and 32 deletions.
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

0 comments on commit ca73faa

Please sign in to comment.