Skip to content

Commit 09b1456

Browse files
authored
Merge pull request #378 from dgageot/check-json-schema
Check json schema
2 parents e08564f + 1f4fd45 commit 09b1456

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

cagent-schema.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"version": {
99
"type": "string",
1010
"description": "Configuration version",
11-
"enum": ["2", "v2"],
11+
"enum": ["2"],
1212
"examples": ["2"]
1313
},
1414
"agents": {
@@ -240,9 +240,12 @@
240240
"shell": {
241241
"type": "object",
242242
"description": "Shell script configurations (for script tool)",
243-
"additionalProperties": {
244-
"$ref": "#/definitions/ScriptShellToolConfig"
245-
}
243+
"patternProperties": {
244+
"^[A-Za-z_][A-Za-z0-9_\\-]*$": {
245+
"$ref": "#/definitions/ScriptShellToolConfig"
246+
}
247+
},
248+
"additionalProperties": false
246249
},
247250
"post_edit": {
248251
"type": "array",
@@ -342,7 +345,6 @@
342345
"description": "Working directory for the command"
343346
}
344347
},
345-
"required": ["cmd", "description", "required"],
346348
"additionalProperties": false
347349
},
348350
"PostEditConfig": {

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/sashabaranov/go-openai v1.41.2
2222
github.com/spf13/cobra v1.10.1
2323
github.com/stretchr/testify v1.11.1
24+
github.com/xeipuuv/gojsonschema v1.2.0
2425
go.opentelemetry.io/otel v1.38.0
2526
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.38.0
2627
go.opentelemetry.io/otel/sdk v1.38.0
@@ -97,6 +98,8 @@ require (
9798
github.com/valyala/fasttemplate v1.2.2 // indirect
9899
github.com/vbatts/tar-split v0.12.1 // indirect
99100
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
101+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
102+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
100103
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
101104
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
102105
github.com/yuin/goldmark v1.7.13 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
178178
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
179179
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
180180
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
181+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
181182
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
182183
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
183184
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
@@ -199,6 +200,12 @@ github.com/vbatts/tar-split v0.12.1 h1:CqKoORW7BUWBe7UL/iqTVvkTBOF8UvOMKOIZykxnn
199200
github.com/vbatts/tar-split v0.12.1/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA=
200201
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
201202
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
203+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
204+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
205+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
206+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
207+
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
208+
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
202209
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
203210
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
204211
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=

pkg/config/examples_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ package config
22

33
import (
44
"io/fs"
5+
"os"
56
"path/filepath"
67
"testing"
78

9+
"github.com/goccy/go-yaml"
810
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/require"
12+
"github.com/xeipuuv/gojsonschema"
1013
)
1114

12-
func TestParseExamples(t *testing.T) {
15+
func collectExamples(t *testing.T) []string {
16+
t.Helper()
17+
1318
var files []string
14-
err := filepath.WalkDir("../../examples", func(path string, d fs.DirEntry, err error) error {
19+
err := filepath.WalkDir(filepath.Join("..", "..", "examples"), func(path string, d fs.DirEntry, err error) error {
1520
if err != nil {
1621
return err
1722
}
@@ -23,7 +28,11 @@ func TestParseExamples(t *testing.T) {
2328
require.NoError(t, err)
2429
assert.NotEmpty(t, files)
2530

26-
for _, file := range files {
31+
return files
32+
}
33+
34+
func TestParseExamples(t *testing.T) {
35+
for _, file := range collectExamples(t) {
2736
t.Run(file, func(t *testing.T) {
2837
t.Parallel()
2938

@@ -36,3 +45,29 @@ func TestParseExamples(t *testing.T) {
3645
})
3746
}
3847
}
48+
49+
func TestJsonSchemaWorksForExamples(t *testing.T) {
50+
// Read json schema.
51+
schemaFile, err := os.ReadFile(filepath.Join("..", "..", "cagent-schema.json"))
52+
require.NoError(t, err)
53+
54+
schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader(schemaFile))
55+
require.NoError(t, err)
56+
57+
for _, file := range collectExamples(t) {
58+
t.Run(file, func(t *testing.T) {
59+
t.Parallel()
60+
61+
buf, err := os.ReadFile(file)
62+
require.NoError(t, err)
63+
64+
var rawJSON any
65+
err = yaml.Unmarshal(buf, &rawJSON)
66+
require.NoError(t, err)
67+
68+
result, err := schema.Validate(gojsonschema.NewRawLoader(rawJSON))
69+
require.NoError(t, err)
70+
assert.True(t, result.Valid(), "Example %s does not match schema: %v", file, result.Errors())
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)