WIP - upgrade jsonschema to v6

This commit is contained in:
Yann Hamon 2025-02-16 18:18:49 +01:00
parent 1bd44986dd
commit 35718141ea
113 changed files with 24459 additions and 4755 deletions

View file

@ -6,10 +6,11 @@ import (
"context"
"errors"
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
"io"
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
jsonschema "github.com/santhosh-tekuri/jsonschema/v6"
"github.com/yannh/kubeconform/pkg/cache"
"github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource"
@ -197,9 +198,13 @@ func (val *v) ValidateResource(res resource.Resource) Result {
var e *jsonschema.ValidationError
if errors.As(err, &e) {
for _, ve := range e.Causes {
path := ""
for _, f := range ve.InstanceLocation {
path = path + "/" + f
}
validationErrors = append(validationErrors, ValidationError{
Path: ve.InstanceLocation,
Msg: ve.Message,
Path: path,
Msg: ve.LocalizedError(message.NewPrinter(language.English)),
})
}
@ -249,16 +254,21 @@ func (val *v) Validate(filename string, r io.ReadCloser) []Result {
}
func downloadSchema(registries []registry.Registry, kind, version, k8sVersion string) (*jsonschema.Schema, error) {
var err error
var schemaBytes []byte
var path string
var err error
for _, reg := range registries {
path, schemaBytes, err = reg.DownloadSchema(kind, version, k8sVersion)
if err == nil {
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schemaBytes))
if err != nil {
continue
}
c := jsonschema.NewCompiler()
c.Draft = jsonschema.Draft4
if err := c.AddResource(path, bytes.NewReader(schemaBytes)); err != nil {
//c.Draft = jsonschema.Draft4
c.DefaultDraft(jsonschema.Draft4)
if err := c.AddResource(path, s); err != nil {
continue
}
schema, err := c.Compile(path)

View file

@ -406,11 +406,11 @@ lastName: bar
}
if len(got.ValidationErrors) != len(testCase.expectErrors) {
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
t.Errorf("Test '%s': expected ValidationErrors: %+#v, got: %+#v", testCase.name, testCase.expectErrors, got.ValidationErrors)
}
for i, _ := range testCase.expectErrors {
if testCase.expectErrors[i] != got.ValidationErrors[i] {
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
t.Errorf("Test '%s': expected ValidationErrors: %+#v, got: %+#v", testCase.name, testCase.expectErrors, got.ValidationErrors)
}
}
}
@ -467,7 +467,7 @@ age: not a number
got := val.ValidateResource(resource.Resource{Bytes: rawResource})
if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) {
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
t.Errorf("Expected %+#v, got %+#v", expectedErrors, got.ValidationErrors)
}
}
@ -529,6 +529,6 @@ firstName: foo
t.Errorf("Expected %+v, got %+v", expectedStatuses, gotStatuses)
}
if !reflect.DeepEqual(expectedValidationErrors, gotValidationErrors) {
t.Errorf("Expected %+v, got %+v", expectedValidationErrors, gotValidationErrors)
t.Errorf("Expected %+#v, got %+#v", expectedValidationErrors, gotValidationErrors)
}
}