-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Labels
Description
The following is being used by the Azure team while working on Raid Azure Blob Storage:
func ValidateVariableValue(variableValue string, regex string) (bool, error) {
// Check if variable is populated
if variableValue == "" {
return false, fmt.Errorf("variable is required and not populated")
}
// Check if variable matches regex
matched, err := regexp.MatchString(regex, variableValue)
if err != nil {
return false, fmt.Errorf("validation of variable has failed with message: %s", err)
}
if !matched {
return false, fmt.Errorf("variable value is not valid")
}
return true, nil
}We could add this directly, as it could have other potential uses.
Or we could add regexp.MatchString to a config variable getter, something like this...
func GetRequiredRegexString(variableName string, result *raidengine.MovementResult) string {
found := GetRequiredVariable(variableName, result)
if !result.Passed {
return ""
}
verified, err := regexp.MatchString(found.(string), "")
if err == nil {
result.Passed = false
result.Message = fmt.Sprintf("Error retrieving variable '%s': %v", variableName, err)
return ""
}
if !verified {
result.Passed = false
result.Message = fmt.Sprintf("Required variable does not match regex: %v", variableName)
return ""
}
return found.(string)
}