Skip to content

[go-fan] Go Module Review: jsonschema #706

@github-actions

Description

@github-actions

🐹 Go Fan Report: santhosh-tekuri/jsonschema

Module Overview

The github.com/santhosh-tekuri/jsonschema module is a comprehensive JSON Schema validation library that supports multiple draft specifications (Draft 4, 6, 7, 2019-09, and 2020-12). It provides fast, spec-compliant validation with excellent error reporting capabilities.

Repository: https://github.com/santhosh-tekuri/jsonschema
Current Version: v5.3.1 (April 2024)
Latest Version: v6.0.2 (May 2025) ⚠️ Major upgrade available
Stars: 1,200+ | License: Apache 2.0

Current Usage in gh-aw-mcpg

Files & Import Count

This is one of our most heavily used validation modules:

  • internal/config/validation_schema.go (452 lines) - Core schema validation
  • internal/config/validation_schema_test.go (696 lines) - Test suite
  • internal/config/validation_schema_fetch_test.go (591 lines) - Fetch tests
  • internal/config/validation_string_patterns_test.go - Pattern tests

Total Impact: 1,739 lines of schema-related code

Key APIs Used

  1. Compiler API: NewCompiler(), compiler.Draft, AddResource(), Compile()
  2. Validation API: schema.Validate(), *ValidationError type
  3. Error Handling: Recursive error formatting with .Causes field

Usage Patterns

Excellent Implementation:

  • Schema caching with sync.Once for performance
  • Remote schema fetching from GitHub
  • Custom error formatter for user-friendly messages
  • Comprehensive test coverage (21+ test cases)

⚠️ Workaround Required:

  • fetchAndFixSchema() function applies regex fixes for Draft 7 limitations
  • 80 lines of workaround code for negative lookahead patterns

Research Findings

Recent Updates

The module was last updated December 16, 2025 with validation fixes. The maintainer (santhosh-tekuri) is actively maintaining the project.

Version 6.0.2 Released

A major new version (v6) was released in May 2025 on the boon branch with significant improvements:

  • Improved error handling with OutputError.String() method
  • Performance optimizations for validation
  • Better type validation support
  • Custom vocabulary support
  • API simplifications

Best Practices from Maintainer

The repository demonstrates:

  • Multi-draft spec support for flexibility
  • Detailed error messages with nested causes
  • Full $ref resolution for complex schemas
  • Extensible format validators
  • Remote schema fetching capabilities

Improvement Opportunities

🏃 Quick Wins

1. Pin Schema Version for Build Reproducibility

Priority: HIGH | Effort: 5 minutes

Current Issue:
The schema URL uses the main branch which can change unexpectedly:

schemaURL = "https://raw.githubusercontent.com/github/gh-aw/main/docs/public/schemas/mcp-gateway-config.schema.json"

Solution:
The code already has a TODO comment about this (lines 32-40 in validation_schema.go). Pin to a specific version:

schemaURL = "https://raw.githubusercontent.com/github/gh-aw/v1.0.0/docs/public/schemas/mcp-gateway-config.schema.json"

Benefits:

  • Predictable builds
  • No surprise breaking changes
  • Better debugging (know exact schema version)

2. Embed Schema with go:embed

Priority: MEDIUM | Effort: 15 minutes

Current Issue:
Network dependency with 10-second timeout on first validation.

Solution:

import _ "embed"

//go:embed mcp-gateway-config.schema.json
var embeddedSchema []byte

Benefits:

  • Offline gateway operation
  • Faster startup (no HTTP fetch)
  • Zero network dependency
  • Eliminates 10s timeout risk

3. Add Schema Validation Metrics

Priority: LOW | Effort: 10 minutes

Solution:

start := time.Now()
defer func() {
    logSchema.Printf("Schema validation took: %v", time.Since(start))
}()

Benefits:

  • Performance monitoring
  • Identify slow validations
  • Track error patterns

✨ Feature Opportunities

Major Opportunity: Upgrade to v6.0.2

Priority: HIGH | Impact: Medium-High | Effort: 2-4 hours

Why Upgrade?

  1. Eliminate Workarounds: The fetchAndFixSchema() function may no longer be needed - v6 might handle negative lookahead patterns natively
  2. Better Error Messages: Enhanced OutputError.String() method
  3. Performance Gains: Validation optimizations in v6
  4. Modern API: Cleaner, more type-safe interfaces
  5. Future-proof: Aligns with latest JSON Schema specifications

Migration Impact:

// Current (v5)
import "github.com/santhosh-tekuri/jsonschema/v5"

// Future (v6) - import path changes
import "github.com/santhosh-tekuri/jsonschema/v6"

