-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validator for
body
and identity
fields
- Loading branch information
Showing
11 changed files
with
100 additions
and
4 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
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
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
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,40 @@ | ||
package myvalidator | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type bodyValidator struct{} | ||
|
||
func (v bodyValidator) Description(ctx context.Context) string { | ||
return "validate the identity block" | ||
} | ||
|
||
func (v bodyValidator) MarkdownDescription(ctx context.Context) string { | ||
return "validate the identity block" | ||
} | ||
|
||
func (_ bodyValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) { | ||
raw := req.ConfigValue | ||
|
||
if raw.IsUnknown() || raw.IsNull() { | ||
return | ||
} | ||
|
||
switch value := raw.UnderlyingValue().(type) { | ||
case types.String: | ||
var out interface{} | ||
err := json.Unmarshal([]byte(value.ValueString()), &out) | ||
if err != nil { | ||
resp.Diagnostics.AddAttributeError(req.Path, "Invalid JSON", err.Error()) | ||
} | ||
} | ||
} | ||
|
||
func BodyValidator() validator.Dynamic { | ||
return bodyValidator{} | ||
} |
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,40 @@ | ||
package myvalidator | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure/terraform-provider-azapi/internal/azure/identity" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
) | ||
|
||
type identityValidator struct{} | ||
|
||
func (v identityValidator) Description(ctx context.Context) string { | ||
return "validate the identity block" | ||
} | ||
|
||
func (v identityValidator) MarkdownDescription(ctx context.Context) string { | ||
return "validate the identity block" | ||
} | ||
|
||
func (_ identityValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) { | ||
value := req.ConfigValue | ||
|
||
if value.IsUnknown() || value.IsNull() { | ||
return | ||
} | ||
|
||
var model identity.Model | ||
if resp.Diagnostics.Append(value.As(ctx, &model, basetypes.ObjectAsOptions{})...); resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if _, err := identity.ExpandIdentity(model); err != nil { | ||
resp.Diagnostics.AddAttributeError(req.Path, "Invalid `identity` block", err.Error()) | ||
} | ||
} | ||
|
||
func IdentityValidator() validator.Object { | ||
return identityValidator{} | ||
} |
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
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