use-form-schema.ts
Overview
This file defines a custom React hook useCreateCategorizeFormSchema that provides a Zod schema for validating and parsing form data related to a "categorize" feature, presumably within a user interface that involves natural language processing or categorization tasks enhanced by Language Model (LLM) settings.
The hook leverages the zod library to define a strongly-typed schema that ensures form inputs conform to expected types and constraints. It also integrates internationalization (i18n) support for validation messages and incorporates shared LLM-related settings from an external schema (LlmSettingSchema).
Detailed Explanation
useCreateCategorizeFormSchema()
Purpose
Creates and returns a validation schema for a categorization form, which includes fields for user queries, parameters, LLM settings, message history window size, and a list of categorized items with their metadata.
Usage
import { useCreateCategorizeFormSchema } from './use-form-schema';
const schema = useCreateCategorizeFormSchema();
// schema can then be used with form libraries like react-hook-form or Formik
// to validate user inputs according to the defined types and rules.
Parameters
This function takes no parameters.
Return Value
Returns a Zod
z.ZodObjectschema that describes the shape and validation rules of the form data.
Schema Structure
Field Name | Type | Description |
|---|---|---|
|
| Optional user query string. |
|
| Optional parameter string. |
|
| Spread of LLM-related settings imported from |
| coerced | Number indicating the size of the message history window; coerced from input to number. |
| array of optional objects | An array containing zero or more item objects, each optionally included, representing categories. |
Each item object contains:
name: string (required, minimum length 1, trimmed) — name of the category; validation message utilizes i18n key'flow.nameMessage'.description: string (optional) — additional description for the category.uuid: string (required) — unique identifier for the category.examples: optional array of objects each with:value: string — example text associated with the category.
Implementation Details
Internationalization: The hook uses
useTranslationfromreact-i18nextto provide localized validation error messages, enabling better UX in multilingual environments.Zod Validation: The schema utilizes Zod's composable validation methods (
z.string(),z.number(),z.array(), etc.) and coercion (z.coerce.number()) to robustly parse and validate incoming form data.Schema Composition: The schema composition spreads in
LlmSettingSchemato incorporate additional LLM-related configuration fields, promoting modularity and reuse.
Interaction with Other Parts of the System
LlmSettingSchema: Imported from@/components/llm-setting-items/next, this external schema defines additional settings related to Language Model configurations. This file depends on it to compose the full form schema.useTranslation: Integration withreact-i18nextallows the schema to use localized strings for validation messages.Form Libraries: This schema is likely used with form management libraries (e.g., react-hook-form, Formik) to enforce validation rules client-side when users submit categorize forms.
Categorize Feature: The schema represents the core data structure for categorization inputs, possibly used in workflows involving natural language queries and category assignments.
Visual Diagram
classDiagram
class useCreateCategorizeFormSchema {
+FormSchema: z.ZodObject
+useTranslation(): { t: function }
}
class FormSchema {
+query?: string
+parameter?: string
+...LlmSettingSchema
+message_history_window_size: number
+items: Item[]
}
class Item {
+name: string
+description?: string
+uuid: string
+examples?: Example[]
}
class Example {
+value: string
}
useCreateCategorizeFormSchema --> FormSchema
FormSchema "1" --> "*" Item
Item "1" --> "*" Example
Summary
use-form-schema.ts provides a reusable hook that returns a Zod schema for validating categorize form data.
The schema validates strings, numbers (with coercion), arrays, and nested objects with optional and required fields.
It integrates i18n for dynamic validation messages and composes an external LLM settings schema.
The schema is intended for use in form validation workflows within a React application that handles categorization enhanced by LLM configurations.
This modular and strongly-typed schema improves data integrity and user experience by ensuring that form inputs meet expected formats before further processing.