cli/compose/schema: Validate(): normalize version before validating

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-16 13:41:21 +02:00
parent cf8c4bab64
commit d44eca129f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 7 additions and 0 deletions

View File

@ -50,6 +50,8 @@ func Version(config map[string]interface{}) string {
func normalizeVersion(version string) string { func normalizeVersion(version string) string {
switch version { switch version {
case "":
return defaultVersion
case "3": case "3":
return "3.0" return "3.0"
default: default:
@ -62,6 +64,7 @@ var schemas embed.FS
// Validate uses the jsonschema to validate the configuration // Validate uses the jsonschema to validate the configuration
func Validate(config map[string]interface{}, version string) error { func Validate(config map[string]interface{}, version string) error {
version = normalizeVersion(version)
schemaData, err := schemas.ReadFile("data/config_schema_v" + version + ".json") schemaData, err := schemas.ReadFile("data/config_schema_v" + version + ".json")
if err != nil { if err != nil {
return errors.Errorf("unsupported Compose file version: %s", version) return errors.Errorf("unsupported Compose file version: %s", version)

View File

@ -20,6 +20,9 @@ func TestValidate(t *testing.T) {
} }
assert.NilError(t, Validate(config, "3.0")) assert.NilError(t, Validate(config, "3.0"))
assert.NilError(t, Validate(config, "3"))
assert.ErrorContains(t, Validate(config, ""), "unsupported Compose file version: 1.0")
assert.ErrorContains(t, Validate(config, "12345"), "unsupported Compose file version: 12345")
} }
func TestValidateUndefinedTopLevelOption(t *testing.T) { func TestValidateUndefinedTopLevelOption(t *testing.T) {
@ -84,6 +87,7 @@ func TestValidateCredentialSpecs(t *testing.T) {
version string version string
expectedErr string expectedErr string
}{ }{
{version: "3", expectedErr: "credential_spec"},
{version: "3.0", expectedErr: "credential_spec"}, {version: "3.0", expectedErr: "credential_spec"},
{version: "3.1", expectedErr: "credential_spec"}, {version: "3.1", expectedErr: "credential_spec"},
{version: "3.2", expectedErr: "credential_spec"}, {version: "3.2", expectedErr: "credential_spec"},