github.com/ticketmaster/terraform@v0.10.0-beta2.0.20170711045249-a12daf5aba4f/helper/validation/validation.go (about)

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/hashicorp/terraform/helper/structure"
    10  )
    11  
    12  // IntBetween returns a SchemaValidateFunc which tests if the provided value
    13  // is of type int and is between min and max (inclusive)
    14  func IntBetween(min, max int) schema.SchemaValidateFunc {
    15  	return func(i interface{}, k string) (s []string, es []error) {
    16  		v, ok := i.(int)
    17  		if !ok {
    18  			es = append(es, fmt.Errorf("expected type of %s to be int", k))
    19  			return
    20  		}
    21  
    22  		if v < min || v > max {
    23  			es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
    24  			return
    25  		}
    26  
    27  		return
    28  	}
    29  }
    30  
    31  // IntAtLeast returns a SchemaValidateFunc which tests if the provided value
    32  // is of type int and is at least min (inclusive)
    33  func IntAtLeast(min int) schema.SchemaValidateFunc {
    34  	return func(i interface{}, k string) (s []string, es []error) {
    35  		v, ok := i.(int)
    36  		if !ok {
    37  			es = append(es, fmt.Errorf("expected type of %s to be int", k))
    38  			return
    39  		}
    40  
    41  		if v < min {
    42  			es = append(es, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v))
    43  			return
    44  		}
    45  
    46  		return
    47  	}
    48  }
    49  
    50  // IntAtMost returns a SchemaValidateFunc which tests if the provided value
    51  // is of type int and is at most max (inclusive)
    52  func IntAtMost(max int) schema.SchemaValidateFunc {
    53  	return func(i interface{}, k string) (s []string, es []error) {
    54  		v, ok := i.(int)
    55  		if !ok {
    56  			es = append(es, fmt.Errorf("expected type of %s to be int", k))
    57  			return
    58  		}
    59  
    60  		if v > max {
    61  			es = append(es, fmt.Errorf("expected %s to be at most (%d), got %d", k, max, v))
    62  			return
    63  		}
    64  
    65  		return
    66  	}
    67  }
    68  
    69  // StringInSlice returns a SchemaValidateFunc which tests if the provided value
    70  // is of type string and matches the value of an element in the valid slice
    71  // will test with in lower case if ignoreCase is true
    72  func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
    73  	return func(i interface{}, k string) (s []string, es []error) {
    74  		v, ok := i.(string)
    75  		if !ok {
    76  			es = append(es, fmt.Errorf("expected type of %s to be string", k))
    77  			return
    78  		}
    79  
    80  		for _, str := range valid {
    81  			if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) {
    82  				return
    83  			}
    84  		}
    85  
    86  		es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v))
    87  		return
    88  	}
    89  }
    90  
    91  // StringLenBetween returns a SchemaValidateFunc which tests if the provided value
    92  // is of type string and has length between min and max (inclusive)
    93  func StringLenBetween(min, max int) schema.SchemaValidateFunc {
    94  	return func(i interface{}, k string) (s []string, es []error) {
    95  		v, ok := i.(string)
    96  		if !ok {
    97  			es = append(es, fmt.Errorf("expected type of %s to be string", k))
    98  			return
    99  		}
   100  		if len(v) < min || len(v) > max {
   101  			es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v))
   102  		}
   103  		return
   104  	}
   105  }
   106  
   107  // CIDRNetwork returns a SchemaValidateFunc which tests if the provided value
   108  // is of type string, is in valid CIDR network notation, and has significant bits between min and max (inclusive)
   109  func CIDRNetwork(min, max int) schema.SchemaValidateFunc {
   110  	return func(i interface{}, k string) (s []string, es []error) {
   111  		v, ok := i.(string)
   112  		if !ok {
   113  			es = append(es, fmt.Errorf("expected type of %s to be string", k))
   114  			return
   115  		}
   116  
   117  		_, ipnet, err := net.ParseCIDR(v)
   118  		if err != nil {
   119  			es = append(es, fmt.Errorf(
   120  				"expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err))
   121  			return
   122  		}
   123  
   124  		if ipnet == nil || v != ipnet.String() {
   125  			es = append(es, fmt.Errorf(
   126  				"expected %s to contain a valid network CIDR, expected %s, got %s",
   127  				k, ipnet, v))
   128  		}
   129  
   130  		sigbits, _ := ipnet.Mask.Size()
   131  		if sigbits < min || sigbits > max {
   132  			es = append(es, fmt.Errorf(
   133  				"expected %q to contain a network CIDR with between %d and %d significant bits, got: %d",
   134  				k, min, max, sigbits))
   135  		}
   136  
   137  		return
   138  	}
   139  }
   140  
   141  func ValidateJsonString(v interface{}, k string) (ws []string, errors []error) {
   142  	if _, err := structure.NormalizeJsonString(v); err != nil {
   143  		errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
   144  	}
   145  	return
   146  }