Skip to content

Commit 34b0e40

Browse files
authored
Merge pull request #3515 from afbjorklund/schema-util
Move jsonschema validation into a utility dir
2 parents d33a05d + 0ad9de1 commit 34b0e40

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

@@ -93,22 +92,8 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
9392
if err != nil {
9493
return err
9594
}
96-
compiler := jsonschema2.NewCompiler()
97-
schema2, err := compiler.Compile(file)
98-
if err != nil {
99-
return err
100-
}
10195
for _, f := range args {
102-
b, err := os.ReadFile(f)
103-
if err != nil {
104-
return err
105-
}
106-
var y any
107-
err = yaml.Unmarshal(b, &y)
108-
if err != nil {
109-
return err
110-
}
111-
err = schema2.Validate(y)
96+
err = jsonschemautil.Validate(file, f)
11297
if err != nil {
11398
return fmt.Errorf("%q: %w", f, err)
11499
}

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)