schema_utils.go

Overview

The schema_utils.go file provides utility functions to validate data structures against JSON schemas defined by the genai.Schema type. Its core functionality revolves around type checking and schema validation for inputs and outputs, ensuring data conforms to expected formats, types, and required properties.

This file is critical for runtime verification of data consistency with schemas, preventing type mismatches or missing required fields when processing input arguments or interpreting output JSON payloads. It supports primary JSON schema types such as string, integer, boolean, number, array, and object, with recursive validation for nested structures.

The validation logic is primarily used in contexts where inputs or outputs must conform strictly to predefined API or model schemas, aligning with schema-driven data handling patterns found in LLM Integration and Agents.


Functions and Methods

matchType(value any, schema *genai.Schema, isInput bool) (bool, error)

Checks whether a given value matches the type definition specified by the schema. Recursively validates arrays and objects using their respective subschemas.


ValidateMapOnSchema(args map[string]any, schema *genai.Schema, isInput bool) error

Validates a map (args) against an object schema, checking each property for existence and type correctness, as well as ensuring all required keys are present.


ValidateOutputSchema(output string, schema *genai.Schema) (map[string]any, error)

Parses a JSON string output and validates the resulting map against the provided schema, intended for validating output data.


Implementation Details and Algorithms


Interaction with Other Components


Visual Flowchart of Main Functions and Relationships

flowchart TD
A[matchType] --> B[ValidateMapOnSchema]
C[ValidateOutputSchema] --> D[json.Unmarshal to map]
D --> B
B --> E["matchType (recursive)"]
A --> E

Explanation:


End of schema_utils.go documentation.