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:
VariableFormSchema: Validates individual key-value variable entries with a reference.FormSchema: Validates an entire form which includes URL, HTTP method, timeout, headers, proxy settings, a boolean flag to clean HTML content, and an array of variables structured perVariableFormSchema.
The file also exports TypeScript types inferred from these schemas for use elsewhere in the codebase.
Detailed Explanation
Imports
import { z } from 'zod';
Imports the Zod library, which is a TypeScript-first schema declaration and validation library.
VariableFormSchema
export const VariableFormSchema = z.object({
key: z.string(),
ref: z.string(),
value: z.string(),
});
Purpose: Defines the schema for a variable object used within a form.
Structure:
key: A string representing the variable's key.ref: A string representing a reference identifier for the variable.value: A string representing the value associated with the variable.
Usage Example:
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);
}
Return: Validates an object with the above three string properties.
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),
});
Purpose: Defines the schema for the entire form data object, validating critical HTTP request parameters and associated metadata.
Fields:
url: Must be a valid URL string.method: HTTP method as a string (e.g.,"GET","POST").timeout: Number representing the timeout duration (likely in milliseconds).headers: A string representing HTTP headers (likely serialized, e.g., JSON string).proxy: A valid URL string denoting a proxy server.clean_html: A boolean flag indicating whether to clean HTML content.variables: An array of variable objects adhering toVariableFormSchema.
Usage Example:
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);
}
Return: Validates an object containing all listed properties with their respective types and constraints.
TypeScript Types
export type FormSchemaType = z.infer<typeof FormSchema>;
export type VariableFormSchemaType = z.infer<typeof VariableFormSchema>;
These aliases infer the TypeScript types from the Zod schemas, allowing strong typing in other parts of the application.
Example usage:
const formData: FormSchemaType = {
url: "https://example.com",
method: "GET",
timeout: 3000,
headers: "{}",
proxy: "http://proxy.local",
clean_html: false,
variables: []
};
Important Implementation Details
Zod Usage: The file leverages Zod to perform both compile-time type inference and runtime validation.
Validation Constraints:
URLs (
urlandproxy) must be valid URLs, not just arbitrary strings.variablesis an array of objects conforming strictly to theVariableFormSchema.
Headers Field: Not typed as an object but as a string, suggesting headers might be serialized JSON or another string format that requires further parsing elsewhere.
No Methods or Classes: The file purely contains schema definitions and types, making it a utility module for validation.
Interaction with Other Parts of the System
This file acts as a validation and typing utility for form data objects used throughout the system.
Likely imported by:
Form handling components that accept or submit form data.
API request modules that use the validated form data to configure HTTP requests.
Data processing layers that need to ensure incoming data matches expected structure.
Because it exports both schemas and types, it supports both validation at runtime and type safety during development.
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
Purpose: Define and enforce the structure of form and variable data objects using Zod.
Key Elements: Two Zod schemas (
VariableFormSchema,FormSchema), and inferred TypeScript types.Usage: Provides both validation and typing for form-related data across the application.
Design: Simple, declarative schema definitions without logic or methods.
Integration: Used wherever form data needs validation or typing, ensuring data integrity and reducing runtime errors.
This modular approach to schema definition promotes maintainability, consistency, and developer confidence when handling complex form data structures.