Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions src/healthchecks_io/schemas/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Optional
from typing import Union
from urllib.parse import urlparse
import re

import pytz
from croniter import croniter
Expand Down Expand Up @@ -66,6 +67,10 @@ class CheckCreate(BaseModel):
"""Pydantic object for creating a check."""

name: Optional[str] = Field("", description="Name of the check")
slug: Optional[str] = Field(
"",
description="Slug for the check. The slug should only contain the following characters: a-z, 0-9, hyphens, underscores.",
)
tags: Optional[str] = Field("", description="String separated list of tags to apply")
desc: Optional[str] = Field("", description="Description of the check")
timeout: Optional[int] = Field(
Expand Down Expand Up @@ -126,6 +131,16 @@ class CheckCreate(BaseModel):
"for the unique field are name, tags, timeout, and grace.",
)

@field_validator("slug")
@classmethod
def validate_slug(cls, value: str) -> str:
"""Validate that the slug is a valid string."""
if not bool(re.match(pattern=r"^[a-z0-9\-_]+$", string=value)):
raise ValueError(
"Slug is invalid, it must only contain the following characters: a-z, 0-9, hyphens, underscores."
)
return value

@field_validator("schedule")
@classmethod
def validate_schedule(cls, value: str) -> str:
Expand Down Expand Up @@ -166,6 +181,10 @@ class CheckUpdate(CheckCreate):
"""Pydantic object for updating a check."""

name: Optional[str] = Field(None, description="Name of the check")
slug: Optional[str] = Field(
"",
description="Slug for the check. The slug should only contain the following characters: a-z, 0-9, hyphens, underscores.",
)
tags: Optional[str] = Field(None, description="String separated list of tags to apply")
timeout: Optional[int] = Field(
None,
Expand Down
6 changes: 3 additions & 3 deletions tests/client/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def test_acreate_check_200_context_manager(fake_check_api_result, respx_mo
)
)
async with test_async_client as test_client:
check = await test_client.create_check(CheckCreate(name="test", tags="test", desc="test"))
check = await test_client.create_check(CheckCreate(name="test", slug="test", tags="test", desc="test"))
assert check.name == "Backups"


Expand Down Expand Up @@ -72,7 +72,7 @@ async def test_acreate_check_200(fake_check_api_result, respx_mock, test_async_c
},
)
)
check = await test_async_client.create_check(CheckCreate(name="test", tags="test", desc="test"))
check = await test_async_client.create_check(CheckCreate(name="test", slug="test", tags="test", desc="test"))
assert check.name == "Backups"


Expand Down Expand Up @@ -103,7 +103,7 @@ async def test_aupdate_check_200(fake_check_api_result, respx_mock, test_async_c
},
)
)
check = await test_async_client.update_check("test", CheckUpdate(name="test", desc="test"))
check = await test_async_client.update_check("test", CheckUpdate(name="test", slug="test", desc="test"))
assert check.name == "Backups"


Expand Down