hashicorp / terraform-plugin-framework-validators

Common Use Case Validators for terraform-plugin-framework
Mozilla Public License 2.0
25 stars 12 forks source link

feat: add json string validator #164

Closed teddylear closed 1 year ago

teddylear commented 1 year ago

Adding validator similar to this func from sdk v2 and it's tests for validating a string is valid json. I'm not sure where to edit the changelog, so please let me know or feel free to edit this PR.

Testing

╰─ go test -v -cover -timeout=120s -parallel=4 -run="TestIsJsonValidator" ./stringvalidator/json_test.go
=== RUN   TestIsJsonValidator
=== PAUSE TestIsJsonValidator
=== CONT  TestIsJsonValidator
=== RUN   TestIsJsonValidator/empty
=== PAUSE TestIsJsonValidator/empty
=== RUN   TestIsJsonValidator/empty_brackets
=== PAUSE TestIsJsonValidator/empty_brackets
=== RUN   TestIsJsonValidator/null
=== PAUSE TestIsJsonValidator/null
=== RUN   TestIsJsonValidator/valid_json
=== PAUSE TestIsJsonValidator/valid_json
=== RUN   TestIsJsonValidator/Invalid_1
=== PAUSE TestIsJsonValidator/Invalid_1
=== RUN   TestIsJsonValidator/Invalid_2
=== PAUSE TestIsJsonValidator/Invalid_2
=== RUN   TestIsJsonValidator/Invalid_3
=== PAUSE TestIsJsonValidator/Invalid_3
=== RUN   TestIsJsonValidator/Invalid_4
=== PAUSE TestIsJsonValidator/Invalid_4
=== RUN   TestIsJsonValidator/unknown
=== PAUSE TestIsJsonValidator/unknown
=== CONT  TestIsJsonValidator/empty
=== CONT  TestIsJsonValidator/Invalid_2
=== CONT  TestIsJsonValidator/empty_brackets
=== CONT  TestIsJsonValidator/null
=== CONT  TestIsJsonValidator/Invalid_4
=== CONT  TestIsJsonValidator/valid_json
=== CONT  TestIsJsonValidator/Invalid_3
=== CONT  TestIsJsonValidator/Invalid_1
=== CONT  TestIsJsonValidator/unknown
--- PASS: TestIsJsonValidator (0.00s)
    --- PASS: TestIsJsonValidator/empty (0.00s)
    --- PASS: TestIsJsonValidator/null (0.00s)
    --- PASS: TestIsJsonValidator/Invalid_4 (0.00s)
    --- PASS: TestIsJsonValidator/Invalid_2 (0.00s)
    --- PASS: TestIsJsonValidator/Invalid_3 (0.00s)
    --- PASS: TestIsJsonValidator/Invalid_1 (0.00s)
    --- PASS: TestIsJsonValidator/empty_brackets (0.00s)
    --- PASS: TestIsJsonValidator/unknown (0.00s)
    --- PASS: TestIsJsonValidator/valid_json (0.00s)
PASS
coverage: [no statements]
ok      command-line-arguments  0.137s  coverage: [no statements]
bflad commented 1 year ago

Hi @teddylear 👋 Thank you for this contribution.

As noted in the repository README:

This Go module is not intended to define all possible validations. In particular, many validators that relate to specific string formats, encodings, and other specifics should instead be implemented separately in custom attribute types

To that end, custom JSON types are available in the separate terraform-plugin-framework-jsontypes Go module: https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-jsontypes

If those custom types do not satisfy this use case, please open an issue in that repository.

teddylear commented 1 year ago

@bflad Would it be possible in another PR to link these other validator repos that are ran by Hashicorp in the README of this repo? It's non-obvious where these other validators are and if they even exist. In most Hashicorp docs regarding framework validators only point to this repo.

github-actions[bot] commented 1 year ago

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

bflad commented 1 year ago

@teddylear sorry for the delayed response -- submitted https://github.com/hashicorp/terraform-plugin-framework-validators/pull/173