Skip to content

Commit

Permalink
Required vs optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Jul 15, 2024
1 parent 8ca636c commit 2a05894
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
18 changes: 14 additions & 4 deletions internal/owl/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions internal/owl/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!`)
Expand All @@ -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,
)
})
}

0 comments on commit 2a05894

Please sign in to comment.