Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c2b6ed7
Update .gitignore to include .scratch directory
teresaromero May 27, 2026
1166296
Add internal Mode scaffolding for validation modes
teresaromero May 27, 2026
892c661
Add public constructor API with mode-aware validators (task 02)
teresaromero May 27, 2026
a5c8cfc
Add eager path/fs validation to NewFromPath and NewFromFS constructors
teresaromero May 28, 2026
30cfd76
Fix API: NewFromZip drops mode param, NewFromFS ModeSource uses linke…
teresaromero Jun 1, 2026
d482f31
Fix lint: add godoc comments to exported modes package symbols
teresaromero Jun 1, 2026
2769edc
Add mode.Valid() guard in NewSpec and improve Validate() closer error…
teresaromero Jun 1, 2026
324c4a0
Fix TestLegacyPreservation_FromZip to match NewFromZip signature change
teresaromero Jun 1, 2026
d999602
Add support for mode-aware constructors and validation APIs in changelog
teresaromero Jun 2, 2026
fe83a0a
Address PR review: fix API semantics, tests, and remove private newFr…
teresaromero Jun 2, 2026
491658c
Fix TestNewFromZip_ConstructorSucceeds file handle leak on Windows
teresaromero Jun 2, 2026
15750f5
Update .gitignore to remove .scratch directory entry
teresaromero Jun 3, 2026
fcaf8c2
Refactor Validator constructors to remove Option parameter
teresaromero Jun 3, 2026
f4e266e
refactor POV on modes validation
teresaromero Jun 3, 2026
e9d0013
remove unused public mode
teresaromero Jun 3, 2026
cbd3b7c
Improve documentation for validation API and modes
teresaromero Jun 3, 2026
0f275c0
Add unit tests for mode validation
teresaromero Jun 3, 2026
d1efd03
Add integration tests for link file behavior across validation modes
teresaromero Jun 3, 2026
4233331
Remove unused test case for package validation without links in TestL…
teresaromero Jun 3, 2026
da2a325
Remove wrapping around specFn
teresaromero Jun 4, 2026
8539209
Restore Option C Validator API with mode-embedded FS and fix link blo…
teresaromero Jun 4, 2026
b0049ff
Improve godoc comments for validation modes API
teresaromero Jun 4, 2026
d4508b1
Add copyright notice to modes.go file
teresaromero Jun 4, 2026
8d441f1
Move modes into internal validator pkg
teresaromero Jun 5, 2026
15cc91e
Refactor validator API to streamline mode handling
teresaromero Jun 5, 2026
cc5e273
Remove modes.go file and consolidate mode definitions in validator.go
teresaromero Jun 5, 2026
2a3fef8
Add comprehensive tests for ValidateFromFS and ValidateFromZip methods
teresaromero Jun 5, 2026
d8a2a6c
Refactor validation mode constants for clarity and consistency
teresaromero Jun 5, 2026
ebac0c7
Refactor validator instantiation to unify creation method
teresaromero Jun 5, 2026
d895e49
Add logging for validation mode in technical preview
teresaromero Jun 5, 2026
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
25 changes: 25 additions & 0 deletions code/go/internal/validator/modes/modes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package modes

// Mode represents the validation mode used when validating a package.
type Mode string

// Validation modes for package validation: Legacy preserves existing behavior,
// Source validates checked-out source trees, and Build validates built artifacts.
const (
Legacy Mode = "legacy"
Source Mode = "source"
Build Mode = "build"
)

