Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: JSON Schema Validator Function #441

Open
Gowiem opened this issue Jan 23, 2025 · 2 comments
Open

[Feature]: JSON Schema Validator Function #441

Gowiem opened this issue Jan 23, 2025 · 2 comments
Assignees

Comments

@Gowiem
Copy link

Gowiem commented Jan 23, 2025

What functionality would you like to see?

My team and I have run into an issue a number of times with what I'm calling the "Optional Type Defaults Problem" (bad name). Lots of info in the OpenTofu Slack. The quick summary: when merging objects with optional fields, there can be null values that unintentionally override desired defaults. This leads to unexpected / undesired behavior in complex module configurations.

The end result of this issue is that we can't use the TF type system to do what we want and instead we're going to specify any as our type for complex objects in certain cases and then we'll use a JSON schema to validate the given variable value at runtime. To do this today, we're thinking about using the jsonschema_validator data source from the jsonschema providera. However that is an old, seemingly unpopular provider and the validation is not implemented as a function.

I would love to see a str_jsonschema_validate function in this provider. If there is interest, I can try to put up a PR to add this.

@skyzyx
Copy link
Member

skyzyx commented Jan 23, 2025

I've been job hunting recently, so I've not been working on this as quickly as I'd like, but I was already looking into implementing this before I got laid off.

The native type system is complex. So if there's merging involved, I've been experimenting with:

  1. Encoding a native type (or types) as JSON via jsonencode.
  2. Passing that to a corefunc::deep_merge() function of some kind (using darccio/mergo). Respond with JSON.
  3. Decode back to native types via jsondecode.

I like the idea of validating inputs using JSON Schema (using something like santhosh-tekuri/jsonschema, which supports a broad set of dialects).

Perhaps a JSON validation function could slide between steps 2 and 3, with the option to WARN or ERROR on failure. I was trying to do something like this at a module level back in the Terraform 0.14 days when "optionals" were introduced as an experiment. I think I have some good test cases for such a feature.

@skyzyx
Copy link
Member

skyzyx commented Jan 23, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants