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


Fields and Validation Rules

Field

Type

Validation Details

Notes

name

string

Minimum length 1 (error message indicates 2 characters, likely a message typo).

User's name or username.

description

string

Minimum length 2 (same error message as name, likely a copy-paste mistake).

A description field.

avatar

any or nullish

Accepts any type or null/undefined. Commented out is a stricter File type validator.

Optional avatar file or data.

permission

string (optional)

Optional string field.

Likely represents permission-level or role.

parser_id

string

Required string.

Identifier for a parser.

embd_id

string

Required string.

Identifier for embedding or related entity.

parser_config

Nested object (optional)

Complex nested object with multiple fields and nested objects including raptor and graphrag.

Contains configuration for parser behavior.

pagerank

number

Required number field.

Likely a ranking or priority metric.


Nested Objects in parser_config

parser_config (Optional Object)

Field

Type

Description / Validation

layout_recognize

string

Required string field.

chunk_token_num

number

Required number.

delimiter

string

Required string delimiter.

auto_keywords

number (optional)

Optional number.

auto_questions

number (optional)

Optional number.

html4excel

boolean

Required boolean flag.

tag_kb_ids

array of string or nullish

Optional array of strings or null.

topn_tags

number (optional)

Optional number.

raptor

Nested object

Contains fields related to the "raptor" configuration and has a custom refinement check.

graphrag

Nested object

Contains fields related to the "graphrag" configuration with a custom refinement check.


raptor Object

Field

Type

Description / Validation

use_raptor

boolean (optional)

Indicates if the "raptor" feature is used.

prompt

string (optional)

Required if use_raptor is true.

max_token

number (optional)

Maximum token limit.

threshold

number (optional)

Threshold value for some processing.

max_cluster

number (optional)

Maximum cluster count.

random_seed

number (optional)

Seed value for randomization.


graphrag Object

Field

Type

Description / Validation

use_graphrag

boolean (optional)

Indicates if the "graphrag" feature is used.

entity_types

array of string (optional)

Required non-empty array if use_graphrag is true.

method

string (optional)

Optional method name.

resolution

boolean (optional)

Optional boolean flag.

community

boolean (optional)

Optional boolean flag.


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


Interaction with Other Parts of the System


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.