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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
     8  )
     9  
    10  // IntBetween returns a SchemaValidateFunc which tests if the provided value
    11  // is of type int and is between min and max (inclusive)
    12  func IntBetween(min, max int) schema.SchemaValidateFunc {
    13  	return func(i interface{}, k string) (warnings []string, errors []error) {
    14  		v, ok := i.(int)
    15  		if !ok {
    16  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
    17  			return warnings, errors
    18  		}
    19  
    20  		if v < min || v > max {
    21  			errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
    22  			return warnings, errors
    23  		}
    24  
    25  		return warnings, errors
    26  	}
    27  }
    28  
    29  // IntAtLeast returns a SchemaValidateFunc which tests if the provided value
    30  // is of type int and is at least min (inclusive)
    31  func IntAtLeast(min int) schema.SchemaValidateFunc {
    32  	return func(i interface{}, k string) (warnings []string, errors []error) {
    33  		v, ok := i.(int)
    34  		if !ok {
    35  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
    36  			return warnings, errors
    37  		}
    38  
    39  		if v < min {
    40  			errors = append(errors, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v))
    41  			return warnings, errors
    42  		}
    43  
    44  		return warnings, errors
    45  	}
    46  }
    47  
    48  // IntAtMost returns a SchemaValidateFunc which tests if the provided value
    49  // is of type int and is at most max (inclusive)
    50  func IntAtMost(max int) schema.SchemaValidateFunc {
    51  	return func(i interface{}, k string) (warnings []string, errors []error) {
    52  		v, ok := i.(int)
    53  		if !ok {
    54  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
    55  			return warnings, errors
    56  		}
    57  
    58  		if v > max {
    59  			errors = append(errors, fmt.Errorf("expected %s to be at most (%d), got %d", k, max, v))
    60  			return warnings, errors
    61  		}
    62  
    63  		return warnings, errors
    64  	}
    65  }
    66  
    67  // IntDivisibleBy returns a SchemaValidateFunc which tests if the provided value
    68  // is of type int and is divisible by a given number
    69  func IntDivisibleBy(divisor int) schema.SchemaValidateFunc {
    70  	return func(i interface{}, k string) (warnings []string, errors []error) {
    71  		v, ok := i.(int)
    72  		if !ok {
    73  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
    74  			return warnings, errors
    75  		}
    76  
    77  		if math.Mod(float64(v), float64(divisor)) != 0 {
    78  			errors = append(errors, fmt.Errorf("expected %s to be divisible by %d, got: %v", k, divisor, i))
    79  			return warnings, errors
    80  		}
    81  
    82  		return warnings, errors
    83  	}
    84  }
    85  
    86  // IntInSlice returns a SchemaValidateFunc which tests if the provided value
    87  // is of type int and matches the value of an element in the valid slice
    88  func IntInSlice(valid []int) schema.SchemaValidateFunc {
    89  	return func(i interface{}, k string) (warnings []string, errors []error) {
    90  		v, ok := i.(int)
    91  		if !ok {
    92  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
    93  			return warnings, errors
    94  		}
    95  
    96  		for _, validInt := range valid {
    97  			if v == validInt {
    98  				return warnings, errors
    99  			}
   100  		}
   101  
   102  		errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %d", k, valid, v))
   103  		return warnings, errors
   104  	}
   105  }
   106  
   107  // IntNotInSlice returns a SchemaValidateFunc which tests if the provided value
   108  // is of type int and matches the value of an element in the valid slice
   109  func IntNotInSlice(valid []int) schema.SchemaValidateFunc {
   110  	return func(i interface{}, k string) (warnings []string, errors []error) {
   111  		v, ok := i.(int)
   112  		if !ok {
   113  			errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
   114  			return warnings, errors
   115  		}
   116  
   117  		for _, validInt := range valid {
   118  			if v == validInt {
   119  				errors = append(errors, fmt.Errorf("expected %s to not be one of %v, got %d", k, valid, v))
   120  			}
   121  		}
   122  
   123  		return warnings, errors
   124  	}
   125  }