Skip to content

Commit 9981615

Browse files
committed
Added readme for jsonschema
1 parent d75aa44 commit 9981615

File tree

3 files changed

+143
-8
lines changed

3 files changed

+143
-8
lines changed

modules/jsonschema/README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# JSON Schema Module for Modular
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/GoCodeAlone/modular/modules/jsonschema.svg)](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)

modules/jsonschema/module.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@ import (
66
)
77

88
type Module struct {
9-
app modular.Application
109
schemaService JSONSchemaService
1110
}
1211

13-
func NewModule(application modular.Application) *Module {
12+
func NewModule() *Module {
1413
return &Module{
15-
app: application,
1614
schemaService: NewJSONSchemaService(),
1715
}
1816
}
1917

20-
func (m *Module) RegisterConfig(app modular.Application) {
18+
func (m *Module) RegisterConfig(modular.Application) {
2119
// nothing to do
2220
}
2321

24-
func (m *Module) Init(app modular.Application) error {
22+
func (m *Module) Init(modular.Application) error {
2523
return nil
2624
}
2725

28-
func (m *Module) Start(ctx context.Context) error {
26+
func (m *Module) Start(context.Context) error {
2927
// Nothing special needed for startup
3028
return nil
3129
}
3230

33-
func (m *Module) Stop(ctx context.Context) error {
31+
func (m *Module) Stop(context.Context) error {
3432
// Nothing special needed for shutdown
3533
return nil
3634
}

modules/jsonschema/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestExample_dependentModule(t *testing.T) {
183183
)),
184184
)
185185

186-
app.RegisterModule(jsonschema.NewModule(app))
186+
app.RegisterModule(jsonschema.NewModule())
187187
app.RegisterModule(&YourModule{t: t})
188188

189189
if err := app.Init(); err != nil {

0 commit comments

Comments
 (0)