-
Notifications
You must be signed in to change notification settings - Fork 21
/
schema_test.go
53 lines (40 loc) · 1.05 KB
/
schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"bytes"
"encoding/json"
"errors"
"os"
"testing"
"github.com/rliebz/ghost"
"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"
)
func TestJSONSchema(t *testing.T) {
g := ghost.New(t)
compiler := jsonschema.NewCompiler()
schemaFile, err := os.Open("tusk.schema.yaml")
g.NoError(err)
t.Cleanup(func() { g.NoError(schemaFile.Close()) })
var schemaData any
err = yaml.NewDecoder(schemaFile).Decode(&schemaData)
g.NoError(err)
jsonSchema, err := json.Marshal(schemaData)
g.NoError(err)
err = compiler.AddResource("tusk.schema.json", bytes.NewReader(jsonSchema))
g.NoError(err)
schema, err := compiler.Compile("tusk.schema.json")
g.NoError(err)
tuskFile, err := os.Open("tusk.yml")
g.NoError(err)
t.Cleanup(func() { g.NoError(tuskFile.Close()) })
var v any
err = yaml.NewDecoder(tuskFile).Decode(&v)
g.NoError(err)
err = schema.Validate(v)
var vde *jsonschema.ValidationError
if errors.As(err, &vde) {
b, err := json.MarshalIndent(vde.DetailedOutput(), "", " ")
g.NoError(err)
t.Fatal(string(b))
}
}