Skip to content

Commit 0ad9de1

Browse files
committed
Move jsonschema validation into a utility dir
This performs the same task as py "check-jsonschema" or "jv". But included with Lima, instead of being a separate CLI tool. Add simple example, for unit test. See https://tour.json-schema.org/ Signed-off-by: Anders F Björklund <[email protected]>
1 parent fcd67bb commit 0ad9de1

File tree

6 files changed

+70
-17
lines changed

6 files changed

+70
-17
lines changed

cmd/limactl/genschema.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"fmt"
1010
"os"
1111

12-
"github.com/goccy/go-yaml"
1312
"github.com/invopop/jsonschema"
13+
"github.com/lima-vm/lima/pkg/jsonschemautil"
1414
"github.com/lima-vm/lima/pkg/limayaml"
15-
jsonschema2 "github.com/santhosh-tekuri/jsonschema/v6"
1615
"github.com/spf13/cobra"
1716
orderedmap "github.com/wk8/go-ordered-map/v2"
1817

@@ -81,22 +80,8 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
8180
if err != nil {
8281
return err
8382
}
84-
compiler := jsonschema2.NewCompiler()
85-
schema2, err := compiler.Compile(file)
86-
if err != nil {
87-
return err
88-
}
8983
for _, f := range args {
90-
b, err := os.ReadFile(f)
91-
if err != nil {
92-
return err
93-
}
94-
var y any
95-
err = yaml.Unmarshal(b, &y)
96-
if err != nil {
97-
return err
98-
}
99-
err = schema2.Validate(y)
84+
err = jsonschemautil.Validate(file, f)
10085
if err != nil {
10186
return fmt.Errorf("%q: %w", f, err)
10287
}

pkg/jsonschemautil/jsonschemautil.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package jsonschemautil
5+
6+
import (
7+
"os"
8+
9+
"github.com/goccy/go-yaml"
10+
"github.com/santhosh-tekuri/jsonschema/v6"
11+
)
12+
13+
func Validate(schemafile, instancefile string) error {
14+
compiler := jsonschema.NewCompiler()
15+
schema, err := compiler.Compile(schemafile)
16+
if err != nil {
17+
return err
18+
}
19+
instance, err := os.ReadFile(instancefile)
20+
if err != nil {
21+
return err
22+
}
23+
var y any
24+
err = yaml.Unmarshal(instance, &y)
25+
if err != nil {
26+
return err
27+
}
28+
return schema.Validate(y)
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package jsonschemautil
5+
6+
import (
7+
"testing"
8+
9+
"gotest.tools/v3/assert"
10+
)
11+
12+
func TestValidateValidInstance(t *testing.T) {
13+
schema := "testdata/schema.json"
14+
instance := "testdata/valid.yaml"
15+
err := Validate(schema, instance)
16+
assert.NilError(t, err)
17+
}
18+
19+
func TestValidateInvalidInstance(t *testing.T) {
20+
schema := "testdata/schema.json"
21+
instance := "testdata/invalid.yaml"
22+
err := Validate(schema, instance)
23+
assert.ErrorContains(t, err, "jsonschema validation failed")
24+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: John Doe
2+
age: "30"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"name": {
5+
"type": "string"
6+
},
7+
"age": {
8+
"type": "integer"
9+
}
10+
}
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: John Doe
2+
age: 30

0 commit comments

Comments
 (0)