Skip to content

[Feature]: JSON Schema Validator Function #441

@Gowiem

Description

@Gowiem

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.

Activity

skyzyx

skyzyx commented on Jan 23, 2025

@skyzyx
Member

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.

added a commit that references this issue on Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @skyzyx@Gowiem

      Issue actions

        [Feature]: JSON Schema Validator Function · Issue #441 · northwood-labs/terraform-provider-corefunc