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

feat: require variable only when displayed ADDON-67013 #985

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions docs/entity/index.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
---
title: Entity
title: Entity
---

## Entity Properties

| Property | Type | Description | Default Value |
| ------------------------------------------------------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| field<span class="required-asterisk">*</span> | string | To define a particular entity field. | - |
| label<span class="required-asterisk">*</span> | string | It represents a caption for a field in a user interface. | - |
| [type](./components)<span class="required-asterisk">*</span> | string | To specify the type of entity to be rendered in inputs or configuration form. | - |
| Property | Type | Description | Default Value |
| ------------------------------------------------------------- | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| field<span class="required-asterisk">\*</span> | string | To define a particular entity field. | - |
| label<span class="required-asterisk">\*</span> | string | It represents a caption for a field in a user interface. | - |
| [type](./components)<span class="required-asterisk">\*</span> | string | To specify the type of entity to be rendered in inputs or configuration form. | - |
| help | string | Help text gives context about a fields input, such as how the input will be used. It is displayed directly below an input field. | - |
| tooltip | string | Displays a tooltip beside the label. | - |
| defaultValue | string, number or boolean | The initial input value. | - |
| [options](#common-options) | object | To specify an additional attribute for a particular type of entity, such as `items` for a radio bar. | - |
| required | boolean | To specify whether the field is required or not. | false |
| encrypted | boolean | To encrypt that particular field. | false |
| [validators](./validators) | array | It is used to validate the values of fields using various validators. | - |
| tooltip | string | Displays a tooltip beside the label. | - |
| defaultValue | string, number or boolean | The initial input value. | - |
| [options](#common-options) | object | To specify an additional attribute for a particular type of entity, such as `items` for a radio bar. | - |
| required | boolean | To specify whether the field is required or not. | false |
| encrypted | boolean | To encrypt that particular field. | false |
| [validators](./validators) | array | It is used to validate the values of fields using various validators. | - |

> [!WARNING]
> [Placeholder](https://splunkui.splunkeng.com/Packages/react-ui/Text?section=develop) attribute is deprecated and will be removed in the next major version. Instead, we recommend to use "help" attribute.

## Common Options

| Property | Type | Description | Default Value |
| ------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| placeholder | string | `Deprecated` The grey text is shown when the input is empty. | - |
| display | boolean | Whether show or hide the field. | true |
| disableonEdit | boolean | When the form is in edit mode, the field becomes uneditable. | false |
| enable | boolean | The enable property sets whether a field is enabled, or not. | true |
| Property | Type | Description | Default Value |
| ------------------- | ------- | --------------------------------------------------------------------------------- | ------------- |
| placeholder | string | `Deprecated` The grey text is shown when the input is empty. | - |
| display | boolean | Whether show or hide the field. | true |
| disableonEdit | boolean | When the form is in edit mode, the field becomes uneditable. | false |
| enable | boolean | The enable property sets whether a field is enabled, or not. | true |
| requiredWhenVisible | boolean | Make field required from UI perspecive when it appears \*used only for visibility | false |
24 changes: 24 additions & 0 deletions splunk_add_on_ucc_framework/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@
},
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -516,6 +519,9 @@
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"rowsMin": {
"type": "number"
},
Expand Down Expand Up @@ -604,6 +610,9 @@
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"disableSearch": {
"type": "boolean"
},
Expand Down Expand Up @@ -716,6 +725,9 @@
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"disableSearch": {
"type": "boolean"
},
Expand Down Expand Up @@ -809,6 +821,9 @@
},
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -976,6 +991,9 @@
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"items": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1043,6 +1061,9 @@
"enable": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"maxFileSize": {
"type": "number"
},
Expand Down Expand Up @@ -1096,6 +1117,9 @@
"disableonEdit": {
"type": "boolean"
},
"requiredWhenVisible": {
"type": "boolean"
},
"enable": {
"type": "boolean"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"help": "Enter the endpoint URL.",
"field": "endpoint",
"options": {
"display": false
"display": false,
"requiredWhenVisible": true
}
},
{
Expand Down Expand Up @@ -1354,7 +1355,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.35.1Rb83f2c0b",
"version": "5.35.1R9330d2a9",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.3"
}
Expand Down
8 changes: 8 additions & 0 deletions ui/src/components/BaseFormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ class BaseFormView extends PureComponent {
temEntities = this.entities;
}

// require elements for UI when they are visible
temEntities = temEntities.map((entity) => {
if (entity?.options?.requiredWhenVisible && this.state.data[entity.field].display) {
return { required: true, ...entity };
}
return entity;
});

// Validation of form fields on Submit
const validator = new Validator(temEntities);
let error = validator.doValidation(this.datadict);
Expand Down
6 changes: 5 additions & 1 deletion ui/src/types/globalConfig/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const CommonEditableEntityOptions = z.object({
display: z.boolean().optional().default(true),
disableonEdit: z.boolean().optional().default(false),
enable: z.boolean().optional().default(true),
requiredWhenVisible: z.boolean().optional().default(false),
});

const AllValidators = z
Expand Down Expand Up @@ -198,7 +199,10 @@ export const OAuthEntity = CommonEditableEntityFields.extend({
type: z.literal('oauth'),
defaultValue: z.string().optional(),
validators: z.array(z.union([StringValidator, RegexValidator])).optional(),
options: CommonEditableEntityOptions.omit({ placeholder: true }).extend({
options: CommonEditableEntityOptions.omit({
placeholder: true,
requiredWhenVisible: true,
}).extend({
auth_type: z.array(z.union([z.literal('basic'), z.literal('oauth')])),
basic: z.array(OAuthFields).optional(),
oauth: z.array(OAuthFields).optional(),
Expand Down
Loading