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
12 changes: 12 additions & 0 deletions validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package validation

import (
"fmt"
"net"
"strings"

"github.com/compose-spec/compose-go/v2/tree"
Expand All @@ -29,6 +30,7 @@ var checks = map[tree.Path]checkerFunc{
"volumes.*": checkVolume,
"configs.*": checkFileObject("file", "environment", "content"),
"secrets.*": checkFileObject("file", "environment"),
"services.*.ports.*": checkIPAddress,
"services.*.develop.watch.*.path": checkPath,
"services.*.deploy.resources.reservations.devices.*": checkDeviceRequest,
"services.*.gpus.*": checkDeviceRequest,
Expand Down Expand Up @@ -105,3 +107,13 @@ func checkDeviceRequest(value any, p tree.Path) error {
}
return nil
}

func checkIPAddress(value any, p tree.Path) error {
if v, ok := value.(map[string]any); ok {
ip, ok := v["host_ip"]
if ok && net.ParseIP(ip.(string)) == nil {
return fmt.Errorf("%s: invalid ip address: %s", p, ip)
}
}
return nil
}
46 changes: 46 additions & 0 deletions validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,49 @@ external: true
})
}
}

func TestIPAddress(t *testing.T) {
checker := checks["services.*.ports.*"]
tests := []struct {
name string
input string
err string
}{
{
name: "port long syntax, invalid IP",
input: `
host_ip: notavalidip
target: 1234
published: "1234"
`,
err: "configs.test.ports[0]: invalid ip address: notavalidip",
},
{
name: "port long syntax, no IP",
input: `
target: 1234
published: "1234"
`,
},
{
name: "port long syntax, valid IP",
input: `
host_ip: 192.168.3.4
target: 1234
published: "1234"
`,
},
}

for _, tt := range tests {
var input map[string]any
err := yaml.Unmarshal([]byte(tt.input), &input)
assert.NilError(t, err)
err = checker(input, tree.NewPath("configs.test.ports[0]"))
if tt.err == "" {
assert.NilError(t, err)
} else {
assert.Equal(t, tt.err, err.Error())
}
}
}