From 44b21f10f153b176aa8af62bc3ccc33a1ec4c8bb Mon Sep 17 00:00:00 2001 From: Ryan Dick Date: Thu, 1 Aug 2024 14:00:57 -0400 Subject: [PATCH] Add a pydantic model_validator to BoundingBoxField to check the validity of the coords. --- invokeai/app/invocations/fields.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/invokeai/app/invocations/fields.py b/invokeai/app/invocations/fields.py index fe97123fef..9efcf2148f 100644 --- a/invokeai/app/invocations/fields.py +++ b/invokeai/app/invocations/fields.py @@ -1,7 +1,7 @@ from enum import Enum from typing import Any, Callable, Optional, Tuple -from pydantic import BaseModel, ConfigDict, Field, RootModel, TypeAdapter +from pydantic import BaseModel, ConfigDict, Field, RootModel, TypeAdapter, model_validator from pydantic.fields import _Unset from pydantic_core import PydanticUndefined @@ -258,6 +258,14 @@ class BoundingBoxField(BaseModel): "when the bounding box was produced by a detector and has an associated confidence score.", ) + @model_validator(mode="after") + def check_coords(self): + if self.x_min > self.x_max: + raise ValueError(f"x_min ({self.x_min}) is greater than x_max ({self.x_max}).") + if self.y_min > self.y_max: + raise ValueError(f"y_min ({self.y_min}) is greater than y_max ({self.y_max}).") + return self + class MetadataField(RootModel[dict[str, Any]]): """