Skip to content

Commit f76665a

Browse files
authored
Merge pull request #3514 from afbjorklund/schema-embed
Make the jsonschema validate the base
2 parents fcd67bb + 2c41fb0 commit f76665a

File tree

8 files changed

+26
-25
lines changed

8 files changed

+26
-25
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,15 @@ ifeq ($(native_compiling),true)
436436
$< tmpl copy --embed-all templates/default.yaml $@
437437
endif
438438

439-
schema-limayaml.json: _output/bin/limactl$(exe) default-template.yaml
439+
schema-limayaml.json: _output/bin/limactl$(exe) templates/default.yaml default-template.yaml
440440
ifeq ($(native_compiling),true)
441-
$< generate-jsonschema --schemafile $@ default-template.yaml
441+
# validate both the original template (with the "base" etc), and the embedded template
442+
$< generate-jsonschema --schemafile $@ templates/default.yaml default-template.yaml
442443
endif
443444

444445
.PHONY: check-jsonschema
445-
check-jsonschema: schema-limayaml.json default-template.yaml
446-
check-jsonschema --schemafile schema-limayaml.json default-template.yaml
446+
check-jsonschema: schema-limayaml.json templates/default.yaml default-template.yaml
447+
check-jsonschema --schemafile schema-limayaml.json templates/default.yaml default-template.yaml
447448

448449
################################################################################
449450
.PHONY: diagrams

cmd/limactl/genschema.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
6060
{Type: "string"},
6161
{Type: "object"},
6262
}
63+
// allow BaseTemplates to be either string (url) or array (array)
64+
schema.Definitions["BaseTemplates"].Type = "" // was: "array"
65+
schema.Definitions["BaseTemplates"].OneOf = []*jsonschema.Schema{
66+
{Type: "string"},
67+
{Type: "array"},
68+
}
69+
// allow LocatorWithDigest to be either string (url) or object (struct)
70+
schema.Definitions["LocatorWithDigest"].Type = "" // was: "object"
71+
schema.Definitions["LocatorWithDigest"].OneOf = []*jsonschema.Schema{
72+
{Type: "string"},
73+
{Type: "object"},
74+
}
6375
properties := schema.Definitions["LimaYAML"].Properties
6476
getProp(properties, "os").Enum = toAny(limayaml.OSTypes)
6577
getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes)

pkg/limatmpl/abs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ var useAbsLocatorsTestCases = []useAbsLocatorsTestCase{
3535
{
3636
"Flow style array of one base template",
3737
"template://foo",
38-
`base: {url: bar.yaml, digest: deadbeef}`,
38+
`base: [{url: bar.yaml, digest: deadbeef}]`,
3939
// not sure why the quotes around the URL were added; maybe because we don't copy the style from the source
40-
`base: {url: 'template://bar.yaml', digest: deadbeef}`,
40+
`base: [{url: 'template://bar.yaml', digest: deadbeef}]`,
4141
},
4242
{
4343
"Flow style array of sequence of two base URLs",

pkg/limatmpl/embed_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,7 @@ provision:
374374
`provision: [{script: "#! my script"}]`,
375375
},
376376
{
377-
"ERROR base digest is not yet implemented (1)",
378-
"",
379-
"base: {url: base.yaml, digest: deafbad}",
380-
"not yet implemented",
381-
},
382-
{
383-
"ERROR base digest is not yet implemented (2)",
377+
"ERROR base digest is not yet implemented",
384378
"",
385379
"base: [{url: base.yaml, digest: deafbad}]",
386380
"not yet implemented",

pkg/limayaml/limayaml.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
)
1111

1212
type LimaYAML struct {
13-
Base BaseTemplates `yaml:"base,omitempty" json:"base,omitempty" jsonschema:"nullable"`
13+
Base BaseTemplates `yaml:"base,omitempty" json:"base,omitempty"`
1414
MinimumLimaVersion *string `yaml:"minimumLimaVersion,omitempty" json:"minimumLimaVersion,omitempty" jsonschema:"nullable"`
1515
VMType *VMType `yaml:"vmType,omitempty" json:"vmType,omitempty" jsonschema:"nullable"`
1616
VMOpts VMOpts `yaml:"vmOpts,omitempty" json:"vmOpts,omitempty"`
1717
OS *OS `yaml:"os,omitempty" json:"os,omitempty" jsonschema:"nullable"`
1818
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty" jsonschema:"nullable"`
19-
Images []Image `yaml:"images" json:"images"` // REQUIRED
19+
Images []Image `yaml:"images,omitempty" json:"images,omitempty" jsonschema:"nullable"`
2020
CPUType CPUType `yaml:"cpuType,omitempty" json:"cpuType,omitempty" jsonschema:"nullable"`
2121
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty" jsonschema:"nullable"`
2222
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty" jsonschema:"nullable"` // go-units.RAMInBytes
@@ -58,7 +58,7 @@ type BaseTemplates []LocatorWithDigest
5858

5959
type LocatorWithDigest struct {
6060
URL string `yaml:"url" json:"url"`
61-
Digest *string `yaml:"digest,omitempty" json:"digest,omitempty" jsonschema:"nullable"` // TODO currently unused
61+
Digest *string `yaml:"digest,omitempty" json:"digest,omitempty"` // TODO currently unused
6262
}
6363

6464
type (
@@ -229,7 +229,7 @@ const (
229229
type Provision struct {
230230
Mode ProvisionMode `yaml:"mode,omitempty" json:"mode,omitempty" jsonschema:"default=system"`
231231
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
232-
Script string `yaml:"script" json:"script"`
232+
Script string `yaml:"script,omitempty" json:"script,omitempty"`
233233
File *LocatorWithDigest `yaml:"file,omitempty" json:"file,omitempty" jsonschema:"nullable"`
234234
Playbook string `yaml:"playbook,omitempty" json:"playbook,omitempty"` // DEPRECATED
235235
// All ProvisionData fields must be nil unless Mode is ProvisionModeData

pkg/limayaml/limayaml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func dumpJSON(t *testing.T, d any) string {
1717
return string(b)
1818
}
1919

20-
const emptyYAML = "images: []\n"
20+
const emptyYAML = "{}\n"
2121

2222
func TestEmptyYAML(t *testing.T) {
2323
var y LimaYAML
@@ -27,7 +27,7 @@ func TestEmptyYAML(t *testing.T) {
2727
assert.Equal(t, string(b), emptyYAML)
2828
}
2929

30-
const defaultYAML = "images: []\n"
30+
const defaultYAML = "{}\n"
3131

3232
func TestDefaultYAML(t *testing.T) {
3333
bytes, err := os.ReadFile("default.yaml")

pkg/limayaml/marshal.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ func unmarshalBaseTemplates(dst *BaseTemplates, b []byte) error {
4545
*dst = BaseTemplates{LocatorWithDigest{URL: s}}
4646
return nil
4747
}
48-
var locator LocatorWithDigest
49-
if err := yaml.Unmarshal(b, &locator); err == nil {
50-
*dst = BaseTemplates{locator}
51-
return nil
52-
}
5348
return yaml.UnmarshalWithOptions(b, dst, yaml.CustomUnmarshaler[LocatorWithDigest](unmarshalLocatorWithDigest))
5449
}
5550

pkg/limayaml/marshal_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func TestMarshalTilde(t *testing.T) {
2828
// yaml will load ~ (or null) as null
2929
// make sure that it is always quoted
3030
assert.Equal(t, string(b), `---
31-
images: []
3231
mounts:
3332
- location: "~"
3433
writable: false

0 commit comments

Comments
 (0)