Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.82 KB

README.md

File metadata and controls

50 lines (36 loc) · 1.82 KB

V001

The V001 analyzer reports when custom SchemaValidateFunc declarations can be replaced with validation.StringMatch() or validation.StringDoesNotMatch().

Flagged Code

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
}

Passing Code

// 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 the a //lintignore:V001 Go code comment at the end of the offending line or on the line immediately proceding, 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
}