feat(reddit-schemas): add community name normalization validator to prevent 404 errors

This commit is contained in:
Anish Sarkar 2026-07-25 02:55:20 +05:30
parent 6068472ecc
commit 2379a6a23f

View file

@ -14,7 +14,7 @@ from __future__ import annotations
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel, ConfigDict, Field, field_validator
RedditSort = Literal["relevance", "hot", "top", "new", "rising", "comments"]
RedditTime = Literal["all", "hour", "day", "week", "month", "year"]
@ -44,6 +44,23 @@ class RedditScrapeInput(BaseModel):
searches: list[str] = Field(default_factory=list)
searchCommunityName: str | None = None
@field_validator("searchCommunityName", mode="after")
@classmethod
def _normalize_community(cls, v: str | None) -> str | None:
"""Normalize the bare subreddit name at the trust boundary.
Both the in-subreddit search path and the community-only listing path
interpolate this straight into ``r/{name}/...``, so a pasted ``r/python``
or ``/r/python/`` would otherwise become ``r/r/python`` and 404. Strip a
leading ``r/`` and surrounding slashes/whitespace; empty coerces to None.
"""
if v is None:
return None
name = v.strip().strip("/")
if name[:2].lower() == "r/":
name = name[2:].strip("/")
return name or None
# Sort / filter
sort: RedditSort = "new"
time: RedditTime | None = None