schema.ts
Overview
The schema.ts file defines a validation schema for a form using the Zod library. This schema enforces the structure and types of data expected when users submit scripting configurations that include programming language selection, script content, input arguments, and output definitions.
Primarily, it ensures the form data conforms to specific types and constraints, validating that:
The programming language is either Python or JavaScript.
The script is a string.
The arguments are an array of objects each with a name and type.
The outputs can be either a single object with name and type or an optional array of such objects.
This validation schema supports type-safe form processing and reduces runtime errors in applications involving dynamic script execution or agent configuration.
Detailed Explanation
Imports
ProgrammingLanguage: An enum imported from the@/constants/agentpath that contains supported programming languages, currentlyPythonandJavascript.z: The Zod library namespace used for schema definition and validation.
FormSchema
export const FormSchema = z.object({
lang: z.enum([ProgrammingLanguage.Python, ProgrammingLanguage.Javascript]),
script: z.string(),
arguments: z.array(z.object({ name: z.string(), type: z.string() })),
outputs: z.union([
z.array(z.object({ name: z.string(), type: z.string() })).optional(),
z.object({ name: z.string(), type: z.string() }),
]),
});
Type: Zod schema object
Purpose: Defines the shape and validation rules for a form submission related to scripting.
Properties:
Property | Type | Description |
|---|---|---|
| Enum of | Specifies the programming language used in the script. |
|
| The actual script content/code as a string. |
| Array of objects | The list of input arguments the script expects, each with a name and a type. |
| Either a single object | Defines the expected outputs from the script; flexible to accept one or multiple outputs. |
Usage Example:
import { FormSchema } from './schema';
import { ProgrammingLanguage } from '@/constants/agent';
const formData = {
lang: ProgrammingLanguage.Python,
script: "def add(a, b): return a + b",
arguments: [
{ name: "a", type: "number" },
{ name: "b", type: "number" },
],
outputs: { name: "result", type: "number" },
};
const parseResult = FormSchema.safeParse(formData);
if (!parseResult.success) {
console.error(parseResult.error);
} else {
console.log("Form data is valid:", parseResult.data);
}
FormSchemaType
export type FormSchemaType = z.infer<typeof FormSchema>;
Type: TypeScript type alias
Purpose: Extracts the inferred TypeScript type from the
FormSchemaZod schema. This type can be used throughout the application to ensure type safety when handling validated form data.
Example:
function handleFormSubmission(data: FormSchemaType) {
// `data` is guaranteed to match the schema structure
console.log(data.lang, data.script);
}
Implementation Details
Zod Library: Used for schema validation and type inference, providing a declarative and type-safe approach to input validation.
Union Type for Outputs: The
outputsproperty uses az.unionto allow flexibility in form data, supporting either a single output object or an array of output objects. The array variant is optional, meaningoutputscan be either a single object or omitted array.
Interaction with Other Parts of the Application
ProgrammingLanguageEnum: The schema relies on theProgrammingLanguageconstants imported from@/constants/agentto restrict the language selection. This ensures consistency with the application's supported languages.Form Processing Components: This schema is likely used in form validation middleware, frontend form components, or API endpoints that accept user scripts.
Script Execution or Agent Configuration: After validation, the structured data may be passed to components responsible for executing scripts, generating dynamic code, or configuring agent behavior.
Mermaid Diagram: Class/Structure Diagram of FormSchema
classDiagram
class FormSchema {
+lang: "Python" | "Javascript"
+script: string
+arguments: Argument[]
+outputs: Output | Output[]
}
class Argument {
+name: string
+type: string
}
class Output {
+name: string
+type: string
}
FormSchema "1" o-- "*" Argument : has
FormSchema "1" o-- "1..*" Output : has
Summary
This file is a critical piece in ensuring reliable user input handling for scripting forms by defining a robust validation schema using Zod. It enforces strict typing and structure for programming language selection, script content, input arguments, and output formats, which helps maintain data integrity across the application. The schema also facilitates type safety in TypeScript through the inferred type alias. It integrates closely with predefined programming language constants and likely serves as a foundation for scripting or agent configuration features elsewhere in the system.