From 9cc705122a1e2ce90078be42ac33b42b55328f2d Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 25 Nov 2024 13:36:25 +0100 Subject: [PATCH] CheckCreate(slug) & CheckUpdate(slug) field validator & docs As per upstream API v3 https://healthchecks.io/docs/api/#create-check --- src/healthchecks_io/schemas/checks.py | 19 +++++++++++++++++++ tests/client/test_async.py | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/healthchecks_io/schemas/checks.py b/src/healthchecks_io/schemas/checks.py index ff5bdffe..9f0b7a3d 100644 --- a/src/healthchecks_io/schemas/checks.py +++ b/src/healthchecks_io/schemas/checks.py @@ -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 @@ -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( @@ -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: @@ -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, diff --git a/tests/client/test_async.py b/tests/client/test_async.py index 09f13694..1bd92c29 100644 --- a/tests/client/test_async.py +++ b/tests/client/test_async.py @@ -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" @@ -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" @@ -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"