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

bugfix for tags validation #617

Merged
merged 1 commit into from
May 31, 2023
Merged
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 @@ -15,13 +15,25 @@ export interface RuleTagsComboBoxProps {
onChange: ((options: EuiComboBoxOptionOption<string>[]) => void) | undefined;
selectedOptions: EuiComboBoxOptionOption<string>[];
}
const STARTS_WITH = 'attack.';

const isValid = (value: string) => {
if (value === '') return true;
return value.startsWith(STARTS_WITH) && value.length > STARTS_WITH.length;
};

export const RuleTagsComboBox: React.FC<RuleTagsComboBoxProps> = ({
onCreateOption,
onBlur,
onChange,
selectedOptions,
}) => {
const [isCurrentlyTypingValueInvalid, setIsCurrentlyTypingValueInvalid] = useState(false);

const onSearchChange = (searchValue: string) => {
setIsCurrentlyTypingValueInvalid(!isValid(searchValue));
};

return (
<>
<EuiFormRow
Expand All @@ -31,15 +43,22 @@ export const RuleTagsComboBox: React.FC<RuleTagsComboBoxProps> = ({
<i>- optional</i>
</EuiText>
}
isInvalid={isCurrentlyTypingValueInvalid}
error={isCurrentlyTypingValueInvalid ? 'Invalid tag' : ''}
helpText={`Tags must start with '${STARTS_WITH}'`}
>
<EuiComboBox
noSuggestions
onSearchChange={onSearchChange}
placeholder="Create tags"
onChange={onChange}
onCreateOption={(searchValue, options) => onCreateOption(searchValue, options)}
onCreateOption={(searchValue, options) =>
isValid(searchValue) && onCreateOption(searchValue, options)
}
onBlur={onBlur}
data-test-subj={'rule_tags_dropdown'}
selectedOptions={selectedOptions}
isInvalid={isCurrentlyTypingValueInvalid}
/>
</EuiFormRow>
</>
Expand Down