Skip to content

Commit 4a46c2b

Browse files
authored
Merge pull request #2769 from afbjorklund/schema-test
Validate the jsonschema against default.yaml
2 parents bc7788b + 157f7a4 commit 4a46c2b

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ jobs:
186186
run: make
187187
- name: Install
188188
run: make install
189+
- name: Validate jsonschema
190+
run: make schema-limayaml.json
189191
- name: Validate templates
190192
run: find -L templates -name '*.yaml' | xargs limactl validate
191193
- name: Install test dependencies

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ endif
423423

424424
################################################################################
425425
schema-limayaml.json: _output/bin/limactl$(exe)
426-
$< generate-jsonschema >$@
426+
ifeq ($(native_compiling),true)
427+
$< generate-jsonschema --schemafile $@ examples/default.yaml
428+
endif
427429

428430
.PHONY: check-jsonschema
429431
check-jsonschema: schema-limayaml.json

cmd/limactl/genschema.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

8+
"github.com/goccy/go-yaml"
79
"github.com/invopop/jsonschema"
810
"github.com/lima-vm/lima/pkg/limayaml"
11+
jsonschema2 "github.com/santhosh-tekuri/jsonschema/v6"
912
"github.com/spf13/cobra"
1013
orderedmap "github.com/wk8/go-ordered-map/v2"
14+
15+
"github.com/sirupsen/logrus"
1116
)
1217

1318
func newGenSchemaCommand() *cobra.Command {
1419
genschemaCommand := &cobra.Command{
1520
Use: "generate-jsonschema",
1621
Short: "Generate json-schema document",
17-
Args: WrapArgsError(cobra.NoArgs),
22+
Args: WrapArgsError(cobra.ArbitraryArgs),
1823
RunE: genschemaAction,
1924
Hidden: true,
2025
}
26+
genschemaCommand.Flags().String("schemafile", "", "Output file")
2127
return genschemaCommand
2228
}
2329

@@ -37,7 +43,12 @@ func getProp(props *orderedmap.OrderedMap[string, *jsonschema.Schema], key strin
3743
return value
3844
}
3945

40-
func genschemaAction(cmd *cobra.Command, _ []string) error {
46+
func genschemaAction(cmd *cobra.Command, args []string) error {
47+
file, err := cmd.Flags().GetString("schemafile")
48+
if err != nil {
49+
return err
50+
}
51+
4152
schema := jsonschema.Reflect(&limayaml.LimaYAML{})
4253
// allow Disk to be either string (name) or object (struct)
4354
schema.Definitions["Disk"].Type = "" // was: "object"
@@ -54,6 +65,39 @@ func genschemaAction(cmd *cobra.Command, _ []string) error {
5465
if err != nil {
5566
return err
5667
}
57-
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(j))
68+
if len(args) == 0 {
69+
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(j))
70+
return err
71+
}
72+
73+
if file == "" {
74+
return fmt.Errorf("need --schemafile to validate")
75+
}
76+
err = os.WriteFile(file, j, 0o644)
77+
if err != nil {
78+
return err
79+
}
80+
compiler := jsonschema2.NewCompiler()
81+
schema2, err := compiler.Compile(file)
82+
if err != nil {
83+
return err
84+
}
85+
for _, f := range args {
86+
b, err := os.ReadFile(f)
87+
if err != nil {
88+
return err
89+
}
90+
var y interface{}
91+
err = yaml.Unmarshal(b, &y)
92+
if err != nil {
93+
return err
94+
}
95+
err = schema2.Validate(y)
96+
if err != nil {
97+
return fmt.Errorf("%q: %w", f, err)
98+
}
99+
logrus.Infof("%q: OK", f)
100+
}
101+
58102
return err
59103
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/opencontainers/go-digest v1.0.0
3737
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
3838
github.com/rjeczalik/notify v0.9.3
39+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
3940
github.com/sethvargo/go-password v0.3.1
4041
github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af
4142
github.com/spf13/cobra v1.8.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ github.com/diskfs/go-diskfs v1.4.1 h1:iODgkzHLmvXS+1VDztpW53T+dQm8GQzi20y9yUd5UC
6767
github.com/diskfs/go-diskfs v1.4.1/go.mod h1:+tOkQs8CMMog6Nvljg8DGIxEXrgL48iyT3OM3IlSz74=
6868
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
6969
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
70+
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
71+
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
7072
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
7173
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
7274
github.com/elastic/go-libaudit/v2 v2.5.0 h1:5OK919QRnGtcjVBz3n/cs5F42im1mPlVTA9TyIn2K54=
@@ -253,6 +255,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
253255
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
254256
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
255257
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
258+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
259+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
256260
github.com/sethvargo/go-password v0.3.1 h1:WqrLTjo7X6AcVYfC6R7GtSyuUQR9hGyAj/f1PYQZCJU=
257261
github.com/sethvargo/go-password v0.3.1/go.mod h1:rXofC1zT54N7R8K/h1WDUdkf9BOx5OptoxrMBcrXzvs=
258262
github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af h1:Sp5TG9f7K39yfB+If0vjp97vuT74F72r8hfRpP8jLU0=

0 commit comments

Comments
 (0)