From 2f59e08401626e35580eaae3d57b0a2465ff5788 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 16 Nov 2023 15:36:47 +0100 Subject: [PATCH] validate watch.path is set to a valid path Signed-off-by: Nicolas De Loof --- loader/loader.go | 7 ++++--- loader/loader_test.go | 1 + paths/resolve.go | 5 ++++- schema/compose-spec.json | 1 + validation/validation.go | 20 +++++++++++++++++--- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/loader/loader.go b/loader/loader.go index e15d4377..82bfd03b 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -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 { diff --git a/loader/loader_test.go b/loader/loader_test.go index d50b3380..d5c0393c 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -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") diff --git a/paths/resolve.go b/paths/resolve.go index c23a717d..62a250a7 100644 --- a/paths/resolve.go +++ b/paths/resolve.go @@ -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) } diff --git a/schema/compose-spec.json b/schema/compose-spec.json index 54e3b896..81dacbcf 100644 --- a/schema/compose-spec.json +++ b/schema/compose-spec.json @@ -461,6 +461,7 @@ "target": {"type": "string"} } }, + "required": ["path", "action"], "additionalProperties": false, "patternProperties": {"^x-": {}} } diff --git a/validation/validation.go b/validation/validation.go index 25b37d9e..75838301 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -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 { @@ -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 +}