Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ fuzz
/build/
.vscode/
.DS_Store

.scratch/
Comment thread
teresaromero marked this conversation as resolved.
Outdated
24 changes: 24 additions & 0 deletions code/go/internal/validator/modes/modes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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. The public API re-exports these as validator.Mode* constants.
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
}
21 changes: 19 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,8 +47,16 @@ 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.
// If mode is empty, it defaults to modes.Legacy.
func NewSpec(version semver.Version, mode modes.Mode) (*Spec, error) {
Comment thread
jsoriano marked this conversation as resolved.
Outdated
if mode == "" {
mode = modes.Legacy
}
if !mode.Valid() {
return nil, fmt.Errorf("invalid validation mode %q", mode)
}

specVersion, err := spec.CheckVersion(version)
if err != nil {
return nil, fmt.Errorf("could not load specification for version [%s]: %w", version.String(), err)
Expand All @@ -62,6 +73,7 @@ func NewSpec(version semver.Version) (*Spec, error) {
version: version,
specVersion: *specVersion,
fs: spec.FS(),
mode: mode,
}

return &s, nil
Expand Down Expand Up @@ -199,6 +211,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 +273,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
3 changes: 2 additions & 1 deletion code/go/internal/validator/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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) {
Expand All @@ -26,7 +27,7 @@ func TestNewSpec(t *testing.T) {
}

for version, test := range tests {
spec, err := NewSpec(*semver.MustParse(version))
spec, err := NewSpec(*semver.MustParse(version), modes.Legacy)
if test.expectedErrContains == "" {
require.NoError(t, err)
require.IsType(t, &Spec{}, spec)
Expand Down
203 changes: 203 additions & 0 deletions code/go/pkg/validator/api.go

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.

Nit. "api" is redundant here, any public method is going to be part of the package API 🙂

This could belong to validator.go, as it actually defines the Validator object.

Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// 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 validator

import (
"archive/zip"
"errors"
"fmt"
"io"
"io/fs"
"os"

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

// Validator holds the configuration for a package validation run.
// Create one with NewFromPath, NewFromZip, or NewFromFS, then call Validate.
type Validator struct {
mode Mode
location string
fsys fs.FS
warningsAsErrors bool
// closer is non-nil when the Validator owns a resource (e.g. a zip reader)
// that must be released after Validate returns.
closer io.Closer
}

// Option configures a Validator.
type Option func(*Validator)

// WithWarningsAsErrors makes validation warnings count as errors when enabled is true.
func WithWarningsAsErrors(enabled bool) Option {
return func(v *Validator) { v.warningsAsErrors = enabled }
}

// NewFromPath returns a Validator for the package rooted at packageRootPath.
//
// For ModeLegacy and ModeSource the filesystem honours linked (.link) files;
// for ModeBuild linked files are blocked (matching a built package artifact).
func NewFromPath(mode Mode, packageRootPath string, opts ...Option) (*Validator, 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.

Nit. Configuration structs are usually easier to discover than functional options, and use to be simpler.

Suggested change
func NewFromPath(mode Mode, packageRootPath string, opts ...Option) (*Validator, error) {
type Config struct {
WarningsAsErrors bool
}
func NewFromPath(mode Mode, packageRootPath string, config Config) (*Validator, error) {

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.

This config is used for the warnings, which is read from the env, so i removed the options/config and read it directly from the env. Changed the test case too to use t.Env instead of a local variable.

v := &Validator{
		mode:             mode,
		location:         location,
		fsys:             fsys,
		warningsAsErrors: common.IsDefinedWarningsAsErrors(),
		closer:           closer,
}

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.

Part of the original design in #549 was actually to introduce the "Option C" API shape, a new "Validator" object that can be somehow customized with different options.

With current code we are back to something like "Option A": methods with the mode as parameter. Only that we have one method per mode instead of having the mode as a parameter.

I still think it would be valuable to introduce this "Validator" object. Having the object we can add configuration options later without needing to add more functions to the package, or parameters to these functions.

For this initial version it could have warningsAsErros as single configuration, and it could have two validation methods (build and source) if we prefer this approach instead of modes as a parameter.

if !mode.Valid() {
return nil, fmt.Errorf("invalid validation mode %q", mode)
}

info, err := os.Stat(packageRootPath)
if err != nil {
return nil, fmt.Errorf("invalid package path %q: %w", packageRootPath, err)
}
if !info.IsDir() {
return nil, fmt.Errorf("invalid package path %q: not a directory", packageRootPath)
}

var fsys fs.FS
if mode == ModeBuild {
fsys = linkedfiles.NewBlockFS(os.DirFS(packageRootPath))
} else {
// ModeLegacy and ModeSource: resolve linked files transparently.
fsys = linkedfiles.NewFS(packageRootPath, os.DirFS(packageRootPath))
}
return buildValidator(mode, packageRootPath, fsys, nil, opts), nil
}

// NewFromZip returns a Validator for the package stored in the zip file at zipPath.
//
// Zip files always contain built packages — they are the output format produced
// by elastic-package build and consumed by the package registry and Fleet.
// Validation always runs in ModeBuild; source-only artifacts (_dev/, .link files,
// external: ecs references) are therefore rejected.
//
// NOTE: ModeBuild-specific validation rules are not yet implemented; ModeBuild
// and ModeLegacy currently produce identical rule sets. This is intentional —
// the mode is set here so that future PRs can attach build-only rules without
// changing the public API.
//
// The returned Validator owns the underlying zip reader; calling Validate closes it.
// Do not call Validate more than once on a Validator created by NewFromZip.
func NewFromZip(zipPath string, opts ...Option) (_ *Validator, err error) {
r, openErr := zip.OpenReader(zipPath)
if openErr != nil {
return nil, fmt.Errorf("failed to open zip file (%s): %w", zipPath, openErr)
}
// Close the reader on any error path; on success the Validator takes ownership.
defer func() {
if err != nil {
if cerr := r.Close(); cerr != nil {
err = errors.Join(err, cerr)
}
}
}()

dirs, err := fs.ReadDir(r, ".")
if err != nil {
return nil, fmt.Errorf("failed to read root directory in zip file (%s): %w", zipPath, err)
}
if len(dirs) != 1 {
return nil, fmt.Errorf("a single directory is expected in zip file, %d found", len(dirs))
}

subDir, err := fs.Sub(r, dirs[0].Name())
if err != nil {
return nil, err
}

// Zip archives contain built packages; linked files are always blocked.
fsys := linkedfiles.NewBlockFS(subDir)
return buildValidator(ModeBuild, zipPath, fsys, r, opts), nil
}

// NewFromFS returns a Validator for the package accessible through fsys at location.
//
// Linked-file handling depends on mode:
// - ModeSource: if fsys is not already a *linkedfiles.FS it is wrapped with
// linkedfiles.NewFS so .link files are resolved transparently.
// - ModeLegacy: a pre-wrapped *linkedfiles.FS is preserved as-is (links
// resolved); any other filesystem is wrapped with BlockFS. This matches the
// behaviour of the deprecated ValidateFromFS.
// - ModeBuild: BlockFS is always applied, even if fsys is already a
// *linkedfiles.FS, so linked files are unconditionally rejected.
func NewFromFS(mode Mode, location string, fsys fs.FS, opts ...Option) (*Validator, error) {
Comment thread
jsoriano marked this conversation as resolved.
Outdated
if !mode.Valid() {
return nil, fmt.Errorf("invalid validation mode %q", mode)
}

info, err := fs.Stat(fsys, ".")
if err != nil {
return nil, fmt.Errorf("invalid package filesystem at %q: %w", location, err)
}
if !info.IsDir() {
return nil, fmt.Errorf("invalid package filesystem at %q: root is not a directory", location)
}

_, isLinkedFS := fsys.(*linkedfiles.FS)
switch mode {
case ModeSource:
if !isLinkedFS {
fsys = linkedfiles.NewFS(location, fsys)

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.

On what cases we receive a linked FS here?

Nit. Maybe NewFS and NewBlockFS should decide what to do when called with a linkedfiles FS of each kind, so their callers don't need to care.

}
case ModeBuild:
// Always block, even a pre-wrapped *linkedfiles.FS.
fsys = linkedfiles.NewBlockFS(fsys)

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.

Umm, not sure if we should do this at this level. Could we instead reject .lnk files in built packages as we will reject _dev files?

Or we need to do this because the spec always receives the link files resolved?

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 am not sure i understand this, or the lines have moved around. here i am validating the fsys, not any linked files. The fs is checked dependeing on the mode "selected" if it allows or not .link files. For build validation there link should be resolved.

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.

When validating a built package there should not be any .link files.

I was wondering how we are ensuring that, and if the different FSs used was because of that.

default: // ModeLegacy
// Preserve a pre-wrapped *linkedfiles.FS; block everything else.
// Matches the old validateFromFS behaviour.

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.

Not sure if this would be needed now. I think this was used before because NewFromPath generated its own linkedfiles.FS. But not sure if this is the case now.

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 need to review this whole refactor, as i am thinking it will be better to have direct constructors for modes, instead of using the mode as a switch...

if !isLinkedFS {
fsys = linkedfiles.NewBlockFS(fsys)
}
}
return buildValidator(mode, location, fsys, nil, opts), nil
}

// buildValidator is the shared internal constructor.
func buildValidator(mode Mode, location string, fsys fs.FS, closer io.Closer, opts []Option) *Validator {
v := &Validator{
mode: mode,
location: location,
fsys: fsys,
warningsAsErrors: common.IsDefinedWarningsAsErrors(),
closer: closer,
}
for _, opt := range opts {
opt(v)
}
return v
}

// Validate runs package validation and returns any errors encountered.
//
// If the Validator was created by NewFromZip it owns an open zip reader;
// Validate closes it on return, so Validate must not be called more than once
// on such a Validator.
func (v *Validator) Validate() (err error) {
if v.closer != nil {
defer func() {
if cerr := v.closer.Close(); cerr != nil {
err = errors.Join(err, cerr)
}
}()
}

pkg, err := packages.NewPackageFromFS(v.location, v.fsys)
if err != nil {
return err
}
if pkg.SpecVersion == nil {
return errors.New("could not determine specification version for package")
}

spec, err := internalvalidator.NewSpec(*pkg.SpecVersion, v.mode)
if err != nil {
return err
}
spec.WarningsAsErrors = v.warningsAsErrors

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