Testing Required:

  • All 1,739 lines of schema code
  • 21+ validation test cases
  • Verify workaround necessity
  • Performance benchmarking

Challenges:

  • Breaking API changes (major version)
  • Need to update all import paths
  • Thorough testing across all use cases
  • May require code changes beyond imports

📐 Best Practice Alignment

Current Implementation Score: ⭐⭐⭐⭐½ (Excellent)

Perfectly Aligned:

  • Error type checking with type assertion + ok pattern
  • Compiler caching with sync.Once (essential for performance)
  • Explicit Draft selection (compiler.Draft = jsonschema.Draft7)

Already Following Best Practices:

  • Detailed error formatting with recursive cause handling
  • User-friendly error messages with context
  • Comprehensive test coverage

🔧 General Improvements

1. Extract Schema Fixing Logic

Benefit: Better code organization and testability

The fetchAndFixSchema() function (80 lines) could be:

  • Moved to separate package: internal/config/schemafixes/
  • Individual unit tests for each fix
  • Documentation of which fixes become obsolete in v6

2. Enhance Error Context

Benefit: More helpful error messages

The formatErrorContext() function could use:

  • Structured error types instead of string matching
  • Schema-specific suggestions with examples
  • Links to relevant documentation sections

3. Add Schema Self-Validation Tests

Benefit: Ensure schema integrity

func TestSchemaIsValid(t *testing.T) {
    // Validate that our schema is a valid JSON Schema
    metaSchema := jsonschema.Draft7
    // Validate schema against meta-schema
}

Recommendations

Immediate Actions

  1. Pin schema version - Quick win, high impact
  2. Add validation metrics - Easy monitoring improvement

Short Term (1-2 sprints)

  1. 📦 Investigate v6 upgrade - Create spike/POC
  2. 🔧 Consider schema embedding - Remove network dependency

Medium Term (3-4 sprints)

  1. 🏗️ Refactor schema fixes - If staying on v5, improve organization
  2. 📚 Enhance error messages - Add examples and better context

Prioritized Action Plan

  1. Week 1: Pin schema version (30 min) + Add metrics (30 min)
  2. Week 2: Create v6 upgrade spike branch
  3. Week 3: Test v6 with all validation tests
  4. Week 4: Evaluate v6 results and decide on migration

Technical Details

Why This Module Was Chosen

Perfect fit for MCP Gateway configuration validation:

  • ✅ Spec compliance across multiple JSON Schema drafts
  • ✅ Fast validation critical for gateway startup
  • ✅ Excellent error quality helps users fix configs
  • ✅ Active maintenance with regular updates

Current Workaround Analysis

The fetchAndFixSchema() function works around Draft 7 regex limitations:

Problem: Pattern ^(?!stdio$|http$).* uses negative lookahead (not universally supported)
Workaround: Replace with not: { enum: ["stdio", "http"] }
Result: Semantic equivalence - both reject "stdio" and "http" values

This is clever engineering, but adds 80 lines of complexity. v6 testing is crucial to see if this can be removed.

Schema Caching Strategy

The implementation uses an excellent pattern:

var (
    schemaOnce   sync.Once
    cachedSchema *jsonschema.Schema
    schemaErr    error
)

Benefits:

  • Thread-safe initialization
  • Single network fetch
  • Error caching (fail fast on repeated attempts)
  • Significant performance improvement

Security & Performance

Security Considerations

  • ✅ HTTPS schema fetching
  • ⚠️ Schema not pinned (version can change)
  • ✅ No sensitive data in validation

Recommendation: Pin schema version or embed for security hardening.

Performance Characteristics

  • Schema Compilation: Once at startup (cached) ✅
  • Validation: Sub-millisecond for typical configs ✅
  • Network Fetch: 10-second timeout on first validation ⚠️

Optimization: Embedding schema eliminates the 10s timeout entirely.

Next Steps

  1. Spike v6 Upgrade - Create feature branch and test
  2. Pin Schema Version - Quick security/reproducibility win
  3. Benchmark Tests - Add performance benchmarks
  4. Document Decision - Track v6 upgrade evaluation results

Generated by Go Fan 🐹
Module Analysis: Deep dive into 1,739 lines of schema validation code
Summary saved to: /tmp/gh-aw/cache-memory/jsonschema-review-2026-02-05.md
Review Date: February 5, 2026

Module Assessment: ⭐⭐⭐⭐⭐ (Excellent)
Usage Quality: ⭐⭐⭐⭐½ (Very Good - minor improvements possible)
Upgrade Priority: 🟡 Medium (v6 offers improvements, but v5 works well)

AI generated by Go Fan

  • expires on Feb 12, 2026, 7:33 AM UTC

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions