use-form-schema.ts
Overview
The use-form-schema.ts file defines and exports a custom React Hook, useCreateCategorizeFormSchema, which generates a Zod validation schema for a form related to categorization functionality. This schema is primarily used to validate form inputs such as query parameters, configuration settings, and a list of categorized items with detailed metadata.
The schema integrates an existing LlmSettingSchema (likely representing settings related to a language model or a similar service) and enriches it with additional fields specific to the categorize form. The validation schema also supports internationalization through react-i18next by allowing error messages to be localized.
This file is a utility module designed to be used in React components or hooks that handle form validation and submission, ensuring that user inputs conform to expected structures before processing.
Detailed Breakdown
Imports
LlmSettingSchema: Imported from
'@/components/llm-setting-items/next'. This appears to be a predefined Zod schema encapsulating settings related to a language model or similar configurable options.useTranslation: From
'react-i18next'. Provides translation functionality to localize validation error messages.z: From
'zod'. The main library used for schema definition and data validation.
Function: useCreateCategorizeFormSchema
function useCreateCategorizeFormSchema(): z.ZodObject<...>
Purpose
Creates and returns a Zod schema object tailored for validating the categorize form data.
Parameters
The function accepts no parameters.
Returns
Returns a Zod object schema (
z.ZodObject) which validates the shape of the categorize form data.
Schema Structure
Field | Type | Description | Notes |
|---|---|---|---|
|
| An optional query string parameter for categorization. | |
|
| Optional additional parameter for the form. | |
Spread | Various | Configuration settings related to the language model or related service. | Imported schema, merged into this schema. |
| coerced to | Numeric value specifying the size of the message history window. | Uses |
| Array of objects (each optional) | An array defining categorized items, each with metadata. | Each item object can be optional but if present must adhere to schema below. |
Item Object Schema (inside items array)
Field | Type | Description | Validation / Notes |
|---|---|---|---|
|
| The name of the categorize item. | Required, minimum 1 character, trimmed, localized error message. |
|
| Optional description of the item. | |
|
| Unique identifier for the item. | Required |
| Array of objects (optional) | Optional list of example objects for the item. | Each example has a |
Usage Example
import { useCreateCategorizeFormSchema } from './use-form-schema';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
function CategorizeFormComponent() {
const schema = useCreateCategorizeFormSchema();
const { register, handleSubmit, formState: { errors } } = useForm({
resolver: zodResolver(schema),
});
const onSubmit = (data) => {
console.log('Validated form data:', data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
{/* form inputs bound with `register` here */}
{/* handle errors with `errors` */}
</form>
);
}
Important Implementation Details
Use of
z.coerce.number(): This allows themessage_history_window_sizefield to accept string inputs that can be converted to numbers, which is useful when form inputs are typically strings but numeric validation is required.Localization of Validation Messages: The schema uses the
useTranslationhook fromreact-i18nextto inject localized error messages. For example, thenamefield usest('flow.nameMessage')as the minimum length validation message, ensuring that users see validation prompts in their selected language.Optional Nested Objects: Each item in the
itemsarray is an optional object. This allows flexibility where some array elements might be undefined or omitted, but if present, they must meet validation constraints.Schema Composition: The schema merges in
LlmSettingSchemausing spread syntax. This modular approach allows the categorization form schema to reuse and extend existing schemas, promoting code reuse and maintainability.
Interaction with Other Parts of the System
LlmSettingSchema Dependency: This file depends directly on
LlmSettingSchemafrom components related to language model settings. Changes inLlmSettingSchemawill affect this form schema.Usage in Form Components: The schema is intended to be used with form management libraries such as React Hook Form, where it acts as the validation blueprint for user input.
i18n Integration: The use of
react-i18nextties this schema to the internationalization system of the application, making it responsive to user locale changes.
Diagram
classDiagram
class useCreateCategorizeFormSchema {
+useCreateCategorizeFormSchema(): ZodObject
}
class FormSchema {
+query?: string
+parameter?: string
+message_history_window_size: number
+items: Item[]
+LlmSettingSchema
}
class Item {
+name: string
+description?: string
+uuid: string
+examples?: Example[]
}
class Example {
+value: string
}
useCreateCategorizeFormSchema --> FormSchema
FormSchema --> Item
Item --> Example
FormSchema --> LlmSettingSchema : includes
Summary
The use-form-schema.ts file provides a well-structured and localized Zod schema for validating a categorize form that includes query parameters, language model settings, and a customizable list of items. It leverages schema composition and internationalization to ensure robust and user-friendly input validation, forming a core part of the form handling mechanism in the application.