Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions code/go/internal/loader/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

// LoadSpec loads a package specification for the given version and type.
func LoadSpec(fsys fs.FS, version semver.Version, pkgType string) (spectypes.ItemSpec, error) {
fileSpecLoader := yamlschema.NewFileSchemaLoader()
loader := specschema.NewFolderSpecLoader(fsys, fileSpecLoader, version)
fileSpecLoader := yamlschema.NewFileSchemaLoader(fsys, version)
loader := specschema.NewFolderSpecLoader(fileSpecLoader)
spec, err := loader.Load(pkgType)
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("package type %q not supported (%w)", pkgType, err)
Expand Down
9 changes: 4 additions & 5 deletions code/go/internal/specschema/folder_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type FolderSpecLoader struct {

// NewFolderSpecLoader creates a new `FolderSpecLoader` that loads schemas from the given directories.
// File schemas referenced with `$ref` are loaded using the given `FileSchemaLoader`.
func NewFolderSpecLoader(fs fs.FS, fileLoader spectypes.FileSchemaLoader, version semver.Version) *FolderSpecLoader {
func NewFolderSpecLoader(fileLoader spectypes.FileSchemaLoader) *FolderSpecLoader {
return &FolderSpecLoader{
fs: fs,
fs: fileLoader.FS(),
fileSpecLoader: fileLoader,
specVersion: version,
specVersion: fileLoader.Version(),
}
}

Expand Down Expand Up @@ -111,11 +111,10 @@ func (l *FolderSpecLoader) loadContents(s *folderItemSpec, fs fs.FS, specPath st
}
specPath := path.Join(path.Dir(specPath), content.Ref)
options := spectypes.FileSchemaLoadOptions{
SpecVersion: l.specVersion,
Limits: &ItemSpec{content},
ContentType: content.ContentMediaType,
}
schema, err := l.fileSpecLoader.Load(fs, specPath, options)
schema, err := l.fileSpecLoader.Load(specPath, options)
if err != nil {
return fmt.Errorf("could not load schema for %q: %w", path.Dir(specPath), err)
}
Expand Down
12 changes: 6 additions & 6 deletions code/go/internal/specschema/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

func TestLoadFolderSpec(t *testing.T) {
fileSpecLoader := yamlschema.NewFileSchemaLoader()
loader := NewFolderSpecLoader(os.DirFS("./testdata"), fileSpecLoader, semver.Version{})
fileSpecLoader := yamlschema.NewFileSchemaLoader(os.DirFS("./testdata"), semver.Version{})
loader := NewFolderSpecLoader(fileSpecLoader)
spec, err := loader.Load("simple-spec")
require.NoError(t, err)

Expand Down Expand Up @@ -86,10 +86,10 @@ func TestPatchedSpec(t *testing.T) {
},
}

fileSpecLoader := yamlschema.NewFileSchemaLoader()
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
loader := NewFolderSpecLoader(os.DirFS("./testdata"), fileSpecLoader, *c.version)
fileSpecLoader := yamlschema.NewFileSchemaLoader(os.DirFS("./testdata"), *c.version)
loader := NewFolderSpecLoader(fileSpecLoader)
_, err := loader.Load(c.path)
if !c.valid {
require.Error(t, err)
Expand Down Expand Up @@ -139,10 +139,10 @@ func TestPatchedFolderSpec(t *testing.T) {
},
}

fileSpecLoader := yamlschema.NewFileSchemaLoader()
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
loader := NewFolderSpecLoader(os.DirFS("./testdata"), fileSpecLoader, *c.version)
fileSpecLoader := yamlschema.NewFileSchemaLoader(os.DirFS("./testdata"), *c.version)
loader := NewFolderSpecLoader(fileSpecLoader)
_, err := loader.Load(c.path)
if !c.valid {
require.Error(t, err)
Expand Down
7 changes: 5 additions & 2 deletions code/go/internal/spectypes/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ type FileSchema interface {
// FileSchemaLoader loads schemas for files.
type FileSchemaLoader interface {
// Load loads an schema from the given path.
Load(fs fs.FS, specPath string, opts FileSchemaLoadOptions) (FileSchema, error)
Load(specPath string, opts FileSchemaLoadOptions) (FileSchema, error)
// FS returns the spec filesystem this loader uses.
FS() fs.FS
// Version returns the spec version this loader was initialised with.
Version() semver.Version
}

// FileSchemaLoadOptions provides additional information for package loading.
type FileSchemaLoadOptions struct {
ContentType *ContentType
Limits LimitsSpec
SpecVersion semver.Version
}
25 changes: 11 additions & 14 deletions code/go/internal/validator/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,43 +114,40 @@ func processErrors(errs specerrors.ValidationErrors) specerrors.ValidationErrors
new string
}{
{
matcher: regexp.MustCompile(`Must not validate the schema \(not\)`),
matcher: regexp.MustCompile(`'not' failed`),
new: "Must not be present",
},
{
matcher: regexp.MustCompile("secret is required"),
matcher: regexp.MustCompile("missing property 'secret'"),
new: "variable identified as possible secret, secret parameter required to be set to true or false",
},
{
matcher: regexp.MustCompile(`(field processors.[0-9]+.rename): if is required`),
matcher: regexp.MustCompile(`(field processors\.[0-9]+\.rename): missing property 'if'`),
new: "%s: rename \"message\" to \"event.original\" processor requires if: 'ctx.event?.original == null'",
},
{
matcher: regexp.MustCompile(`(field processors.[0-9]+): remove is required`),
matcher: regexp.MustCompile(`(field processors\.[0-9]+): missing property 'remove'`),
new: "%s: rename \"message\" to \"event.original\" processor requires remove \"message\" processor",
},
{
matcher: regexp.MustCompile(`(processors.[0-9]+.remove.field(.[0-9]+)?): processors.[0-9]+.remove.field(.[0-9]+)? does not match: "message"`),
matcher: regexp.MustCompile(`(field processors\.[0-9]+\.remove\.field(?:\.[0-9]+)?): value must be 'message'`),
new: "%s: rename \"message\" to \"event.original\" processor requires remove \"message\" processor",
},
{
matcher: regexp.MustCompile(`(processors.[0-9]+.remove.if): processors.[0-9]+.remove.if does not match: "ctx\.event\?\.original != null"`),
matcher: regexp.MustCompile(`(field processors\.[0-9]+\.remove\.if): value must be 'ctx\.event\?\.original != null'`),
new: "%s: rename \"message\" to \"event.original\" processor requires remove \"message\" processor with if: 'ctx.event?.original != null'",
},
{
matcher: regexp.MustCompile(`(field processors.[0-9]+.remove): (ignore_missing|if) is required`),
matcher: regexp.MustCompile(`(field processors\.[0-9]+\.remove): missing propert(?:y|ies) '(?:ignore_missing|if)'`),
new: "%s: rename \"message\" to \"event.original\" processor requires remove \"message\" processor with if: 'ctx.event?.original != null'",
},
}

// Filter out redundant errors
// Filter out redundant intermediate errors that are superseded by leaf errors.
redundant := []string{
"Must validate \"then\" as \"if\" was valid",
"Must validate \"else\" as \"if\" was not valid",
"Must validate all the schemas (allOf)",
"Must validate at least one schema (anyOf)",
"Must validate one and only one schema (oneOf)",
"At least one of the items must match",
"'allOf' failed",
"'anyOf' failed",
"'oneOf' failed, none matched",
}

// Add error code to specific errors
Expand Down
88 changes: 0 additions & 88 deletions code/go/internal/yamlschema/formatcheckers.go

This file was deleted.

Loading