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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
     7  )
     8  
     9  // FloatBetween returns a SchemaValidateFunc which tests if the provided value
    10  // is of type float64 and is between min and max (inclusive).
    11  func FloatBetween(min, max float64) schema.SchemaValidateFunc {
    12  	return func(i interface{}, k string) (s []string, es []error) {
    13  		v, ok := i.(float64)
    14  		if !ok {
    15  			es = append(es, fmt.Errorf("expected type of %s to be float64", k))
    16  			return
    17  		}
    18  
    19  		if v < min || v > max {
    20  			es = append(es, fmt.Errorf("expected %s to be in the range (%f - %f), got %f", k, min, max, v))
    21  			return
    22  		}
    23  
    24  		return
    25  	}
    26  }
    27  
    28  // FloatAtLeast returns a SchemaValidateFunc which tests if the provided value
    29  // is of type float and is at least min (inclusive)
    30  func FloatAtLeast(min float64) schema.SchemaValidateFunc {
    31  	return func(i interface{}, k string) (s []string, es []error) {
    32  		v, ok := i.(float64)
    33  		if !ok {
    34  			es = append(es, fmt.Errorf("expected type of %s to be float", k))
    35  			return
    36  		}
    37  
    38  		if v < min {
    39  			es = append(es, fmt.Errorf("expected %s to be at least (%f), got %f", k, min, v))
    40  			return
    41  		}
    42  
    43  		return
    44  	}
    45  }
    46  
    47  // FloatAtMost returns a SchemaValidateFunc which tests if the provided value
    48  // is of type float and is at most max (inclusive)
    49  func FloatAtMost(max float64) schema.SchemaValidateFunc {
    50  	return func(i interface{}, k string) (s []string, es []error) {
    51  		v, ok := i.(float64)
    52  		if !ok {
    53  			es = append(es, fmt.Errorf("expected type of %s to be float", k))
    54  			return
    55  		}
    56  
    57  		if v > max {
    58  			es = append(es, fmt.Errorf("expected %s to be at most (%f), got %f", k, max, v))
    59  			return
    60  		}
    61  
    62  		return
    63  	}
    64  }