Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation): add optional enum values with warning #1500

Merged
merged 4 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions internal/core/arg_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type ArgSpec struct {
// EnumValues contains all possible values of an enum.
EnumValues []string

// BadEnumWarning is the warning printed when the value is not in the EnumValues
// If not set, the value has to be in EnumeValues
BadEnumWarning string

// AutoCompleteFunc is used to autocomplete possible values for a given argument.
AutoCompleteFunc AutoCompleteArgFunc

Expand Down
4 changes: 4 additions & 0 deletions internal/core/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func DefaultArgSpecValidateFunc() ArgSpecValidateFunc {
}

if !stringExists(argSpec.EnumValues, strValue) {
if argSpec.BadEnumWarning != "" {
logger.Warningf(argSpec.BadEnumWarning, strValue)
return nil
}
return InvalidValueForEnumError(argSpec.Name, argSpec.EnumValues, strValue)
}
return nil
Expand Down
9 changes: 5 additions & 4 deletions internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func serverCreateCommand() *core.Command {
AutoCompleteFunc: instanceServerCreateImageAutoCompleteFunc,
},
{
Name: "type",
Short: "Server commercial type",
Default: core.DefaultValueSetter("DEV1-S"),
EnumValues: []string{"GP1-XS", "GP1-S", "GP1-M", "GP1-L", "GP1-XL", "DEV1-S", "DEV1-M", "DEV1-L", "DEV1-XL", "RENDER-S"},
Name: "type",
Short: "Server commercial type",
Default: core.DefaultValueSetter("DEV1-S"),
EnumValues: []string{"GP1-XS", "GP1-S", "GP1-M", "GP1-L", "GP1-XL", "DEV1-S", "DEV1-M", "DEV1-L", "DEV1-XL", "RENDER-S"},
BadEnumWarning: "The Commercial Type %s is not supported, it may misbehave\n",
},
{
Name: "name",
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
🎲🎲🎲 EXIT CODE: 1 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Invalid value 'MACBOOK1-S' for arg 'type'

Hint:
Accepted values for 'type' are [GP1-XS GP1-S GP1-M GP1-L GP1-XL DEV1-S DEV1-M DEV1-L DEV1-XL RENDER-S]
Bad image label 'ubuntu_bionic' for MACBOOK1-S
🟥🟥🟥 JSON STDERR 🟥🟥🟥
{
"message": "invalid value 'MACBOOK1-S' for arg 'type'",
"error": {},
"hint": "Accepted values for 'type' are [GP1-XS GP1-S GP1-M GP1-L GP1-XL DEV1-S DEV1-M DEV1-L DEV1-XL RENDER-S]"
"error": "bad image label 'ubuntu_bionic' for MACBOOK1-S"
}