From 2a05894ecc6c148211e45b511e499ceaa7f3535a Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Mon, 15 Jul 2024 13:04:12 -0700 Subject: [PATCH] Required vs optional --- internal/owl/validate.go | 18 ++++++++++++++---- internal/owl/validate_test.go | 17 +++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/owl/validate.go b/internal/owl/validate.go index 8d96c384..fc46b42a 100644 --- a/internal/owl/validate.go +++ b/internal/owl/validate.go @@ -118,17 +118,17 @@ var ComplexDefTypes = map[string]*ComplexDef{ Items: map[string]*varSpec{ "HOST": { Name: SpecNamePlain, - Rules: "required,ip|hostname", + Rules: "ip|hostname", Required: true, }, "PORT": { Name: SpecNamePlain, - Rules: "required,number", + Rules: "number", Required: true, }, "PASSWORD": { Name: SpecNamePassword, - Rules: "required,min=18,max=32", + Rules: "min=18,max=32", Required: false, }, }, @@ -175,10 +175,20 @@ func (s *ComplexOperationSet) validate() (ValidationErrors, error) { } itemKey := (parts[len(parts)-1]) + item, ok := typ.Items[itemKey] + if !ok { + return nil, fmt.Errorf("complex item not found: %s", itemKey) + } + data := make(map[string]interface{}, 1) rules := make(map[string]interface{}, 1) + + if val.Value.Resolved == "" && !item.Required { + continue + } + data[val.Var.Key] = val.Value.Resolved - rules[val.Var.Key] = typ.Items[itemKey].Rules + rules[val.Var.Key] = item.Rules field := validator.ValidateMap(data, rules) diff --git a/internal/owl/validate_test.go b/internal/owl/validate_test.go index 24cec119..3dbae925 100644 --- a/internal/owl/validate_test.go +++ b/internal/owl/validate_test.go @@ -64,11 +64,12 @@ func Test_Store_ComplexSpecs(t *testing.T) { assert.EqualValues(t, "GCLOUD_2_REDIS_HOST", snapshot[0].Var.Key) assert.EqualValues(t, "127.0.0.5", snapshot[0].Value.Resolved) }) +} - t.Run("Validation errors for invalid env values", func(t *testing.T) { +func Test_Store_ComplexValidation(t *testing.T) { + t.Run("Invalid env values", func(t *testing.T) { fake := []byte(`GOPATH=/Users/sourishkrout/go INSTRUMENTATION_KEY=05a2cc58-5101-4c69-a0d0-7a126253a972 # Secret! - PGPASS=too-short # Password! HOMEBREW_REPOSITORY=/opt/homebrew # Plain REDIS_HOST=12345 # Redis! REDIS_PORT=invalid-port # Redis!`) @@ -80,18 +81,18 @@ func Test_Store_ComplexSpecs(t *testing.T) { require.NoError(t, err) snapshot.sortbyKey() - assert.EqualValues(t, "REDIS_HOST", snapshot[4].Var.Key) - assert.EqualValues(t, "12345", snapshot[4].Value.Resolved) + assert.EqualValues(t, "REDIS_HOST", snapshot[3].Var.Key) + assert.EqualValues(t, "12345", snapshot[3].Value.Resolved) assert.EqualValues(t, `Error 1: The value of variable "REDIS_HOST" failed tag validation "ip|hostname" required by "Redis->HOST" declared in ".env.example"`, - snapshot[4].Errors[0].Message, + snapshot[3].Errors[0].Message, ) - assert.EqualValues(t, "REDIS_PORT", snapshot[5].Var.Key) - assert.EqualValues(t, "invalid-port", snapshot[5].Value.Resolved) + assert.EqualValues(t, "REDIS_PORT", snapshot[4].Var.Key) + assert.EqualValues(t, "invalid-port", snapshot[4].Value.Resolved) assert.EqualValues(t, `Error 1: The value of variable "REDIS_PORT" failed tag validation "number" required by "Redis->PORT" declared in ".env.example"`, - snapshot[5].Errors[0].Message, + snapshot[4].Errors[0].Message, ) }) }