Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,69 @@ describe('Fleet - validatePackagePolicyConfig', () => {
expect(res).toBeNull();
});
});

describe('Select', () => {
it('should return an error message if the value is not an option value', () => {
const res = validatePackagePolicyConfig(
{
type: 'select',
value: 'c',
},
{
name: 'myvariable',
type: 'select',
options: [
{ value: 'a', text: 'A' },
{ value: 'b', text: 'B' },
],
},
'myvariable',
safeLoad
);

expect(res).toEqual(['Invalid value for select type']);
});

it('should accept a select with a valid value', () => {
const res = validatePackagePolicyConfig(
{
type: 'select',
value: 'b',
},
{
name: 'myvariable',
type: 'select',
options: [
{ value: 'a', text: 'A' },
{ value: 'b', text: 'B' },
],
},
'myvariable',
safeLoad
);

expect(res).toBeNull();
});

it('should accept a select with undefined value', () => {
const res = validatePackagePolicyConfig(
{
type: 'select',
value: undefined,
},
{
name: 'myvariable',
type: 'select',
options: [
{ value: 'a', text: 'A' },
{ value: 'b', text: 'B' },
],
},
'myvariable',
safeLoad
);

expect(res).toBeNull();
});
});
});
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/common/services/validate_package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ export const validatePackagePolicyConfig = (
}
}

if (varDef.type === 'select' && parsedValue) {
if (!varDef.options?.map((o) => o.value).includes(parsedValue)) {
errors.push(
i18n.translate('xpack.fleet.packagePolicyValidation.invalidSelectValueErrorMessage', {
defaultMessage: 'Invalid value for select type',
})
);
}
}

return errors.length ? errors : null;
};

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export type RegistryVarType =
| 'integer'
| 'bool'
| 'password'
| 'select'
| 'text'
| 'yaml'
| 'string'
Expand All @@ -390,6 +391,7 @@ export enum RegistryVarsEntryKeys {
required = 'required',
show_user = 'show_user',
multi = 'multi',
options = 'options',
default = 'default',
os = 'os',
}
Expand All @@ -405,6 +407,7 @@ export interface RegistryVarsEntry {
[RegistryVarsEntryKeys.required]?: boolean;
[RegistryVarsEntryKeys.show_user]?: boolean;
[RegistryVarsEntryKeys.multi]?: boolean;
[RegistryVarsEntryKeys.options]?: Array<{ value: string; text: string }>;
[RegistryVarsEntryKeys.default]?: string | string[] | boolean;
[RegistryVarsEntryKeys.os]?: {
[key: string]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
EuiFieldPassword,
EuiCodeBlock,
EuiTextArea,
EuiSelect,
} from '@elastic/eui';
import styled from 'styled-components';

Expand Down Expand Up @@ -58,7 +59,7 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
isEditPage = false,
}) => {
const [isDirty, setIsDirty] = useState<boolean>(false);
const { multi, required, type, title, name, description } = varDef;
const { multi, required, type, title, name, description, options } = varDef;
const isInvalid = (isDirty || forceShowErrors) && !!varErrors;
const errors = isInvalid ? varErrors : null;
const fieldLabel = title || name;
Expand Down Expand Up @@ -154,6 +155,10 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
disabled={frozen}
/>
);
case 'select':
return (
<EuiSelect options={options} value={value} onChange={(e) => onChange(e.target.value)} />
);
default:
return (
<EuiFieldText
Expand All @@ -178,6 +183,7 @@ export const PackagePolicyInputVarField: React.FunctionComponent<{
isEditPage,
isInvalid,
fieldLabel,
options,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suspicious about memoizing an array since it would be compared by reference, but I'm not sure at all if that could be an issue in this case. I didn't see any strangeness with local testing. Does anyone know?

]);

// Boolean cannot be optional by default set to false
Expand Down