github.com/Hashicorp/terraform@v0.11.12-beta1/helper/customdiff/validate.go (about)

     1  package customdiff
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  )
     6  
     7  // ValueChangeValidationFunc is a function type that validates the difference
     8  // (or lack thereof) between two values, returning an error if the change
     9  // is invalid.
    10  type ValueChangeValidationFunc func(old, new, meta interface{}) error
    11  
    12  // ValueValidationFunc is a function type that validates a particular value,
    13  // returning an error if the value is invalid.
    14  type ValueValidationFunc func(value, meta interface{}) error
    15  
    16  // ValidateChange returns a CustomizeDiffFunc that applies the given validation
    17  // function to the change for the given key, returning any error produced.
    18  func ValidateChange(key string, f ValueChangeValidationFunc) schema.CustomizeDiffFunc {
    19  	return func(d *schema.ResourceDiff, meta interface{}) error {
    20  		old, new := d.GetChange(key)
    21  		return f(old, new, meta)
    22  	}
    23  }
    24  
    25  // ValidateValue returns a CustomizeDiffFunc that applies the given validation
    26  // function to value of the given key, returning any error produced.
    27  //
    28  // This should generally not be used since it is functionally equivalent to
    29  // a validation function applied directly to the schema attribute in question,
    30  // but is provided for situations where composing multiple CustomizeDiffFuncs
    31  // together makes intent clearer than spreading that validation across the
    32  // schema.
    33  func ValidateValue(key string, f ValueValidationFunc) schema.CustomizeDiffFunc {
    34  	return func(d *schema.ResourceDiff, meta interface{}) error {
    35  		val := d.Get(key)
    36  		return f(val, meta)
    37  	}
    38  }