schema.ts


Overview

The schema.ts file defines data validation schemas using the Zod library. It serves as a centralized module for specifying and enforcing the shape and constraints of form-related data objects. This ensures that any form data handled in the application conforms to expected formats, improving type safety and runtime validation.

Specifically, the file provides two main schemas:

The file also exports TypeScript types inferred from these schemas for use elsewhere in the codebase.


Detailed Explanation

Imports

import { z } from 'zod';

VariableFormSchema

export const VariableFormSchema = z.object({
  key: z.string(),
  ref: z.string(),
  value: z.string(),
});
const exampleVar = {
  key: "username",
  ref: "userRef123",
  value: "john_doe"
};

const parseResult = VariableFormSchema.safeParse(exampleVar);
if (parseResult.success) {
  console.log("Valid variable:", parseResult.data);
} else {
  console.error(parseResult.error);
}

FormSchema

export const FormSchema = z.object({
  url: z.string().url(),
  method: z.string(),
  timeout: z.number(),
  headers: z.string(),
  proxy: z.string().url(),
  clean_html: z.boolean(),
  variables: z.array(VariableFormSchema),
});
const exampleForm = {
  url: "https://api.example.com/data",
  method: "POST",
  timeout: 5000,
  headers: '{"Content-Type":"application/json"}',
  proxy: "http://proxy.example.com",
  clean_html: true,
  variables: [
    { key: "token", ref: "auth", value: "abc123" },
    { key: "userId", ref: "user", value: "42" }
  ]
};

const result = FormSchema.safeParse(exampleForm);
if (result.success) {
  console.log("Form data is valid:", result.data);
} else {
  console.error(result.error);
}

TypeScript Types

export type FormSchemaType = z.infer<typeof FormSchema>;

export type VariableFormSchemaType = z.infer<typeof VariableFormSchema>;
const formData: FormSchemaType = {
  url: "https://example.com",
  method: "GET",
  timeout: 3000,
  headers: "{}",
  proxy: "http://proxy.local",
  clean_html: false,
  variables: []
};

Important Implementation Details


Interaction with Other Parts of the System


Diagram: File Structure and Schema Relationships

classDiagram
    class VariableFormSchema {
        +key: string
        +ref: string
        +value: string
    }

    class FormSchema {
        +url: string (valid URL)
        +method: string
        +timeout: number
        +headers: string
        +proxy: string (valid URL)
        +clean_html: boolean
        +variables: VariableFormSchema[]
    }

    FormSchema "1" *-- "*" VariableFormSchema : contains

Summary

This modular approach to schema definition promotes maintainability, consistency, and developer confidence when handling complex form data structures.