generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb4f2ec
commit eaf3ca0
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "valid_json function - assert" | ||
subcategory: "" | ||
description: |- | ||
Checks whether a string is valid JSON | ||
--- | ||
|
||
# function: valid_json | ||
|
||
|
||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
valid_json(json string) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `json` (String) The JSON string to check | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var ( | ||
_ function.Function = ValidJSONFunction{} | ||
) | ||
|
||
func NewValidJSONFunction() function.Function { | ||
return ValidJSONFunction{} | ||
} | ||
|
||
type ValidJSONFunction struct{} | ||
|
||
func (r ValidJSONFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "valid_json" | ||
} | ||
|
||
func (r ValidJSONFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Checks whether a string is valid JSON", | ||
Parameters: []function.Parameter{ | ||
function.StringParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: true, | ||
Description: "The JSON string to check", | ||
Name: "json", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (r ValidJSONFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var JSON *string | ||
|
||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &JSON)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
isValidJSON, err := isValidJSON(JSON) | ||
if err != nil { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(err.Error())) | ||
return | ||
} | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, isValidJSON)) | ||
} | ||
|
||
func isValidJSON(JSON *string) (bool, error) { | ||
var js map[string]interface{} | ||
err := json.Unmarshal([]byte(*JSON), &js) | ||
if err != nil { | ||
return false, nil | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestValidJSONFunction_basic(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
locals { | ||
json = jsonencode({ | ||
foo = "bar" | ||
}) | ||
} | ||
output "test" { | ||
value = provider::assert::valid_json(local.json) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestValidJSONFunction_multiline(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
locals { | ||
json = <<EOF | ||
{ | ||
"foo": "bar" | ||
} | ||
EOF | ||
} | ||
output "test" { | ||
value = provider::assert::valid_json(local.json) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |