Skip to content

Commit 44b21f1

Browse files
committed
Add a pydantic model_validator to BoundingBoxField to check the validity of the coords.
1 parent c6d49e8 commit 44b21f1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

invokeai/app/invocations/fields.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Enum
22
from typing import Any, Callable, Optional, Tuple
33

4-
from pydantic import BaseModel, ConfigDict, Field, RootModel, TypeAdapter
4+
from pydantic import BaseModel, ConfigDict, Field, RootModel, TypeAdapter, model_validator
55
from pydantic.fields import _Unset
66
from pydantic_core import PydanticUndefined
77

@@ -258,6 +258,14 @@ class BoundingBoxField(BaseModel):
258258
"when the bounding box was produced by a detector and has an associated confidence score.",
259259
)
260260

261+
@model_validator(mode="after")
262+
def check_coords(self):
263+
if self.x_min > self.x_max:
264+
raise ValueError(f"x_min ({self.x_min}) is greater than x_max ({self.x_max}).")
265+
if self.y_min > self.y_max:
266+
raise ValueError(f"y_min ({self.y_min}) is greater than y_max ({self.y_max}).")
267+
return self
268+
261269

262270
class MetadataField(RootModel[dict[str, Any]]):
263271
"""

0 commit comments

Comments
 (0)