Skip to content

Commit

Permalink
🐛 Fix validator Error (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mingi3314 authored Jul 10, 2024
1 parent 9364289 commit 0d6f2b7
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions sitemapr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from decimal import Decimal
from typing import Literal, TypeVar

from pydantic import BaseModel, ValidationError, validator
from pydantic import BaseModel, field_validator

T = TypeVar("T")

Expand Down Expand Up @@ -32,18 +32,17 @@ class SiteMapUrl(BaseModel):
changefreq: ChangeFreq | None = None # Google ignores this
priority: str | None = None # Google ignores this

@validator("priority")
@field_validator("priority")
@classmethod
def validate_priority(cls, v: str | None) -> str | None:
if v is None:
return v
try:
priority = Decimal(v)
except Exception as e:
raise ValidationError(
"Priority must be a valid decimal string between 0.0 and 1.0"
) from e
raise ValueError("Priority must be a valid decimal string between 0.0 and 1.0") from e

if 0 <= priority <= 1:
return f"{priority:.1f}"

raise ValidationError("Priority must be between 0.0 and 1.0")
raise ValueError("Priority must be between 0.0 and 1.0")

0 comments on commit 0d6f2b7

Please sign in to comment.