Skip to content
Merged
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
7 changes: 4 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,10 @@ func loadYamlModel(ctx context.Context, config types.ConfigDetails, opts *Option

dict = groupXFieldsIntoExtensions(dict, tree.NewPath())

// TODO(ndeloof) shall we implement this as a Validate func on types ?
if err := validation.Validate(dict); err != nil {
return nil, err
if !opts.SkipValidation {
if err := validation.Validate(dict); err != nil {
return nil, err
}
}

if opts.ResolvePaths {
Expand Down
1 change: 1 addition & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,7 @@ services:
action: sync+restart
`, nil), func(options *Options) {
options.ResolvePaths = false
options.SkipValidation = true
})
assert.NilError(t, err)
frontend, err := project.GetService("frontend")
Expand Down
5 changes: 4 additions & 1 deletion paths/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ func (r *relativePathsResolver) absPath(value any) (any, error) {
if filepath.IsAbs(v) {
return v, nil
}
return filepath.Join(r.workingDir, v), nil
if v != "" {
return filepath.Join(r.workingDir, v), nil
}
return v, nil
}
return nil, fmt.Errorf("unexpected type %T", value)
}
Expand Down
1 change: 1 addition & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
"target": {"type": "string"}
}
},
"required": ["path", "action"],
"additionalProperties": false,
"patternProperties": {"^x-": {}}
}
Expand Down
20 changes: 17 additions & 3 deletions validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ package validation

import (
"fmt"
"os"
"strings"

"github.com/compose-spec/compose-go/v2/tree"
"github.com/pkg/errors"
)

type checkerFunc func(value any, p tree.Path) error

var checks = map[tree.Path]checkerFunc{
"volumes.*": checkVolume,
"configs.*": checkFileObject("file", "environment", "content"),
"secrets.*": checkFileObject("file", "environment"),
"volumes.*": checkVolume,
"configs.*": checkFileObject("file", "environment", "content"),
"secrets.*": checkFileObject("file", "environment"),
"services.*.develop.watch.*.path": checkPath,
}

func Validate(dict map[string]any) error {
Expand Down Expand Up @@ -85,3 +88,14 @@ func checkFileObject(keys ...string) checkerFunc {
return nil
}
}

func checkPath(value any, p tree.Path) error {
v := value.(string)
if v == "" {
return errors.Errorf("%s: value can't be blank", p)
}
if _, err := os.Stat(v); err != nil {
return errors.Wrapf(err, "%s: invalid path %s", p, value)
}
return nil
}