schema.ts
Overview
The schema.ts file defines TypeScript schemas for form data validation using the Zod library. It provides structured validation rules for a form containing HTTP request parameters and an array of key-value variables, ensuring that the input data conforms to expected types and formats before further processing.
This file is primarily responsible for:
Validating the shape and data types of form inputs.
Defining TypeScript types inferred from these schemas for type-safe usage across the application.
Detailed Explanation
Imports
import { z } from 'zod';
Imports the
znamespace from the Zod library, which is used to create validation schemas.
VariableFormSchema
export const VariableFormSchema = z.object({
key: z.string(),
ref: z.string(),
value: z.string(),
});
Description
Defines the schema for an individual variable object used within the main form. Each variable has three string properties.
Properties
Property | Type | Description |
|---|---|---|
| string | The key name of the variable. |
| string | A reference identifier. |
| string | The value assigned to the variable. |
Usage Example
const exampleVar = {
key: "username",
ref: "user_ref_123",
value: "john_doe",
};
const parsedVar = VariableFormSchema.parse(exampleVar); // Validates `exampleVar`
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),
});
Description
Defines the main form schema representing HTTP request configuration details and a list of variables.
Properties
Property | Type | Description |
|---|---|---|
| string (URL format) | The target URL for the HTTP request. |
| string | The HTTP method (e.g., GET, POST). |
| number | Request timeout duration in milliseconds or seconds. |
| string | HTTP headers represented as a string (likely JSON or raw). |
| string (URL format) | Proxy URL to route the HTTP request through. |
| boolean | Flag indicating whether to clean HTML content in the response. |
| array of | List of variable objects to be included in the form. |
Usage Example
const exampleForm = {
url: "https://api.example.com/data",
method: "POST",
timeout: 5000,
headers: '{"Content-Type": "application/json"}',
proxy: "https://proxy.example.com",
clean_html: true,
variables: [
{ key: "token", ref: "auth", value: "xyz123" },
{ key: "userId", ref: "user", value: "42" },
],
};
const parsedForm = FormSchema.parse(exampleForm); // Validates `exampleForm`
TypeScript Type Inference
export type FormSchemaType = z.infer<typeof FormSchema>;
export type VariableFormSchemaType = z.infer<typeof VariableFormSchema>;
FormSchemaTypeis a TypeScript type representing the validated shape of the main form.VariableFormSchemaTyperepresents the type of an individual variable object.
These types can be used for static type checking throughout the application, ensuring consistency between validation and usage.
Implementation Details & Algorithms
Validation: Uses Zod's declarative syntax to specify constraints (
z.string(),.url(),.boolean(),.number(),.array()).URL validation:
.url()ensures thaturlandproxyfields are valid URLs.Nested schema: The
variablesproperty is an array of objects validated against the nestedVariableFormSchema.Type inference: Leverages Zod's
.inferutility to derive TypeScript types directly from schemas, reducing duplication and errors.
Interaction with Other Parts of the System
Form handling components: This schema file is likely imported by form components or controllers responsible for accepting user input, validating it, and processing HTTP requests.
API request modules: The validated form data probably feeds into HTTP client modules or services that perform network requests using the configured URL, method, headers, and proxy.
State management & type safety: The inferred types are used across the codebase to ensure that form data objects conform to expected structures at compile time.
Error handling: Validation errors thrown by Zod when parsing invalid data can be caught and displayed to users or logged.
Visual Diagram
classDiagram
class VariableFormSchema {
+key: string
+ref: string
+value: string
}
class FormSchema {
+url: string (URL)
+method: string
+timeout: number
+headers: string
+proxy: string (URL)
+clean_html: boolean
+variables: VariableFormSchema[]
}
FormSchema --> "0..*" VariableFormSchema : contains
Summary
schema.ts defines robust validation schemas for form data related to HTTP request configuration using Zod.
It ensures data correctness and provides type safety through inferred TypeScript types.
The nested structure supports complex form data with variables.
This file is foundational for validating input before performing network operations and maintaining data integrity system-wide.