Skip to content

Commit

Permalink
Validation and documentation edits
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickcping committed Dec 27, 2023
1 parent c7bb29d commit 4a9cf38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 65 deletions.
22 changes: 11 additions & 11 deletions docs/resources/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Optional:

- `alignment` (String) **Required** when the `type` is one of `QR_CODE`, `RECAPTCHA_V2`. A string that specifies the reCAPTCHA alignment. Options are `CENTER`, `LEFT`, `RIGHT`.
- `attribute_disabled` (Boolean) Optional when the `type` is one of `CHECKBOX`, `COMBOBOX`, `DROPDOWN`, `PASSWORD`, `PASSWORD_VERIFY`, `RADIO`, `TEXT`. A boolean that specifies whether the linked directory attribute is disabled.
- `content` (String) Optional when the `type` is one of `SLATE_TEXTBLOB`, `TEXTBLOB`.
- `content` (String) Optional when the `type` is one of `SLATE_TEXTBLOB`, `TEXTBLOB`. A string that specifies the field's content (for example, HTML when the field type is `TEXTBLOB`.)
- `key` (String) **Required** when the `type` is one of `CHECKBOX`, `COMBOBOX`, `DROPDOWN`, `FLOW_BUTTON`, `FLOW_LINK`, `PASSWORD`, `PASSWORD_VERIFY`, `QR_CODE`, `RADIO`, `TEXT`. A string that specifies an identifier for the field component.
- `label` (String) **Required** when the `type` is one of `CHECKBOX`, `COMBOBOX`, `DROPDOWN`, `FLOW_BUTTON`, `FLOW_LINK`, `PASSWORD`, `PASSWORD_VERIFY`, `RADIO`, `SUBMIT_BUTTON`, `TEXT`. A string that specifies the field label.
- `label_mode` (String) Optional when the `type` is one of `CHECKBOX`, `COMBOBOX`, `DROPDOWN`, `PASSWORD`, `PASSWORD_VERIFY`, `RADIO`, `TEXT`. A string that specifies how the field is rendered. Options are `DEFAULT`, `FLOAT`.
Expand All @@ -220,7 +220,7 @@ Optional:
- `qr_code_type` (String) **Required** when the `type` is one of `QR_CODE`. A string that specifies the QR Code type.
- `required` (Boolean) Optional when the `type` is one of `CHECKBOX`, `COMBOBOX`, `DROPDOWN`, `PASSWORD`, `PASSWORD_VERIFY`, `RADIO`, `TEXT`. A boolean that specifies whether the field is required.
- `show_border` (Boolean) Optional when the `type` is one of `QR_CODE`. A boolean that specifies the border visibility.
- `show_password_requirements` (Boolean) Optional when the `type` is one of `PASSWORD`, `PASSWORD_VERIFY`.
- `show_password_requirements` (Boolean) Optional when the `type` is one of `PASSWORD`, `PASSWORD_VERIFY`. A boolean that specifies whether to display password requirements to the user.
- `size` (String) **Required** when the `type` is one of `RECAPTCHA_V2`. A string that specifies the reCAPTCHA size. Options are `COMPACT`, `NORMAL`.
- `styles` (Attributes) Optional when the `type` is one of `FLOW_BUTTON`, `FLOW_LINK`, `SUBMIT_BUTTON`. A single object that describes style settings for the field. (see [below for nested schema](#nestedatt--components--fields--styles))
- `theme` (String) **Required** when the `type` is one of `RECAPTCHA_V2`. A string that specifies the reCAPTCHA theme. Options are `DARK`, `LIGHT`.
Expand Down Expand Up @@ -252,8 +252,8 @@ Optional:

Required:

- `label` (String)
- `value` (String)
- `label` (String) A string that specifies the option's label in the form field that is shown to the end user.
- `value` (String) A string that specifies the option's value in the form field that is posted as form data.


<a id="nestedatt--components--fields--styles"></a>
Expand All @@ -265,21 +265,21 @@ Optional:
- `background_color` (String) A string that specifies the button background color. The value must be a valid hexadecimal color.
- `border_color` (String) A string that specifies the button border color. The value must be a valid hexadecimal color.
- `enabled` (Boolean) A boolean that specifies whether the button is enabled.
- `height` (Number)
- `padding` (Attributes) (see [below for nested schema](#nestedatt--components--fields--styles--padding))
- `height` (Number) An integer that specifies a custom height of the field (in pixels) when displayed in the form.
- `padding` (Attributes) A single object that specifies custom padding styles for the field. (see [below for nested schema](#nestedatt--components--fields--styles--padding))
- `text_color` (String) A string that specifies the button text color. The value must be a valid hexadecimal color.
- `width` (Number) An integer that specifies the button width. Set as a percentage.
- `width_unit` (String)
- `width_unit` (String) A string that specifies the unit to apply to the `width` parameter. Options are `PERCENT`, `PIXELS`.

<a id="nestedatt--components--fields--styles--padding"></a>
### Nested Schema for `components.fields.styles.width_unit`

Optional:

- `bottom` (Number)
- `left` (Number)
- `right` (Number)
- `top` (Number)
- `bottom` (Number) An integer that specifies the bottom padding (in pixels) to apply to the field.
- `left` (Number) An integer that specifies the left padding (in pixels) to apply to the field.
- `right` (Number) An integer that specifies the right padding (in pixels) to apply to the field.
- `top` (Number) An integer that specifies the top padding (in pixels) to apply to the field.



Expand Down
87 changes: 33 additions & 54 deletions internal/service/base/resource_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
Expand Down Expand Up @@ -511,7 +512,7 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
componentsFieldsContentDescription := framework.SchemaAttributeDescriptionFromMarkdown(
formFieldValidationDocumentation("content"),
).AppendMarkdownString(
"",
fmt.Sprintf("A string that specifies the field's content (for example, HTML when the field type is `%s`.)", string(management.ENUMFORMFIELDTYPE_TEXTBLOB)),
)

componentsFieldsKeyDescription := framework.SchemaAttributeDescriptionFromMarkdown(
Expand Down Expand Up @@ -551,11 +552,11 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
)

componentsFieldsOptionsLabelDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"A string that specifies the option's label in the form field that is shown to the end user.",
)

componentsFieldsOptionsValueDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"A string that specifies the option's value in the form field that is posted as form data.",
)

componentsFieldsRequiredDescription := framework.SchemaAttributeDescriptionFromMarkdown(
Expand Down Expand Up @@ -605,7 +606,7 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
componentsFieldsShowPasswordRequirementsDescription := framework.SchemaAttributeDescriptionFromMarkdown(
formFieldValidationDocumentation("show_password_requirements"),
).AppendMarkdownString(
"",
"A boolean that specifies whether to display password requirements to the user.",
)

componentsFieldsStylesDescription := framework.SchemaAttributeDescriptionFromMarkdown(
Expand Down Expand Up @@ -635,36 +636,36 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
)

componentsFieldsStylesHeightDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"An integer that specifies a custom height of the field (in pixels) when displayed in the form.",
)

componentsFieldsStylesPaddingDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"A single object that specifies custom padding styles for the field.",
)

componentsFieldsStylesPaddingTopDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"An integer that specifies the top padding (in pixels) to apply to the field.",
)

componentsFieldsStylesPaddingRightDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"An integer that specifies the right padding (in pixels) to apply to the field.",
)

componentsFieldsStylesPaddingBottomDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"An integer that specifies the bottom padding (in pixels) to apply to the field.",
)

componentsFieldsStylesPaddingLeftDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
"An integer that specifies the left padding (in pixels) to apply to the field.",
)

componentsFieldsStylesTextColorDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"A string that specifies the button text color. The value must be a valid hexadecimal color.",
)

componentsFieldsStylesWidthUnitDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"",
)
"A string that specifies the unit to apply to the `width` parameter.",
).AllowedValuesEnum(management.AllowedEnumFormStylesWidthUnitEnumValues)

Check failure on line 668 in internal/service/base/resource_form.go

View workflow job for this annotation

GitHub Actions / Build

undefined: management.AllowedEnumFormStylesWidthUnitEnumValues

Check failure on line 668 in internal/service/base/resource_form.go

View workflow job for this annotation

GitHub Actions / Build

undefined: management.AllowedEnumFormStylesWidthUnitEnumValues

Check failure on line 668 in internal/service/base/resource_form.go

View workflow job for this annotation

GitHub Actions / Build

undefined: management.AllowedEnumFormStylesWidthUnitEnumValues

Check failure on line 668 in internal/service/base/resource_form.go

View workflow job for this annotation

GitHub Actions / Build

undefined: management.AllowedEnumFormStylesWidthUnitEnumValues

componentsFieldsSizeDescription := framework.SchemaAttributeDescriptionFromMarkdown(
formFieldValidationDocumentation("size"),
Expand Down Expand Up @@ -828,40 +829,30 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: componentsFieldsAttributeDisabledDescription.MarkdownDescription,
Optional: true,
Computed: true,

// TODO: Validator can't be false if key is "user.username" or similar
},

"content": schema.StringAttribute{
Description: componentsFieldsContentDescription.Description,
MarkdownDescription: componentsFieldsContentDescription.MarkdownDescription,
Optional: true,

// TODO Functional validator
},

"key": schema.StringAttribute{
Description: componentsFieldsKeyDescription.Description,
MarkdownDescription: componentsFieldsKeyDescription.MarkdownDescription,
Optional: true,

// TODO Functional validator
},

