From cf033504c2a7de5258f91ab41c310bdffb5c635a Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Fri, 12 Apr 2024 20:11:20 +0200 Subject: refactor(agent/utils): Clean up `JSONSchema.validate_object` signature & docstring --- .../autogpt/agents/prompt_strategies/one_shot.py | 5 +---- autogpts/autogpt/autogpt/core/utils/json_schema.py | 20 ++++++++------------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py index 72916e1fa..0234c59a5 100644 --- a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py +++ b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py @@ -392,10 +392,7 @@ class OneShotAgentPromptStrategy(PromptStrategy): f"{json.dumps(assistant_reply_dict, indent=4)}" ) - _, errors = self.response_schema.validate_object( - object=assistant_reply_dict, - logger=self.logger, - ) + _, errors = self.response_schema.validate_object(assistant_reply_dict) if errors: raise InvalidAgentResponseError( "Validation of response failed:\n " diff --git a/autogpts/autogpt/autogpt/core/utils/json_schema.py b/autogpts/autogpt/autogpt/core/utils/json_schema.py index c9f9026e0..d72b509dd 100644 --- a/autogpts/autogpt/autogpt/core/utils/json_schema.py +++ b/autogpts/autogpt/autogpt/core/utils/json_schema.py @@ -1,9 +1,8 @@ import enum -from logging import Logger from textwrap import indent -from typing import Literal, Optional +from typing import Optional -from jsonschema import Draft7Validator +from jsonschema import Draft7Validator, ValidationError from pydantic import BaseModel @@ -84,27 +83,24 @@ class JSONSchema(BaseModel): v.required = k in schema_node["required"] return properties - def validate_object( - self, object: object, logger: Logger - ) -> tuple[Literal[True], None] | tuple[Literal[False], list]: + def validate_object(self, object: object) -> tuple[bool, list[ValidationError]]: """ - Validates a dictionary object against the JSONSchema. + Validates an object or a value against the JSONSchema. Params: - object: The dictionary object to validate. + object: The value/object to validate. schema (JSONSchema): The JSONSchema to validate against. Returns: - tuple: A tuple where the first element is a boolean indicating whether the - object is valid or not, and the second element is a list of errors found - in the object, or None if the object is valid. + bool: Indicates whether the given value or object is valid for the schema. + list[ValidationError]: The issues with the value or object (if any). """ validator = Draft7Validator(self.to_dict()) if errors := sorted(validator.iter_errors(object), key=lambda e: e.path): return False, errors - return True, None + return True, [] def to_typescript_object_interface(self, interface_name: str = "") -> str: if self.type != JSONSchema.Type.OBJECT: -- cgit v1.2.3