Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/adcp/types/_forward_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
)
from pydantic.fields import FieldInfo
from pydantic.json_schema import SkipJsonSchema
from pydantic_core import CoreSchema, InitErrorDetails, core_schema
from pydantic_core import CoreSchema, InitErrorDetails, PydanticCustomError, core_schema
from pydantic_core.core_schema import ErrorType

from adcp.types.aliases import FormatAssetUnion, GroupFormatAssetUnion, RepeatableAssetGroup
from adcp.types.base import AdCPBaseModel
Expand Down Expand Up @@ -339,6 +340,11 @@ def _validate_targeting_overlay(value: Any, handler: ValidatorFunctionWrapHandle
"loc": error["loc"][1:],
"input": error["input"],
}
# Built-in codes can be reconstructed from their name and context.
# Schema keyword errors (for example anyOf) need their custom type
# and message; core cannot look them up in its built-in registry.
if error["type"] not in get_args(ErrorType):
detail["type"] = PydanticCustomError(error["type"], error["msg"], error.get("ctx"))
if "ctx" in error:
detail["ctx"] = error["ctx"]
input_errors.append(detail)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_targeting_overlay_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def test_direct_validation_error_uses_request_document_path(
@pytest.mark.parametrize(
"overlay",
[
{"frequency_cap": {}},
{"frequency_cap": {}, "geo_countries": [{"secret": _SECRET}]},
{"geo_countries": [_SECRET]},
{"geo_countries": []},
{"keyword_targets": [{"keyword": "", "match_type": _SECRET, "bid_price": -1}]},
Expand All @@ -134,6 +136,8 @@ def test_direct_validation_error_uses_request_document_path(
_SECRET,
],
ids=[
"custom-keyword",
"custom-and-built-in",
"pattern-context",
"length-context",
"multiple-errors",
Expand Down Expand Up @@ -167,6 +171,44 @@ def test_errors_match_input_schema_including_nested_unions_and_context(
]


@pytest.mark.parametrize("task,collection,model", _PATHS)
@pytest.mark.parametrize("mode", ["python", "json"])
def test_valid_frequency_cap_still_validates(
task: str, collection: str, model: type[BaseModel], mode: str
) -> None:
params = _payload(task, collection)
params[collection][0]["targeting_overlay"] = {"frequency_cap": {"suppress_minutes": 60}}
result = (
model.model_validate_json(json.dumps(params))
if mode == "json"
else model.model_validate(params)
)
assert (
result.model_dump(mode="json")[collection][0]["targeting_overlay"]["frequency_cap"][
"suppress_minutes"
]
== 60
)


@pytest.mark.asyncio
@pytest.mark.parametrize("task,collection,model", _PATHS)
async def test_custom_targeting_error_becomes_invalid_request(
task: str, collection: str, model: type[BaseModel]
) -> None:
params = _payload(task, collection)
params[collection][0]["targeting_overlay"] = {"frequency_cap": {}}
caller = create_tool_caller(_TargetingErrorHandler(), task)
with pytest.raises(ADCPTaskError) as raised:
await caller(params)
error = raised.value.errors[0].model_dump(mode="json", exclude_none=True)
assert error["code"] == "INVALID_REQUEST"
assert error["field"] == f"{collection}.0.targeting_overlay.frequency_cap"
assert error["details"]["validation_errors"][0]["type"] == "anyOf"
assert "FrequencyCap requires" in error["message"]
assert "TargetingOverlay" not in json.dumps(error)


@pytest.mark.parametrize("task,collection,model", _PATHS)
@pytest.mark.parametrize("mode", ["python", "json"])
def test_error_order_controls_primary_field_and_message(
Expand Down
Loading