BUPKIS
    Preparing search index...

    Function valueToSchema

    • Recursively converts an arbitrary value to a Zod v4 schema that would validate values with the same structure.

      This function analyzes the runtime value and generates a corresponding Zod schema that captures the value's structure and type information. It handles primitives, objects, arrays, functions, and various built-in types, with support for circular reference detection.

      Parameters

      • value: unknown

        The value to convert to a schema

      • options: ValueToSchemaOptions = {}

        Configuration options for schema generation

      • visited: WeakSet<object> = ...

        Internal WeakSet for circular reference detection

      Returns ZodType<any>

      A Zod schema that validates values matching the input's structure. This value is unfortunately untyped due to the complexity involved. But the schema works!

      // Primitive types
      valueToSchema('hello'); // z.string()
      valueToSchema(42); // z.number()
      valueToSchema(true); // z.boolean()

      // Objects
      valueToSchema({ name: 'John', age: 30 });
      // z.object({ name: z.string(), age: z.number() })

      // Arrays
      valueToSchema(['a', 'b', 'c']); // z.array(z.string())
      valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))

      // Nested structures
      valueToSchema({ users: [{ name: 'John' }] });
      // z.object({ users: z.array(z.object({ name: z.string() })) })