// Valid reports whether m is a recognised validation mode.
func (m Mode) Valid() bool {
switch m {
case Legacy, Source, Build:
return true
}
return false
}
45 changes: 45 additions & 0 deletions code/go/internal/validator/modes/modes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package modes

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValid(t *testing.T) {
tests := map[string]struct {
mode Mode
valid bool
}{
"valid": {
mode: Legacy,
valid: true,
},
"invalid": {
mode: Mode("invalid"),
valid: false,
},
"source": {
mode: Source,
valid: true,
},
"build": {
mode: Build,
valid: true,
},
"": {
mode: Mode(""),
valid: false,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.valid, test.mode.Valid(), "mode %s should be %v", test.mode, test.valid)
})
}
}
31 changes: 29 additions & 2 deletions code/go/internal/validator/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/elastic/package-spec/v3/code/go/internal/loader"
"github.com/elastic/package-spec/v3/code/go/internal/packages"
"github.com/elastic/package-spec/v3/code/go/internal/spectypes"
"github.com/elastic/package-spec/v3/code/go/internal/validator/modes"
"github.com/elastic/package-spec/v3/code/go/internal/validator/semantic"
"github.com/elastic/package-spec/v3/code/go/pkg/specerrors"
)
Expand All @@ -32,6 +33,8 @@ type Spec struct {
specVersion semver.Version
// fs contains the filesystem of the spec.
fs fs.FS
// mode is the validation mode (legacy, source, build).
mode modes.Mode

// WarningsAsErrors causes validation warnings to be reported as errors when true.
WarningsAsErrors bool
Expand All @@ -44,12 +47,15 @@ type validationRules []validationRule
// GASpecCheckVersion represents minimum version to start checking for unreleased version of the spec
var GASpecCheckVersion = semver.MustParse("3.0.1")

// NewSpec creates a new Spec for the given version
func NewSpec(version semver.Version) (*Spec, error) {
// newSpec creates a new Spec for the given version and validation mode.
func newSpec(version semver.Version, mode modes.Mode) (*Spec, error) {
specVersion, err := spec.CheckVersion(version)
if err != nil {
return nil, fmt.Errorf("could not load specification for version [%s]: %w", version.String(), err)
}
if !mode.Valid() {
return nil, fmt.Errorf("invalid validation mode %q", mode)
}

// With more current versions this is reported as a filterable validation error for GA packages.
if version.LessThan(GASpecCheckVersion) {
Expand All @@ -62,11 +68,27 @@ func NewSpec(version semver.Version) (*Spec, error) {
version: version,
specVersion: *specVersion,
fs: spec.FS(),
mode: mode,
}

return &s, nil
}

// NewLegacySpec creates a new Spec for the given version using the legacy specification.
func NewLegacySpec(version semver.Version) (*Spec, error) {
return newSpec(version, modes.Legacy)
}

// NewBuildSpec creates a new Spec for the given version using the build specification.
func NewBuildSpec(version semver.Version) (*Spec, error) {
return newSpec(version, modes.Build)
}

// NewSourceSpec creates a new Spec for the given version using the source specification.
func NewSourceSpec(version semver.Version) (*Spec, error) {
return newSpec(version, modes.Source)
}

// ValidatePackage validates the given Package against the Spec
func (s Spec) ValidatePackage(pkg packages.Package) specerrors.ValidationErrors {
var errs specerrors.ValidationErrors
Expand Down Expand Up @@ -199,6 +221,7 @@ func (s Spec) rules(pkgType string, rootSpec spectypes.ItemSpec) validationRules
since *semver.Version
until *semver.Version
types []string
modes []modes.Mode
}{
{fn: semantic.ValidateVersionIntegrity},
{fn: semantic.ValidateChangelogLinks},
Expand Down Expand Up @@ -260,6 +283,10 @@ func (s Spec) rules(pkgType string, rootSpec spectypes.ItemSpec) validationRules
continue
}

if rule.modes != nil && !slices.Contains(rule.modes, s.mode) {
continue
}

validationRules = append(validationRules, rule.fn)
}

Expand Down
8 changes: 6 additions & 2 deletions code/go/internal/validator/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

"github.com/elastic/package-spec/v3/code/go/internal/fspath"
"github.com/elastic/package-spec/v3/code/go/internal/packages"
"github.com/elastic/package-spec/v3/code/go/internal/validator/modes"
)

func TestNewSpec(t *testing.T) {
func TestNewLegacySpec(t *testing.T) {
tests := map[string]struct {
expectedErrContains string
}{
Expand All @@ -26,7 +27,7 @@ func TestNewSpec(t *testing.T) {
}

for version, test := range tests {
spec, err := NewSpec(*semver.MustParse(version))
spec, err := NewLegacySpec(*semver.MustParse(version))
if test.expectedErrContains == "" {
require.NoError(t, err)
require.IsType(t, &Spec{}, spec)
Expand All @@ -44,6 +45,7 @@ func TestNoBetaFeatures_Package_GA(t *testing.T) {
version: *semver.MustParse("1.0.0"),
specVersion: *semver.MustParse("1.0.0"),
fs: fspath.DirFS("testdata/fakespec"),
mode: modes.Legacy,
}
pkg, err := packages.NewPackage("testdata/packages/features_ga")
require.NoError(t, err)
Expand All @@ -58,6 +60,7 @@ func TestBetaFeatures_Package_GA(t *testing.T) {
version: *semver.MustParse("1.0.0"),
specVersion: *semver.MustParse("1.0.0"),
fs: fspath.DirFS("testdata/fakespec"),
mode: modes.Legacy,
}
pkg, err := packages.NewPackage("testdata/packages/features_beta")
require.NoError(t, err)
Expand Down Expand Up @@ -134,6 +137,7 @@ func TestFolderSpecInvalid(t *testing.T) {
version: c.version,
specVersion: c.version,
fs: c.spec,
mode: modes.Legacy,
}
pkg, err := packages.NewPackage(c.pkgPath)
require.NoError(t, err)
Expand Down
8 changes: 7 additions & 1 deletion code/go/pkg/validator/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
"testing"
"time"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"

"github.com/elastic/package-spec/v3/code/go/internal/spectypes"
"github.com/elastic/package-spec/v3/code/go/internal/validator"
)

func TestLimitsValidation(t *testing.T) {
Expand Down Expand Up @@ -116,7 +118,11 @@ func TestLimitsValidation(t *testing.T) {
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
t.Parallel()
err := ValidateFromFS("test-package", c.fsys)

specFn := func(version semver.Version) (*validator.Spec, error) {
return validator.NewLegacySpec(version)
}
err := validateFromFS("test-package", c.fsys, specFn)
Comment thread
teresaromero marked this conversation as resolved.
Outdated
if c.valid {
assert.NoError(t, err)
} else {
Expand Down
66 changes: 53 additions & 13 deletions code/go/pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,61 @@ import (
"io/fs"
"os"

"github.com/Masterminds/semver/v3"

"github.com/elastic/package-spec/v3/code/go/internal/linkedfiles"
"github.com/elastic/package-spec/v3/code/go/internal/packages"
"github.com/elastic/package-spec/v3/code/go/internal/validator"
"github.com/elastic/package-spec/v3/code/go/internal/validator/common"
)

// ValidateFromPath validates a package located at the given path against the
// appropriate specification and returns any errors.
type specFn func(semver.Version) (*validator.Spec, error)

// ValidateFromPath validates a package located at the given path using the legacy specification.
// This function preserves byte-for-byte identical behavior with existing validation workflows.
// Linked (.link) files are resolved transparently.
// Deprecated: Use ValidateFromSourcePath or ValidateFromBuildPath depending on the package type.
func ValidateFromPath(packageRootPath string) error {
// We wrap the fs.FS with a linkedfiles.LinksFS to handle linked files.
linksFS := linkedfiles.NewFS(packageRootPath, os.DirFS(packageRootPath))
return ValidateFromFS(packageRootPath, linksFS)

legacySpec := func(version semver.Version) (*validator.Spec, error) {
return validator.NewLegacySpec(version)
}

return validateFromFS(packageRootPath, linksFS, legacySpec)
Comment thread
teresaromero marked this conversation as resolved.
Outdated
}

// ValidateFromBuildPath validates a built package located at the given path.
// This function uses the build specification, appropriate for packages produced by
// elastic-package build, distributed as zip files, or served by the package registry.
// Linked files (.link) are blocked; source-only artifacts are rejected.
func ValidateFromBuildPath(packageRootPath string) error {
fs := os.DirFS(packageRootPath)

buildSpec := func(version semver.Version) (*validator.Spec, error) {
return validator.NewBuildSpec(version)
}
return validateFromFS(packageRootPath, fs, buildSpec)
Comment thread
teresaromero marked this conversation as resolved.
Outdated
}

// ValidateFromSourcePath validates a package source tree located at the given path.
// This function uses the source specification for checked-out source trees.
// Linked (.link) files are resolved transparently.
func ValidateFromSourcePath(packageRootPath string) error {
// We wrap the fs.FS with a linkedfiles.LinksFS to handle linked files.
linksFS := linkedfiles.NewFS(packageRootPath, os.DirFS(packageRootPath))

sourceSpec := func(version semver.Version) (*validator.Spec, error) {
return validator.NewSourceSpec(version)
}

return validateFromFS(packageRootPath, linksFS, sourceSpec)
Comment thread
teresaromero marked this conversation as resolved.
Outdated
}

// ValidateFromZip validates a package on its zip format.
// ValidateFromZip validates a package in zip file format.
// This function uses the build specification since zip files are by definition built packages.
// Linked files (.link) are blocked; source-only artifacts are rejected.
func ValidateFromZip(packagePath string) error {
r, err := zip.OpenReader(packagePath)
if err != nil {
Expand All @@ -46,16 +86,16 @@ func ValidateFromZip(packagePath string) error {
return err
}

return ValidateFromFS(packagePath, subDir)
}
buildSpec := func(version semver.Version) (*validator.Spec, error) {
return validator.NewBuildSpec(version)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't like the handling of linked file systems, but this is internal and we can revisit later, I don't have much better ideas now.


// ValidateFromFS validates a package against the appropiate specification and returns any errors.
// Package files are obtained through the given filesystem.
func ValidateFromFS(location string, fsys fs.FS) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is removed, right? This would be a breaking change in the public API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidateFromFS although is a public method is just being used at tests files. I've reviewed elastic-package and dont see this function used explicitly so, its a breaking change because is no longer available but is not being used 🤔 I will revert and keep it, although i think we should deprecate it in favour of NewFromFS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should deprecate all ValidateFrom methods at the package level, and keep in the long term only the Validator object.
In the meantime let's keep all of them, just to follow good practices, just in case.

return validateFromFS(location, fsys, common.IsDefinedWarningsAsErrors())
return validateFromFS(packagePath, subDir, buildSpec)
Comment thread
teresaromero marked this conversation as resolved.
Outdated
}

func validateFromFS(location string, fsys fs.FS, warningsAsErrors bool) error {
// validateFromFS validates a package against the appropriate specification and returns any errors.
// Package files are obtained through the given filesystem.
func validateFromFS(location string, fsys fs.FS, specFn specFn) error {
// If we are not explicitly using the linkedfiles.FS, we wrap fsys with
// a linkedfiles.BlockFS to block the use of linked files.
if _, ok := fsys.(*linkedfiles.FS); !ok {
Expand All @@ -70,11 +110,11 @@ func validateFromFS(location string, fsys fs.FS, warningsAsErrors bool) error {
return errors.New("could not determine specification version for package")
}

spec, err := validator.NewSpec(*pkg.SpecVersion)
spec, err := specFn(*pkg.SpecVersion)
if err != nil {
return err
}
spec.WarningsAsErrors = warningsAsErrors
spec.WarningsAsErrors = common.IsDefinedWarningsAsErrors()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should append a technical preview warning when Source or Build modes are used, at least till we complete the validators.

@teresaromero teresaromero Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added just using the log pkg as i havent seen any logger implemented on the package d895e49

i followed the same pattern as in

log.Printf("Warning: %s", err.Error())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we don't have an abstraction for warnings, maybe we should add it.

We could leverage warningsAsErrors, something like this:

errs := spec.ValidatePackage(*pkg)
if v.mode != LegacyMode {
    err := specerrors.NewStructuredErrorf("validation mode '%s' is in technical preview", v.mode)
    if v.warningsAsErrors() {
        errs = append(errs, err)
    } else {
        log.Printf("Warning: %s", err.Message())
    }
}
if len(errs) > 0 {
    return errs
}
return nil

if errs := spec.ValidatePackage(*pkg); len(errs) > 0 {
return errs
Expand Down
Loading