|
| 1 | +# JSON Schema Module for Modular |
| 2 | + |
| 3 | +[](https://pkg.go.dev/github.com/GoCodeAlone/modular/modules/jsonschema) |
| 4 | + |
| 5 | +A [Modular](https://github.com/GoCodeAlone/modular) module that provides JSON Schema validation capabilities. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The JSON Schema module provides a service for validating JSON data against JSON Schema specifications. It wraps [github.com/santhosh-tekuri/jsonschema/v6](https://github.com/santhosh-tekuri/jsonschema) to provide a clean, service-oriented interface that integrates with the Modular framework. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Compile JSON schemas from file paths or URLs |
| 14 | +- Validate JSON data in multiple formats: |
| 15 | + - Raw JSON bytes |
| 16 | + - io.Reader interface |
| 17 | + - Go interface{} values |
| 18 | +- Simple integration with other Modular modules |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +go get github.com/GoCodeAlone/modular/modules/jsonschema |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Registering the Module |
| 29 | + |
| 30 | +```go |
| 31 | +import ( |
| 32 | + "github.com/GoCodeAlone/modular" |
| 33 | + "github.com/GoCodeAlone/modular/modules/jsonschema" |
| 34 | +) |
| 35 | + |
| 36 | +func main() { |
| 37 | + app := modular.NewStdApplication( |
| 38 | + modular.NewStdConfigProvider(nil), |
| 39 | + logger, |
| 40 | + ) |
| 41 | + |
| 42 | + // Register the JSON Schema module |
| 43 | + app.RegisterModule(jsonschema.NewModule()) |
| 44 | + |
| 45 | + // Register your modules that depend on the schema service |
| 46 | + app.RegisterModule(NewYourModule()) |
| 47 | + |
| 48 | + // Run the application |
| 49 | + if err := app.Run(); err != nil { |
| 50 | + logger.Error("Application error", "error", err) |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Using the JSON Schema Service |
| 56 | + |
| 57 | +```go |
| 58 | +type YourModule struct { |
| 59 | + schemaService jsonschema.JSONSchemaService |
| 60 | +} |
| 61 | + |
| 62 | +// Request the JSON Schema service |
| 63 | +func (m *YourModule) RequiresServices() []modular.ServiceDependency { |
| 64 | + return []modular.ServiceDependency{ |
| 65 | + { |
| 66 | + Name: "jsonschema.service", |
| 67 | + Required: true, |
| 68 | + SatisfiesInterface: reflect.TypeOf((*jsonschema.JSONSchemaService)(nil)).Elem(), |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Inject the service using constructor injection |
| 74 | +func (m *YourModule) Constructor() modular.ModuleConstructor { |
| 75 | + return func(app *modular.StdApplication, services map[string]any) (modular.Module, error) { |
| 76 | + schemaService, ok := services["jsonschema.service"].(jsonschema.JSONSchemaService) |
| 77 | + if !ok { |
| 78 | + return nil, fmt.Errorf("service 'jsonschema.service' not found or wrong type") |
| 79 | + } |
| 80 | + |
| 81 | + return &YourModule{ |
| 82 | + schemaService: schemaService, |
| 83 | + }, nil |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Example of using the schema service |
| 88 | +func (m *YourModule) ValidateData(schemaPath string, data []byte) error { |
| 89 | + // Compile the schema |
| 90 | + schema, err := m.schemaService.CompileSchema(schemaPath) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to compile schema: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Validate data against the schema |
| 96 | + if err := m.schemaService.ValidateBytes(schema, data); err != nil { |
| 97 | + return fmt.Errorf("validation failed: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## API Reference |
| 105 | + |
| 106 | +### Types |
| 107 | + |
| 108 | +#### Schema |
| 109 | + |
| 110 | +```go |
| 111 | +type Schema interface { |
| 112 | + // Validate validates the given value against the JSON schema |
| 113 | + Validate(value interface{}) error |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### JSONSchemaService |
| 118 | + |
| 119 | +```go |
| 120 | +type JSONSchemaService interface { |
| 121 | + // CompileSchema compiles a JSON schema from a file path or URL |
| 122 | + CompileSchema(source string) (Schema, error) |
| 123 | + |
| 124 | + // ValidateBytes validates raw JSON data against a compiled schema |
| 125 | + ValidateBytes(schema Schema, data []byte) error |
| 126 | + |
| 127 | + // ValidateReader validates JSON from an io.Reader against a compiled schema |
| 128 | + ValidateReader(schema Schema, reader io.Reader) error |
| 129 | + |
| 130 | + // ValidateInterface validates a Go interface{} against a compiled schema |
| 131 | + ValidateInterface(schema Schema, data interface{}) error |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +[MIT License](../../LICENSE) |
0 commit comments