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

[Uptime] Settings threshold validation #65454

Merged
merged 7 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -58,7 +58,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
>
<EuiFormRow
describedByIds={['errorState']}
error={fieldErrors?.certificatesThresholds?.expirationThresholdError}
error={fieldErrors?.expirationThresholdError}
fullWidth
helpText={
<FormattedMessage
Expand All @@ -69,7 +69,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
}}
/>
}
isInvalid={!!fieldErrors?.certificatesThresholds?.expirationThresholdError}
isInvalid={!!fieldErrors?.expirationThresholdError}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.errorStateLabel"
Expand All @@ -80,6 +80,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
<EuiFlexGroup>
<EuiFlexItem grow={2}>
<EuiFieldNumber
min={1}
data-test-subj={`expiration-threshold-input-${loading ? 'loading' : 'loaded'}`}
fullWidth
disabled={isDisabled}
Expand All @@ -104,7 +105,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
</EuiFormRow>
<EuiFormRow
describedByIds={['warningState']}
error={fieldErrors?.certificatesThresholds?.ageThresholdError}
error={fieldErrors?.ageThresholdError}
fullWidth
helpText={
<FormattedMessage
Expand All @@ -115,7 +116,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
}}
/>
}
isInvalid={!!fieldErrors?.certificatesThresholds?.ageThresholdError}
isInvalid={!!fieldErrors?.ageThresholdError}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.warningStateLabel"
Expand All @@ -126,14 +127,15 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
<EuiFlexGroup>
<EuiFlexItem grow={2}>
<EuiFieldNumber
min={1}
data-test-subj={`age-threshold-input-${loading ? 'loading' : 'loaded'}`}
fullWidth
disabled={isDisabled}
isLoading={loading}
value={formFields?.certAgeThreshold ?? ''}
onChange={e =>
onChange={({ currentTarget: { value } }) =>
onChange({
certAgeThreshold: Number(e.currentTarget.value),
certAgeThreshold: Number(value),
})
}
/>
Expand Down
38 changes: 21 additions & 17 deletions x-pack/plugins/uptime/public/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ import {
import * as Translations from './translations';

interface SettingsPageFieldErrors {
heartbeatIndices: 'May not be blank' | '';
certificatesThresholds: {
expirationThresholdError: string | null;
ageThresholdError: string | null;
} | null;
heartbeatIndices: string | '';
expirationThresholdError?: string;
ageThresholdError?: string;
}

export interface SettingsFormProps {
Expand All @@ -49,22 +47,28 @@ export interface SettingsFormProps {
isDisabled: boolean;
}

const isValidCertVal = (val: string | number) => {
if (val === '') {
return Translations.BLANK_STR;
}
if (val === 0) {
return Translations.VALID_STR;
}
};

const getFieldErrors = (formFields: DynamicSettings | null): SettingsPageFieldErrors | null => {
if (formFields) {
const blankStr = 'May not be blank';
const { certAgeThreshold, certExpirationThreshold, heartbeatIndices } = formFields;
const heartbeatIndErr = heartbeatIndices.match(/^\S+$/) ? '' : blankStr;
const expirationThresholdError = certExpirationThreshold ? null : blankStr;
const ageThresholdError = certAgeThreshold ? null : blankStr;

const indError = heartbeatIndices.match(/^\S+$/) ? '' : Translations.BLANK_STR;

const expError = isValidCertVal(certExpirationThreshold);
const ageError = isValidCertVal(certAgeThreshold);

return {
heartbeatIndices: heartbeatIndErr,
certificatesThresholds:
expirationThresholdError || ageThresholdError
? {
expirationThresholdError,
ageThresholdError,
}
: null,
heartbeatIndices: indError,
expirationThresholdError: expError,
ageThresholdError: ageError,
};
}
return null;
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/uptime/public/pages/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ export const settings = {
defaultMessage: 'Return to overview',
}),
};

export const BLANK_STR = i18n.translate('xpack.uptime.settings.blank.error', {
defaultMessage: 'May not be blank.',
});

export const VALID_STR = i18n.translate('xpack.uptime.settings.invalid.error', {
defaultMessage: 'Not a valid value.',
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should say "Must be greater than 0."

});