Skip to content

Add Regex validation for config vars #15

@eddie-knight

Description

@eddie-knight

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions