form-schema.ts
Overview
The form-schema.ts file defines a comprehensive validation schema for form data using the zod library, a TypeScript-first schema declaration and validation library. The schema, named formSchema, outlines the expected structure and validation rules for a complex form object, including nested objects and conditional validations.
This file's primary purpose is to ensure that any data submitted through the associated form adheres to the required format and constraints before further processing or storage. It is typically used in frontend or backend form handling to validate user input, providing immediate feedback on errors according to the defined rules.
Detailed Explanation
formSchema
Type:
z.ZodObjectDescription: The main schema object that validates the entire form data structure.
Structure:
The schema validates an object that includes fields such asname,description,avatar,permission,parser_id,embd_id,parser_config, andpagerank.
Fields and Validation Rules
Field | Type | Validation Details | Notes |
|---|---|---|---|
|
| Minimum length 1 (error message indicates 2 characters, likely a message typo). | User's name or username. |
|
| Minimum length 2 (same error message as | A description field. |
|
| Accepts any type or | Optional avatar file or data. |
|
| Optional string field. | Likely represents permission-level or role. |
|
| Required string. | Identifier for a parser. |
|
| Required string. | Identifier for embedding or related entity. |
| Nested object (optional) | Complex nested object with multiple fields and nested objects including | Contains configuration for parser behavior. |
|
| Required number field. | Likely a ranking or priority metric. |
Nested Objects in parser_config
parser_config (Optional Object)
Field | Type | Description / Validation |
|---|---|---|
|
| Required string field. |
|
| Required number. |
|
| Required string delimiter. |
|
| Optional number. |
|
| Optional number. |
|
| Required boolean flag. |
| array of | Optional array of strings or null. |
|
| Optional number. |
| Nested object | Contains fields related to the "raptor" configuration and has a custom refinement check. |
| Nested object | Contains fields related to the "graphrag" configuration with a custom refinement check. |
raptor Object
Field | Type | Description / Validation |
|---|---|---|
|
| Indicates if the "raptor" feature is used. |
|
| Required if |
|
| Maximum token limit. |
|
| Threshold value for some processing. |
|
| Maximum cluster count. |
|
| Seed value for randomization. |
Refinement rule:
Ifuse_raptoristrue,promptmust be provided; otherwise, validation fails with the message"Prompt is required".
graphrag Object
Field | Type | Description / Validation |
|---|---|---|
|
| Indicates if the "graphrag" feature is used. |
| array of | Required non-empty array if |
|
| Optional method name. |
|
| Optional boolean flag. |
|
| Optional boolean flag. |
Refinement rule:
Ifuse_graphragistrue,entity_typesmust be a non-empty array; otherwise, validation fails with the message"Please enter Entity types".
Usage Example
import { formSchema } from './form-schema.ts';
const formData = {
name: "John",
description: "Sample description",
avatar: null,
parser_id: "parser123",
embd_id: "embed456",
parser_config: {
layout_recognize: "standard",
chunk_token_num: 100,
delimiter: ",",
html4excel: true,
raptor: {
use_raptor: true,
prompt: "Analyze this document",
max_token: 500,
threshold: 0.8,
max_cluster: 10,
random_seed: 42,
},
graphrag: {
use_graphrag: true,
entity_types: ["Person", "Organization"],
method: "default",
resolution: true,
community: false,
},
},
pagerank: 5,
};
// Validate form data
const result = formSchema.safeParse(formData);
if (!result.success) {
console.error(result.error.format());
} else {
console.log("Form data is valid:", result.data);
}
Important Implementation Details
Validation library: Uses
zodfor schema definition and validation, allowing declarative and composable validations.Custom Refinements:
For
raptor, ensures a prompt is provided if the feature is enabled.For
graphrag, ensures entity types are provided if the feature is enabled.
Flexible avatar field: Currently allows any type or nullish values; a commented line suggests a more strict validation for
Fileinstances is possible.
Interaction with Other Parts of the System
This schema is designed to validate form data likely collected via a UI form or received via API requests.
It ensures data integrity before passing the data to further processing layers, such as parsers, embedding services, or ranking algorithms.
The identifiers (
parser_id,embd_id) suggest links to parser and embedding modules elsewhere in the system.The nested
parser_configobject configures how parsing and data extraction should behave, potentially influencing downstream NLP or data processing workflows.Validation errors produced by this schema can be used directly for user feedback or logging.
Mermaid Diagram
classDiagram
class formSchema {
<<z.ZodObject>>
+name: string (min 1)
+description: string (min 2)
+avatar: any | nullish
+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 : parser_config (optional)
ParserConfig --> RaptorConfig : raptor
ParserConfig --> GraphragConfig : graphrag
Summary
The form-schema.ts file provides a robust, declarative validation schema for form input data with nested configurations and conditional validations tailored for parser and graph-related features. It plays a crucial role in data integrity enforcement and user feedback in form-driven workflows related to parsing and embedding configurations within the larger system.