jsonschema

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2025 License: MIT Imports: 5 Imported by: 1

README

JSON Schema Module for Modular

Go Reference Modules CI

A Modular module that provides JSON Schema validation capabilities.

Overview

The JSON Schema module provides a service for validating JSON data against JSON Schema specifications. It wraps github.com/santhosh-tekuri/jsonschema/v6 to provide a clean, service-oriented interface that integrates with the Modular framework.

Features

  • Compile JSON schemas from file paths or URLs
  • Validate JSON data in multiple formats:
    • Raw JSON bytes
    • io.Reader interface
    • Go interface{} values
  • Simple integration with other Modular modules

Installation

go get github.com/GoCodeAlone/modular/modules/jsonschema@v1.0.0

Usage

Registering the Module
import (
    "github.com/GoCodeAlone/modular"
    "github.com/GoCodeAlone/modular/modules/jsonschema"
)

func main() {
    app := modular.NewStdApplication(
        modular.NewStdConfigProvider(nil),
        logger,
    )
    
    // Register the JSON Schema module
    app.RegisterModule(jsonschema.NewModule())
    
    // Register your modules that depend on the schema service
    app.RegisterModule(NewYourModule())
    
    // Run the application
    if err := app.Run(); err != nil {
        logger.Error("Application error", "error", err)
    }
}
Using the JSON Schema Service
type YourModule struct {
    schemaService jsonschema.JSONSchemaService
}

// Request the JSON Schema service
func (m *YourModule) RequiresServices() []modular.ServiceDependency {
    return []modular.ServiceDependency{
        {
            Name:               "jsonschema.service",
            Required:           true,
            SatisfiesInterface: reflect.TypeOf((*jsonschema.JSONSchemaService)(nil)).Elem(),
        },
    }
}

// Inject the service using constructor injection
func (m *YourModule) Constructor() modular.ModuleConstructor {
    return func(app *modular.StdApplication, services map[string]any) (modular.Module, error) {
        schemaService, ok := services["jsonschema.service"].(jsonschema.JSONSchemaService)
        if !ok {
            return nil, fmt.Errorf("service 'jsonschema.service' not found or wrong type")
        }
        
        return &YourModule{
            schemaService: schemaService,
        }, nil
    }
}

// Example of using the schema service
func (m *YourModule) ValidateData(schemaPath string, data []byte) error {
    // Compile the schema
    schema, err := m.schemaService.CompileSchema(schemaPath)
    if err != nil {
        return fmt.Errorf("failed to compile schema: %w", err)
    }
    
    // Validate data against the schema
    if err := m.schemaService.ValidateBytes(schema, data); err != nil {
        return fmt.Errorf("validation failed: %w", err)
    }
    
    return nil
}

API Reference

Types
Schema
type Schema interface {
    // Validate validates the given value against the JSON schema
    Validate(value interface{}) error
}
JSONSchemaService
type JSONSchemaService interface {
    // CompileSchema compiles a JSON schema from a file path or URL
    CompileSchema(source string) (Schema, error)
    
    // ValidateBytes validates raw JSON data against a compiled schema
    ValidateBytes(schema Schema, data []byte) error
    
    // ValidateReader validates JSON from an io.Reader against a compiled schema
    ValidateReader(schema Schema, reader io.Reader) error
    
    // ValidateInterface validates a Go interface{} against a compiled schema
    ValidateInterface(schema Schema, data interface{}) error
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONSchemaService

type JSONSchemaService interface {
	// CompileSchema compiles a JSON schema from a file path or URL
	CompileSchema(source string) (Schema, error)

	// ValidateBytes validates raw JSON data against a compiled schema
	ValidateBytes(schema Schema, data []byte) error

	// ValidateReader validates JSON from an io.Reader against a compiled schema
	ValidateReader(schema Schema, reader io.Reader) error

	// ValidateInterface validates a Go interface{} against a compiled schema
	ValidateInterface(schema Schema, data interface{}) error
}

JSONSchemaService defines the operations that can be performed with JSON schemas

func NewJSONSchemaService

func NewJSONSchemaService() JSONSchemaService

NewJSONSchemaService creates a new JSON schema service

type Module

type Module struct {
	// contains filtered or unexported fields
}

func NewModule

func NewModule() *Module

func (*Module) Name

func (m *Module) Name() string

func (*Module) ProvidesServices

func (m *Module) ProvidesServices() []modular.ServiceProvider

func (*Module) RequiresServices

func (m *Module) RequiresServices() []modular.ServiceDependency

type Schema

type Schema interface {
	// Validate validates the given value against the JSON schema
	Validate(value interface{}) error
}

Schema represents a compiled JSON schema

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL