• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-data-services / 10353957042

12 Aug 2024 02:35PM UTC coverage: 90.758% (+0.4%) from 90.398%
10353957042

Pull #338

github

web-flow
Merge 3a49eb6c2 into 8afb94949
Pull Request #338: feat!: expand environments specification

227 of 237 new or added lines in 7 files covered. (95.78%)

48 existing lines in 9 files now uncovered.

9202 of 10139 relevant lines covered (90.76%)

1.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.95
/components/renku_data_services/base_models/validation.py
1
"""Base response validation used by services."""
2✔
2

3
from collections.abc import Callable
2✔
4
from typing import Any
2✔
5

6
from pydantic import BaseModel
2✔
7
from pydantic import ValidationError as PydanticValidationError
2✔
8
from sanic import json
2✔
9
from sanic.response import JSONResponse
2✔
10

11
from renku_data_services import errors
2✔
12

13

14
def validate_and_dump(
2✔
15
    model: type[BaseModel],
16
    data: Any,
17
    exclude_none: bool = True,
18
) -> Any:
19
    """Validate and dump with a pydantic model, ensuring proper validation errors."""
20
    try:
2✔
21
        body = model.model_validate(data).model_dump(exclude_none=exclude_none, mode="json")
2✔
UNCOV
22
    except PydanticValidationError as err:
×
UNCOV
23
        parts = [".".join(str(i) for i in field["loc"]) + ": " + field["msg"] for field in err.errors()]
×
UNCOV
24
        message = (
×
25
            f"The server could not construct a valid response. Errors found in the following fields: {', '.join(parts)}"
26
        )
UNCOV
27
        raise errors.ProgrammingError(message=message) from err
×
28
    return body
2✔
29

30

31
def validated_json(
2✔
32
    model: type[BaseModel],
33
    data: Any,
34
    status: int = 200,
35
    headers: dict[str, str] | None = None,
36
    content_type: str = "application/json",
37
    dumps: Callable[..., str] | None = None,
38
    exclude_none: bool = True,
39
    **kwargs: Any,
40
) -> JSONResponse:
41
    """Creates a JSON response with data validation.
42

43
    If the input data fails validation, an HTTP status code 500 will be raised.
44
    """
45
    body = validate_and_dump(model, data, exclude_none)
2✔
46
    return json(body, status=status, headers=headers, content_type=content_type, dumps=dumps, **kwargs)
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc