Skip to content

Commit 86c266b

Browse files
authored
Merge pull request #1 from ContentSquare/master
Allow specifying custom error messages when failing
2 parents 6e114cd + 1cab7e4 commit 86c266b

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,27 @@ locals {
1919
}
2020
2121
resource "errorcheck_is_valid" "shouldMatch" {
22-
name = "shouldMatch"
23-
test = local.compare == local.testSuccess
22+
name = "check_something"
23+
test = {
24+
assert = local.compare == local.testSuccess
25+
error_message = "Your assertion is not valid"
26+
}
2427
}
2528
2629
resource "errorcheck_is_valid" "Not_valid_if_not_match" {
27-
name = "Not_valid_if_not_match"
28-
test = local.compare == local.testFail
30+
name = "Should not match"
31+
test = {
32+
assert = local.compare == local.testFail
33+
error_message = "Your assertion is not valid"
34+
}
2935
}
36+
3037
```
3138
output:
3239
```bash
3340
terraform validate .
3441

35-
Error: Not Valid
42+
Error: Your assertion is not valid
3643

3744
on main.tf line 11, in resource "errorcheck_is_valid" "Not_valid_if_not_match":
3845
11: resource "errorcheck_is_valid" "Not_valid_if_not_match" {

‎main.tf‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ locals {
55
}
66

77
resource "errorcheck_is_valid" "shouldMatch" {
8-
test = local.compare == local.testSuccess
8+
name = "check_something"
9+
test = {
10+
assert = local.compare == local.testSuccess
11+
error_message = "Your assertion is not valid"
12+
}
913
}
1014

1115
resource "errorcheck_is_valid" "Not_valid_if_not_match" {
12-
test = local.compare == local.testFail
16+
name = "Should not match"
17+
test = {
18+
assert = local.compare == local.testFail
19+
error_message = "Your assertion is not valid"
20+
}
1321
}

‎resource_isValid.go‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/hashicorp/terraform/helper/schema"
78
)
@@ -19,12 +20,34 @@ func resourceIsValid() *schema.Resource {
1920
Required: true,
2021
},
2122
"test": &schema.Schema{
22-
Type: schema.TypeBool,
23+
Type: schema.TypeMap,
2324
Required: true,
2425
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
25-
v := val.(bool)
26-
if v != true {
27-
errs = append(errs, fmt.Errorf("Not Valid"))
26+
v := val.(map[string]interface{})
27+
var errorMessage = "Not Valid"
28+
29+
// Get assertValue (string)
30+
assertValue, assertKeyExists := v["assert"]
31+
if !assertKeyExists {
32+
errs = append(errs, fmt.Errorf("'test' map must contain an 'assert' key"))
33+
return
34+
}
35+
36+
// Parse assert value (string -> bool)
37+
assert, err := strconv.ParseBool(assertValue.(string))
38+
if err != nil {
39+
errs = append(errs, fmt.Errorf("Your assert must be a bool: %v", assertValue))
40+
return
41+
}
42+
43+
// Get optional error message
44+
if x, ok := v["error_message"]; ok {
45+
errorMessage = x.(string)
46+
}
47+
48+
// Check assertion
49+
if !assert {
50+
errs = append(errs, fmt.Errorf(errorMessage))
2851
}
2952
return
3053
},
@@ -43,7 +66,7 @@ func resourceIsValidCreate(d *schema.ResourceData, m interface{}) error {
4366
}
4467

4568
func resourceIsValidRead(d *schema.ResourceData, m interface{}) error {
46-
return nil
69+
return nil
4770
}
4871

4972
func resourceIsValidUpdate(d *schema.ResourceData, m interface{}) error {

0 commit comments

Comments
 (0)