# hapijs/joi architecture

> Architecture diagram of the GitHub repository hapijs/joi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hapijs/joi
- Repository: https://github.com/hapijs/joi

## Overview

##### Main Components Identification

a. Core Validation Engine
- Located in /lib/validator.js
- Central component that processes validation rules

b. Type System
- Located in /lib/types/
- Contains various data type validators (string, number, array, etc.)

c. Schema Management
- Located in /lib/schemas.js and /lib/template.js
- Handles schema definition and processing

d. Error Handling System
- Located in /lib/errors.js
- Manages validation errors and messages

**┌─────────────────────────────────────────┐**
- Public API Layer             │
- (lib/index.js/.d.ts)          │
- ┤
- Schema Management             │
- (schemas.js, template.js)         │
- ┤
- Core Validation Engine          │
- (validator.js)               │
- ┤
- Type System                 │
- (types/*.js - string, number, etc)  │
- ┘

##### Specific Elements

- Schema Parser
- Validation Engine
- Type System
- Error Handler

b. Support Systems (Rounded rectangles):
- Caching System (lib/cache.js)
- Reference Handler (lib/ref.js)
- State Manager (lib/state.js)

c. Extension Points (Hexagons):
- Custom Type Extensions
- Custom Validation Rules
- Custom Error Messages

##### Additional Details

Annotations for:
- Browser support (browser/ directory)
- Test coverage (test/ directory)
- Performance benchmarks (benchmarks/ directory)

##### Integration Points

External interaction points:
- API consumers
- Custom extensions
- Browser integration

##### The error handling flow

- Component dependencies
- Data flow
- Extension points
- Core functionality

- Common patterns and flows

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Legend
        direction TB
        L1[Core Component]:::core
        L2([Support Component]):::support
        L3{{Extension Point}}:::extension
        L4[(Type Component)]:::type
        L5>Error Component]:::error
        L6[Schema Component]:::schema
        
        style Legend fill:#f5f5f5,stroke:#333
    end

    subgraph "Public API Layer"
        direction TB
        API[JavaScript API]:::core
        TSD[TypeScript Definitions]:::core
    end

    subgraph "Schema Management"
        direction TB
        SH[Schema Handler]:::schema
        TS[Template System]:::schema
    end

    subgraph "Core Engine"
        direction TB
        VE[Validation Engine]:::core
        Cache([Caching System]):::support
        Ref([Reference Handler]):::support
        State([State Manager]):::support
        Common[Common Utilities]:::support
        Extend{{Extension System}}:::extension
        Messages[Message System]:::support
    end

    subgraph "Type System"
        direction TB
        String[(String)]:::type
        Number[(Number)]:::type
        Array[(Array)]:::type
        Object[(Object)]:::type
        Boolean[(Boolean)]:::type
        Date[(Date)]:::type
        Function[(Function)]:::type
        Binary[(Binary)]:::type
        Symbol[(Symbol)]:::type
    end

    subgraph "Error Handling"
        direction TB
        EH>Error Handler]:::error
    end

    subgraph "Integration & Testing"
        direction TB
        Browser[Browser Integration]:::support
        Bench[Performance Benchmarks]:::support
        Test[Test Coverage]:::support
    end

    %% Relationships
    API --> SH
    API --> VE
    TSD --> SH
    SH --> VE
    TS --> VE
    VE --> String & Number & Array & Object & Boolean & Date & Function & Binary & Symbol
    VE --> EH
    VE --> Cache
    VE --> Ref
    VE --> State
    VE --> Common
    VE --> Extend
    VE --> Messages

    %% Styles
    classDef core fill:#d4e6f1,stroke:#2874a6
    classDef support fill:#d5f5e3,stroke:#196f3d
    classDef extension fill:#fdebd0,stroke:#d35400
    classDef type fill:#ebf5fb,stroke:#2e86c1
    classDef error fill:#f9ebea,stroke:#c0392b
    classDef schema fill:#e8f8f5,stroke:#16a085

    %% Click Events
    click API "https://github.com/hapijs/joi/blob/master/lib/index.js"
    click TSD "https://github.com/hapijs/joi/blob/master/lib/index.d.ts"
    click SH "https://github.com/hapijs/joi/blob/master/lib/schemas.js"
    click TS "https://github.com/hapijs/joi/blob/master/lib/template.js"
    click VE "https://github.com/hapijs/joi/blob/master/lib/validator.js"
    click Cache "https://github.com/hapijs/joi/blob/master/lib/cache.js"
    click Ref "https://github.com/hapijs/joi/blob/master/lib/ref.js"
    click State "https://github.com/hapijs/joi/blob/master/lib/state.js"
    click Common "https://github.com/hapijs/joi/blob/master/lib/common.js"
    click Extend "https://github.com/hapijs/joi/blob/master/lib/extend.js"
    click Messages "https://github.com/hapijs/joi/blob/master/lib/messages.js"
    click String "https://github.com/hapijs/joi/blob/master/lib/types/string.js"
    click Number "https://github.com/hapijs/joi/blob/master/lib/types/number.js"
    click Array "https://github.com/hapijs/joi/blob/master/lib/types/array.js"
    click Object "https://github.com/hapijs/joi/blob/master/lib/types/object.js"
    click Boolean "https://github.com/hapijs/joi/blob/master/lib/types/boolean.js"
    click Date "https://github.com/hapijs/joi/blob/master/lib/types/date.js"
    click Function "https://github.com/hapijs/joi/blob/master/lib/types/function.js"
    click Binary "https://github.com/hapijs/joi/blob/master/lib/types/binary.js"
    click Symbol "https://github.com/hapijs/joi/blob/master/lib/types/symbol.js"
    click EH "https://github.com/hapijs/joi/blob/master/lib/errors.js"
    click Browser "https://github.com/hapijs/joi/tree/master/browser/"
    click Bench "https://github.com/hapijs/joi/tree/master/benchmarks/"
    click Test "https://github.com/hapijs/joi/tree/master/test/"
```
