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:

This validation schema supports type-safe form processing and reduces runtime errors in applications involving dynamic script execution or agent configuration.


Detailed Explanation

Imports


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() }),
  ]),
});

Properties:

Property

Type

Description

lang

Enum of ProgrammingLanguage.Python or ProgrammingLanguage.Javascript

Specifies the programming language used in the script.

script

string

The actual script content/code as a string.

arguments

Array of objects { name: string, type: string }

The list of input arguments the script expects, each with a name and a type.

outputs

Either a single object { name: string, type: string } or an optional array of such

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>;

Example:

function handleFormSubmission(data: FormSchemaType) {
  // `data` is guaranteed to match the schema structure
  console.log(data.lang, data.script);
}

Implementation Details


Interaction with Other Parts of the Application


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.