"label": schema.StringAttribute{
Description: componentsFieldsLabelDescription.Description,
MarkdownDescription: componentsFieldsLabelDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"label_password_verify": schema.StringAttribute{
Description: componentsFieldsLabelPasswordVerifyDescription.Description,
MarkdownDescription: componentsFieldsLabelPasswordVerifyDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"label_mode": schema.StringAttribute{
Expand All @@ -879,8 +870,6 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: componentsFieldsLayoutDescription.MarkdownDescription,
Optional: true,

// TODO functional validator

Validators: []validator.String{
stringvalidator.OneOf(utils.EnumSliceToStringSlice(management.AllowedEnumFormElementLayoutEnumValues)...),
},
Expand All @@ -891,8 +880,6 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: componentsFieldsOptionsDescription.MarkdownDescription,
Optional: true,

// TODO functional validator

NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"label": schema.StringAttribute{
Expand All @@ -915,17 +902,13 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: componentsFieldsRequiredDescription.MarkdownDescription,
Optional: true,
Computed: true,

// TODO: Validator can't be false if key is "user.username" or similar
},

"validation": schema.SingleNestedAttribute{
Description: componentsFieldsValidationDescription.Description,
MarkdownDescription: componentsFieldsValidationDescription.MarkdownDescription,
Optional: true,

// TODO: optional validator

Attributes: map[string]schema.Attribute{
"regex": schema.StringAttribute{
Description: componentsFieldsValidationRegexDescription.Description,
Expand Down Expand Up @@ -968,58 +951,44 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
Description: componentsFieldsOtherOptionEnabledDescription.Description,
MarkdownDescription: componentsFieldsOtherOptionEnabledDescription.MarkdownDescription,
Computed: true,

// TODO: functional validator
},

"other_option_key": schema.StringAttribute{
Description: componentsFieldsOtherOptionKeyDescription.Description,
MarkdownDescription: componentsFieldsOtherOptionKeyDescription.MarkdownDescription,
Computed: true,

// TODO: functional validator
},

"other_option_label": schema.StringAttribute{
Description: componentsFieldsOtherOptionLabelDescription.Description,
MarkdownDescription: componentsFieldsOtherOptionLabelDescription.MarkdownDescription,
Computed: true,

// TODO: functional validator
},

"other_option_input_label": schema.StringAttribute{
Description: componentsFieldsOtherOptionInputLabelDescription.Description,
MarkdownDescription: componentsFieldsOtherOptionInputLabelDescription.MarkdownDescription,
Computed: true,

// TODO: functional validator
},

"other_option_attribute_disabled": schema.BoolAttribute{
Description: componentsFieldsOtherOptionAttributeDisabledDescription.Description,
MarkdownDescription: componentsFieldsOtherOptionAttributeDisabledDescription.MarkdownDescription,
Computed: true,

// TODO: functional validator
},

"show_password_requirements": schema.BoolAttribute{
Description: componentsFieldsShowPasswordRequirementsDescription.Description,
MarkdownDescription: componentsFieldsShowPasswordRequirementsDescription.MarkdownDescription,
Optional: true,
Computed: true,

// TODO: functional validator
},

"styles": schema.SingleNestedAttribute{
Description: componentsFieldsStylesDescription.Description,
MarkdownDescription: componentsFieldsStylesDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator

Attributes: map[string]schema.Attribute{
"alignment": schema.StringAttribute{
Description: componentsFieldsStylesAlignmentDescription.Description,
Expand Down Expand Up @@ -1112,41 +1081,31 @@ func (r *FormResource) Schema(ctx context.Context, req resource.SchemaRequest, r
Description: componentsFieldsSizeDescription.Description,
MarkdownDescription: componentsFieldsSizeDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"theme": schema.StringAttribute{
Description: componentsFieldsThemeDescription.Description,
MarkdownDescription: componentsFieldsThemeDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"alignment": schema.StringAttribute{
Description: componentsFieldsAlignmentDescription.Description,
MarkdownDescription: componentsFieldsAlignmentDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"qr_code_type": schema.StringAttribute{
Description: componentsFieldsQrCodeTypeDescription.Description,
MarkdownDescription: componentsFieldsQrCodeTypeDescription.MarkdownDescription,
Optional: true,

// TODO: functional validator
},

"show_border": schema.BoolAttribute{
Description: componentsFieldsShowBorderDescription.Description,
MarkdownDescription: componentsFieldsShowBorderDescription.MarkdownDescription,
Optional: true,
Computed: true,

// TODO: functional validator
},
},
},
Expand Down Expand Up @@ -1297,6 +1256,26 @@ func (r *FormResource) ValidateConfig(ctx context.Context, req resource.Validate
fmt.Sprintf("The form field type `%s` does not have Required/Optional metadata configured. Please report this to the provider maintainers.", field.Type.ValueString()),
)
}

// Validate parameters must have specific values if the `key` is a user field
m, err := regexp.Compile(`^user\..+$`)
if err != nil {
resp.Diagnostics.AddError(
"Unexpected error",
fmt.Sprintf("Failed to compile regex: %s. This is always a bug in the provider. Please report this error to the provider maintainers.", err.Error()),
)
return
}

if m.MatchString(field.Key.ValueString()) {
if !field.Required.IsNull() && !field.Required.IsUnknown() && !field.Required.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("components").AtName("fields"),
"Invalid DaVinci form configuration",
fmt.Sprintf("The `required` parameter must be set to `true` for the `%s` field type when the `key` is a user field.", field.Type.ValueString()),
)
}
}
}

// Validate has submit button
Expand Down

0 comments on commit 4a9cf38

Please sign in to comment.