-
Notifications
You must be signed in to change notification settings - Fork 143
feat: add example of usage of ValidateDiagFunc #47
Comments
Hi @mavogel ! Been through the same problem but got it running eventually. The function ( for testing purposes )
Then within resource schema
And finally running terraform plan ( expecting to properly fail :) )
Just make sure your imports are correct!
This should work! Please let me know if you cannot get it to run - we can try to sort it out further! |
TY @RafPe it was the wrong import that caused trouble. I also added a unit test and filed a PR #58 // validators.go
func validateStringIsABC(v interface{}, p cty.Path) diag.Diagnostics {
value := v.(string)
var diags diag.Diagnostics
if value != "abc" {
diag := diag.Diagnostic{
Severity: diag.Error,
Summary: "wrong value",
Detail: fmt.Sprintf("%q is not abc", value),
}
diags = append(diags, diag)
}
return diags
}
// validators_test.go
func TestValidateStringShouldBeABC(t *testing.T) {
v := "abc"
if diags := validateStringIsABC(v, *new(cty.Path)); len(diags) != 0 {
t.Fatalf("%q should be abc", v)
}
v = "not-abc"
if diags := validateStringIsABC(v, *new(cty.Path)); len(diags) == 0 {
t.Fatalf("%q should NOT be abc", v)
}
} |
Hi @mavogel 👋 Thank you for raising this. Having appropriate documentation on Would you be interested in introducing this documentation on the website instead? The source code can be found at: https://github.com/hashicorp/terraform-website/blob/master/content/plugin/sdkv2/schemas/schema-behaviors.mdx Thanks! |
Details
An example of
ValidateFunc
would be helpful.
Currently, I cannot get it running:
and the implementation:
I get the error
Versions
v0.14.3
v2.4.0
Links
The text was updated successfully, but these errors were encountered: