Skip to content

Commit

Permalink
lint: limit some string values via enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan authored and buger committed Nov 2, 2017
1 parent f06e5e4 commit bdae5a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ func resultWarns(result *schema.Result) []string {
}
// We need this since formats can only return bools, not
// custom errors/messages.
var desc string
switch format := ferr.Details()["format"].(string); format {
case "path":
ferr.SetDescription("Path does not exist or is not accessible")
desc = "Path does not exist or is not accessible"
case "host-no-port":
ferr.SetDescription("Address should be a host without port")
desc = "Address should be a host without port"
default:
panic(fmt.Sprintf("unexpected format type: %q", format))
}
ferr.SetDescription(desc)
strs[i] = ferr.String()
}
return strs
Expand Down
12 changes: 12 additions & 0 deletions lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ var tests = []struct {
"BadHost", `{"storage": {"host": "::::"}}`,
"storage.host: Address should be a host without port",
},
{
"BadLogLevel", `{"log_level": "catastrophic"}`,
`log_level: log_level must be one of the following: "", "debug", "info", "warn", "error"`,
},
{
"BadStorageType", `{"storage": {"type": "cd-rom"}}`,
`storage.type: storage.type must be one of the following: "", "redis"`,
},
{
"BadPolicySource", `{"policies": {"policy_source": "internet"}}`,
`policies.policy_source: policies.policy_source must be one of the following: "", "service", "rpc"`,
},
}

func allContains(got, want []string) bool {
Expand Down
9 changes: 6 additions & 3 deletions lint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const confSchema = `{
"type": "integer"
},
"type": {
"type": "string"
"type": "string",
"enum": ["", "redis"]
},
"username": {
"type": "string"
Expand Down Expand Up @@ -367,7 +368,8 @@ const confSchema = `{
}
},
"log_level": {
"type": "string"
"type": "string",
"enum": ["", "debug", "info", "warn", "error"]
},
"logstash_network_addr": {
"type": "string"
Expand Down Expand Up @@ -457,7 +459,8 @@ const confSchema = `{
"type": "string"
},
"policy_source": {
"type": "string"
"type": "string",
"enum": ["", "service", "rpc"]
}
}
},
Expand Down

0 comments on commit bdae5a4

Please sign in to comment.