Skip to content

Commit

Permalink
validate watch.path is set to a valid path
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 16, 2023
1 parent e81a720 commit 35f5c58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
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
}

0 comments on commit 35f5c58

Please sign in to comment.