Zie619/n8n-workflows · warning · ValueError
Rating must be between 1 and 5
Error message
Rating must be between 1 and 5
What it means
A ValueError raised by CommunityFeatures.add_rating when the rating argument falls outside the inclusive range 1..5. It is explicit input validation before any DB write, so no SQLite state is touched when it fires. Callers that do not catch it will see it propagate up as an unhandled exception (or a 500 if raised inside an HTTP handler).
Source
Thrown at src/community_features.py:133
workflow_id TEXT NOT NULL,
user_id TEXT NOT NULL,
parent_id INTEGER, -- For threaded comments
comment TEXT NOT NULL,
helpful_votes INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
conn.close()
def add_rating(
self, workflow_id: str, user_id: str, rating: int, review: str = None
) -> bool:
"""Add or update a workflow rating and review"""
if not (1 <= rating <= 5):
raise ValueError("Rating must be between 1 and 5")
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
try:
# Insert or update rating
cursor.execute(
"""
INSERT OR REPLACE INTO workflow_ratings
(workflow_id, user_id, rating, review, updated_at)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
""",
(workflow_id, user_id, rating, review),
)
# Update workflow statistics
self._update_workflow_stats(workflow_id)
View on GitHub (pinned to 94007c1445)
Solutions
- Clamp or reject the value on the client before calling: only send integers 1 through 5.
- If exposing via HTTP, validate with a Pydantic model field rating: int = Field(ge=1, le=5) so FastAPI returns 422 instead of a 500.
- Catch the ValueError at the API boundary and map it to a 400 Bad Request with the message.
- Audit star-widget code for 0-based indices (map index+1 before sending).
Example fix
# before
community.add_rating(wf_id, user_id, rating=stars) # stars can be 0
# after
from pydantic import BaseModel, Field
class RatingIn(BaseModel):
rating: int = Field(ge=1, le=5)
rating_in = RatingIn.model_validate({"rating": stars})
community.add_rating(wf_id, user_id, rating=rating_in.rating) Defensive patterns
Strategy: validation
Validate before calling
def is_valid_rating(rating) -> bool:
return isinstance(rating, int) and not isinstance(rating, bool) and 1 <= rating <= 5 Type guard
def is_valid_rating(rating) -> TypeGuard[bool] is not needed; use:
def is_valid_rating(value: object) -> bool:
"""True when value is an int in [1, 5] (bools excluded)."""
if isinstance(value, bool):
return False
return isinstance(value, int) and 1 <= value <= 5 Try / catch
try:
community.add_rating(wf_id, user_id, rating=stars)
except ValueError as e:
if "Rating must be between 1 and 5" in str(e):
raise HTTPException(status_code=400, detail=str(e)) from e
raise Prevention
- Validate at the API boundary with Pydantic Field(ge=1, le=5) so bad input 422s instead of 500ing.
- Guard star widgets: disable submit until a star is chosen (no 0-star submits).
- Map this ValueError to 400 in HTTP layers; never let it surface as 500.
When it happens
Trigger: Calling add_rating(workflow_id, user_id, rating=0), rating=6, rating=-1, or a non-integer value that Python comparison rejects; also rating passed as a string like '5' when type annotation is not enforced at runtime.
Common situations: Frontend star widget sending 0 for 'unrated'; off-by-one loop indexing producing 6; API endpoint forwarding unvalidated user input straight into add_rating; JSON body delivering rating as a string.
Related errors
- Research query must be at least 3 characters long
- Initial Vector 'data' is undefined or missing.
- Flow token is missing.
- Insufficient stock for ${$('Retrieve Issue Request Details')
- Invalid quantity Requested: ${quantityRequested}. Must be gr
AI-assisted analysis of Zie619/n8n-workflows@94007c1445 (2026-08-15).
Data as JSON: /api/errors/96c2cbd2a7b7aa6e.
Report an issue: GitHub.