mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-07 22:53:08 -06:00
e999ae77ba
This commit adds ValidateFunc to the policy attribute so that JSON parsing errors can be caught early. Generally, when there is a ValidateFunc set for the attribute, one can safely assume that before any of the creation and/or update of the existing resource would happen it would have to succeed validation. Also adds support for new helper function which is used to normalise JSON string. Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
59 lines
951 B
Go
59 lines
951 B
Go
package azurerm
|
|
|
|
import "testing"
|
|
|
|
func TestValidateJsonString(t *testing.T) {
|
|
type testCases struct {
|
|
Value string
|
|
ErrCount int
|
|
}
|
|
|
|
invalidCases := []testCases{
|
|
{
|
|
Value: `{0:"1"}`,
|
|
ErrCount: 1,
|
|
},
|
|
{
|
|
Value: `{'abc':1}`,
|
|
ErrCount: 1,
|
|
},
|
|
{
|
|
Value: `{"def":}`,
|
|
ErrCount: 1,
|
|
},
|
|
{
|
|
Value: `{"xyz":[}}`,
|
|
ErrCount: 1,
|
|
},
|
|
}
|
|
|
|
for _, tc := range invalidCases {
|
|
_, errors := validateJsonString(tc.Value, "json")
|
|
if len(errors) != tc.ErrCount {
|
|
t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
|
|
}
|
|
}
|
|
|
|
validCases := []testCases{
|
|
{
|
|
Value: ``,
|
|
ErrCount: 0,
|
|
},
|
|
{
|
|
Value: `{}`,
|
|
ErrCount: 0,
|
|
},
|
|
{
|
|
Value: `{"abc":["1","2"]}`,
|
|
ErrCount: 0,
|
|
},
|
|
}
|
|
|
|
for _, tc := range validCases {
|
|
_, errors := validateJsonString(tc.Value, "json")
|
|
if len(errors) != tc.ErrCount {
|
|
t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
|
|
}
|
|
}
|
|
}
|