// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package validators

import (
	"context"
	"encoding/json"
	"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var _ validator.String = JSONParseValidator{}

// JSONParseValidator validates if the provided value is of type string and can be parsed as JSON.
type JSONParseValidator struct {
}

func (validator JSONParseValidator) Description(ctx context.Context) string {
	return "value must be parsable as JSON"
}

func (validator JSONParseValidator) MarkdownDescription(ctx context.Context) string {
	return validator.Description(ctx)
}

func (validator JSONParseValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
	// Only validate the attribute configuration value if it is known.
	if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
		return
	}

	if !json.Valid([]byte(req.ConfigValue.ValueString())) {
		resp.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
			req.Path,
			validator.MarkdownDescription(ctx),
			req.ConfigValue.ValueString(),
		))
		return
	}
}

// IsValidJSON returns an AttributeValidator which ensures that any configured
// attribute value:
//
//   - Is a String.
//   - Is considered valid JSON.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func IsValidJSON() validator.String {
	return JSONParseValidator{}
}
