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>
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package azurerm
|
|
|
|
import "testing"
|
|
|
|
func TestNormalizeJsonString(t *testing.T) {
|
|
var err error
|
|
var actual string
|
|
|
|
// Well formatted and valid.
|
|
validJson := `{
|
|
"abc": {
|
|
"def": 123,
|
|
"xyz": [
|
|
{
|
|
"a": "ホリネズミ"
|
|
},
|
|
{
|
|
"b": "1\\n2"
|
|
}
|
|
]
|
|
}
|
|
}`
|
|
expected := `{"abc":{"def":123,"xyz":[{"a":"ホリネズミ"},{"b":"1\\n2"}]}}`
|
|
|
|
actual, err = normalizeJsonString(validJson)
|
|
if err != nil {
|
|
t.Fatalf("Expected not to throw an error while parsing JSON, but got: %s", err)
|
|
}
|
|
|
|
if actual != expected {
|
|
t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", actual, expected)
|
|
}
|
|
|
|
// Well formatted but not valid,
|
|
// missing closing squre bracket.
|
|
invalidJson := `{
|
|
"abc": {
|
|
"def": 123,
|
|
"xyz": [
|
|
{
|
|
"a": "1"
|
|
}
|
|
}
|
|
}
|
|
}`
|
|
actual, err = normalizeJsonString(invalidJson)
|
|
if err == nil {
|
|
t.Fatalf("Expected to throw an error while parsing JSON, but got: %s", err)
|
|
}
|
|
|
|
// We expect the invalid JSON to be shown back to us again.
|
|
if actual != invalidJson {
|
|
t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", expected, invalidJson)
|
|
}
|
|
}
|