form-schema.ts
Overview
The form-schema.ts file defines a comprehensive validation schema for form data using the Zod library. This schema enforces data types, required fields, optional fields, nested object structures, and complex validation rules for a form related to user or configuration input, likely within a larger application involving user profiles, parsing configurations, and knowledge base tagging.
By leveraging Zod's schema definition and refinement capabilities, the file ensures that the form data conforms to expected shapes before processing or persisting, reducing runtime errors and improving data integrity.
Detailed Explanation
formSchema (Zod Object Schema)
formSchema is the main export of this file. It is a Zod object schema that validates an input object with multiple fields, some of which are primitive types, optional, nullable, or nested objects with their own rules.
Schema Fields and Validations
Field Name | Type | Required | Notes / Validation |
|---|---|---|---|
|
| Yes | Must be at least 1 character. Custom error message: "Username must be at least 2 characters." |
|
| Yes | Must be at least 2 characters. Same custom error message as |
|
| No | Nullable; originally intended to be a |
|
| No | Optional string field. |
|
| Yes | Required string field, presumably an identifier for a parser. |
|
| Yes | Required string field, presumably an identifier for an embedding. |
| Object (nested) | No | Optional nested object containing multiple parsing-related configurations. See details below. |
|
| Yes | Required number field, likely for page rank or priority. |
Nested parser_config Object
The parser_config field is an optional object with the following properties:
Property | Type | Required | Notes / Validation |
|---|---|---|---|
|
| Yes | Required string. |
|
| Yes | Required number. |
|
| Yes | Required string. |
|
| No | Optional number. |
|
| No | Optional number. |
|
| Yes | Required boolean. |
|
| No | Nullable array of strings. |
|
| No | Optional number. |
raptor Object (Nested inside parser_config)
Optional object with the following fields:
use_raptor:boolean(optional)prompt:string(optional)max_token:number(optional)threshold:number(optional)max_cluster:number(optional)random_seed:number(optional)
Custom Refinement:
Ifuse_raptoris truthy, thenpromptmust be present (non-empty).
If not, validation fails with message "Prompt is required" assigned to path['prompt'].
graphrag Object (Nested inside parser_config)
Optional object with the following fields:
use_graphrag:boolean(optional)entity_types:string[](optional)method:string(optional)resolution:boolean(optional)community:boolean(optional)
Custom Refinement:
Ifuse_graphragis truthy, thenentity_typesmust be provided and contain at least one string.
Otherwise, validation fails with message "Please enter Entity types" assigned to path['entity_types'].
Usage Example
import { formSchema } from './form-schema';
// Example form data to validate
const formData = {
name: 'John',
description: 'A sample user',
avatar: null,
permission: 'admin',
parser_id: 'parser-123',
embd_id: 'embed-456',
parser_config: {
layout_recognize: 'standard',
chunk_token_num: 100,
delimiter: ',',
html4excel: true,
tag_kb_ids: ['kb1', 'kb2'],
raptor: {
use_raptor: true,
prompt: 'Analyze this text',
},
graphrag: {
use_graphrag: true,
entity_types: ['Person', 'Organization'],
},
},
pagerank: 10,
};
// Validate form data
try {
const validatedData = formSchema.parse(formData);
console.log('Validation succeeded:', validatedData);
} catch (e) {
if (e instanceof z.ZodError) {
console.error('Validation errors:', e.errors);
}
}
Important Implementation Details
Zod Library: The file uses Zod, a TypeScript-first schema validation library, to declaratively define validation rules.
Custom Refinements:
The nested objectsraptorandgraphraginclude.refine()calls to add conditional validation logic that depends on other fields' values.Nullable and Optional Fields:
Some fields allownullorundefined(e.g.,avatar,tag_kb_ids), enabling flexibility in form submission.Commented Out Code:
Theavatarfield originally expected aFileinstance (commented out), hinting at file upload or avatar image handling.Error Messages:
Custom error messages are used for minimum length constraints onnameanddescription, although the message text ("Username...") might be a copy-paste oversight givendescriptionis validated similarly.
Interaction with Other System Components
Form Input Handling:
This schema likely functions as the validation layer for a user-facing form component or API endpoint that collects user or configuration data.Parser and Embedding Modules:
Fields likeparser_id,embd_id, andparser_configimply integration with parsing and embedding subsystems, potentially for processing text or documents.Knowledge Base and Tagging:
The presence oftag_kb_ids,raptor, andgraphragconfigurations suggests interaction with knowledge base tagging or graph-based entity recognition modules.File Uploads:
The commentedavatarfield indicates potential integration with file upload or media storage components.
Visual Diagram
classDiagram
class formSchema {
+name: string
+description: string
+avatar: any|null
+permission?: string
+parser_id: string
+embd_id: string
+parser_config?: ParserConfig
+pagerank: number
}
class ParserConfig {
+layout_recognize: string
+chunk_token_num: number
+delimiter: string
+auto_keywords?: number
+auto_questions?: number
+html4excel: boolean
+tag_kb_ids: string[]|null
+topn_tags?: number
+raptor?: RaptorConfig
+graphrag?: GraphragConfig
}
class RaptorConfig {
+use_raptor?: boolean
+prompt?: string
+max_token?: number
+threshold?: number
+max_cluster?: number
+random_seed?: number
}
class GraphragConfig {
+use_graphrag?: boolean
+entity_types?: string[]
+method?: string
+resolution?: boolean
+community?: boolean
}
formSchema --> ParserConfig
ParserConfig --> RaptorConfig
ParserConfig --> GraphragConfig
Summary
The form-schema.ts file provides a robust validation schema for complex form data involving user info, parsing configuration, and advanced features like graph-based entity recognition and ranking. Its use of Zod ensures strong typing, clear validation rules, and custom logic to enforce dependencies within nested data structures, making it a critical part of data integrity enforcement in the associated application.