github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/list.go (about)

     1  package validation
     2  
     3  import "fmt"
     4  
     5  // ValidateListUniqueStrings is a ValidateFunc that ensures a list has no
     6  // duplicate items in it. It's useful for when a list is needed over a set
     7  // because order matters, yet the items still need to be unique.
     8  //
     9  // Deprecated: use ListOfUniqueStrings
    10  func ValidateListUniqueStrings(i interface{}, k string) (warnings []string, errors []error) {
    11  	return ListOfUniqueStrings(i, k)
    12  }
    13  
    14  // ListOfUniqueStrings is a ValidateFunc that ensures a list has no
    15  // duplicate items in it. It's useful for when a list is needed over a set
    16  // because order matters, yet the items still need to be unique.
    17  func ListOfUniqueStrings(i interface{}, k string) (warnings []string, errors []error) {
    18  	v, ok := i.([]interface{})
    19  	if !ok {
    20  		errors = append(errors, fmt.Errorf("expected type of %q to be List", k))
    21  		return warnings, errors
    22  	}
    23  
    24  	for _, e := range v {
    25  		if _, eok := e.(string); !eok {
    26  			errors = append(errors, fmt.Errorf("expected %q to only contain string elements, found :%v", k, e))
    27  			return warnings, errors
    28  		}
    29  	}
    30  
    31  	for n1, i1 := range v {
    32  		for n2, i2 := range v {
    33  			if i1.(string) == i2.(string) && n1 != n2 {
    34  				errors = append(errors, fmt.Errorf("expected %q to not have duplicates: found 2 or more of %v", k, i1))
    35  				return warnings, errors
    36  			}
    37  		}
    38  	}
    39  
    40  	return warnings, errors
    41  }