V001¶
ValidationStandard
check for custom SchemaValidateFunc that implement validation.StringMatch() or validation.StringDoesNotMatch()
The V001 analyzer reports when custom SchemaValidateFunc declarations can be replaced with validation.StringMatch() or validation.StringDoesNotMatch().
Examples¶
func validateExampleThing(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^s-([0-9a-f]{17})$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must begin with s- and contain 17 lowercase alphanumeric characters: %q", k, value))
}
return
}
// directly in Schema
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^s-([0-9a-f]{17})$`), "must begin with s- and contain 17 lowercase alphanumeric characters"),
// or saving as a variable
var validateExampleThing = validation.StringMatch(regexp.MustCompile(`^s-([0-9a-f]{17})$`), "must begin with s- and contain 17 lowercase alphanumeric characters")
// or replacing the function
func validateExampleThing() schema.SchemaValidateFunc {
return validation.StringMatch(regexp.MustCompile(`^s-([0-9a-f]{17})$`), "must begin with s- and contain 17 lowercase alphanumeric characters")
}
Ignoring reports¶
Singular reports can be ignored by adding a //lintignore:V001 Go code comment at the end of the offending line or on the line immediately preceding, e.g.
//lintignore:V001
func validateExampleThing(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^s-([0-9a-f]{17})$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must begin with s- and contain 17 lowercase alphanumeric characters: %q", k, value))
}
return
}
This page is generated from passes/V001/README.md, which lives beside the analyzer.