# stefanterdell/zod-to-json-schema architecture

> Architecture diagram of the GitHub repository stefanterdell/zod-to-json-schema, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stefanterdell/zod-to-json-schema
- Repository: https://github.com/stefanterdell/zod-to-json-schema

## Overview

##### Main Components

a. Input Layer:
- Zod Schema Input Handler
- Options Configuration Handler (represent the extensive options system)

b. Core Processing Layer:
- Schema Parser Hub
- Type-Specific Parsers (grouped by categories)
- Reference Resolution System
- Strategy Handlers

c. Output Layer:
- JSON Schema Generator
- Schema Validation Layer

##### Component Relationships and Structure

**[Input] -> [Processing] -> [Output]**

Detail it as follows:

a. Input Section:
- It to an "Options Handler" component

b. Processing Core (Central Focus):
- Around it, arrange grouped parser components:
- Primitive Types (string, number, boolean)
- Complex Types (array, object, union)
- Special Types (date, promise, enum)
- Validation Types (optional, nullable)

c. Output Section:
- JSON Schema Generator component
- Format-Specific Adapters (OpenAPI, OpenAI)

##### Important Elements to Highlight

- Blue: Core parsing components
- Green: Input/Output handlers
- Yellow: Configuration/Options
- Purple: Reference handling system

- Rectangles: Main processing components
- Hexagons: Input/Output interfaces
- Circles: Strategy handlers
- Diamonds: Decision points

##### Specific Implementation Details

- Parser directory structure relationship:
- Related parsers (e.g., all numeric types)
- How they connect to the central parser hub

- Reference handling system:
- Different reference strategies (root, relative, seen)
- Definitions handling mechanism

##### Key Flows

- Initial schema input flow
- Parser selection process
- Reference resolution paths
- Strategy application points
- Final schema generation

##### Technical Details

- Integration points for:
- Error message handling
- Definition management
- Override system
- Target format adaptation

Remember to:
- Keep the main flow clear and central
- Related components logically
- Use consistent styling for similar components
- Clear labels and annotations
- Both synchronous and asynchronous paths

This will create a comprehensive yet clear representation of how the library transforms Zod schemas into JSON Schema format while handling all the various options and edge cases described in the documentation.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Input
        ZS[Zod Schema Input]
        OH["Options Handler"]:::config
    end

    subgraph Processing
        PH["Parser Hub"]:::core
        
        subgraph "Primitive Parsers"
            SP["String Parser"]:::parser
            NP["Number Parser"]:::parser
            BP["Boolean Parser"]:::parser
        end
        
        subgraph "Complex Parsers"
            AP["Array Parser"]:::parser
            OP["Object Parser"]:::parser
            UP["Union Parser"]:::parser
            IP["Intersection Parser"]:::parser
        end
        
        subgraph "Special Parsers"
            DP["Date Parser"]:::parser
            PP["Promise Parser"]:::parser
            EP["Enum Parser"]:::parser
            NE["Native Enum Parser"]:::parser
        end
        
        subgraph "Validation Parsers"
            VOP["Optional Parser"]:::parser
            NUP["Nullable Parser"]:::parser
            DEF["Default Parser"]:::parser
        end
        
        RRS["Reference Resolution System"]:::core
        SH["Strategy Handlers"]:::core
        EH["Error Handler"]:::error
    end

    subgraph Output
        JSG["JSON Schema Generator"]:::output
        
        subgraph "Format Adapters"
            OAI["OpenAPI Adapter"]:::adapter
            OAPI["OpenAI Adapter"]:::adapter
        end
    end

    %% Connections
    ZS --> OH
    OH --> PH
    
    %% Parser Hub Connections
    PH --> SP & NP & BP
    PH --> AP & OP & UP & IP
    PH --> DP & PP & EP & NE
    PH --> VOP & NUP & DEF
    
    PH <--> RRS
    PH --> SH
    PH --> EH
    
    SP & NP & BP --> JSG
    AP & OP & UP & IP --> JSG
    DP & PP & EP & NE --> JSG
    VOP & NUP & DEF --> JSG
    
    JSG --> OAI
    JSG --> OAPI

    %% Styling
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef parser fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef config fill:#f1c40f,stroke:#f39c12,color:black
    classDef output fill:#2ecc71,stroke:#27ae60,color:white
    classDef error fill:#e74c3c,stroke:#c0392b,color:white
    classDef adapter fill:#9b59b6,stroke:#8e44ad,color:white

    %% Click Events
    click OH "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/Options.ts"
    click RRS "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/Refs.ts"
    click PH "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parseDef.ts"
    click EH "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/errorMessages.ts"
    click JSG "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/zodToJsonSchema.ts"
    click SP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts"
    click NP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/number.ts"
    click BP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/boolean.ts"
    click AP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/array.ts"
    click OP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/object.ts"
    click UP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/union.ts"
    click IP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/intersection.ts"
    click DP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/date.ts"
    click PP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/promise.ts"
    click EP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/enum.ts"
    click NE "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/nativeEnum.ts"
    click VOP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/optional.ts"
    click NUP "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/nullable.ts"
    click DEF "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/default.ts"
    click SH "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/effects.ts"
    click OAI "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/test/openApiMode.test.ts"
    click OAPI "https://github.com/StefanTerdell/zod-to-json-schema/blob/master/test/openAiMode.test.ts"
```
