Skip to content

Commit

Permalink
validte API req
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed May 7, 2020
1 parent 0cb3e58 commit 1d96e26
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/uptime/common/translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';

export const VALUE_MUST_BE_GREATER_THEN_ZEO = i18n.translate(
'xpack.uptime.settings.invalid.error',
{
defaultMessage: 'Value must be greater than 0.',
}
);
3 changes: 2 additions & 1 deletion x-pack/plugins/uptime/public/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
OnFieldChangeType,
} from '../components/settings/certificate_form';
import * as Translations from './translations';
import { VALUE_MUST_BE_GREATER_THEN_ZEO } from '../../common/translations';

interface SettingsPageFieldErrors {
heartbeatIndices: string | '';
Expand All @@ -52,7 +53,7 @@ const isValidCertVal = (val: string | number) => {
return Translations.BLANK_STR;
}
if (val === 0) {
return Translations.VALID_STR;
return VALUE_MUST_BE_GREATER_THEN_ZEO;
}
};

Expand Down
4 changes: 0 additions & 4 deletions x-pack/plugins/uptime/public/pages/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,3 @@ export const settings = {
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.',
});
29 changes: 23 additions & 6 deletions x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UMServerLibs } from '../lib/lib';
import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types';
import { UMRestApiRouteFactory } from '.';
import { savedObjectsAdapter } from '../lib/saved_objects';
import { VALUE_MUST_BE_GREATER_THEN_ZEO } from '../../common/translations';

export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) => ({
method: 'GET',
Expand All @@ -23,19 +24,35 @@ export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSer
},
});

const validateCertsValues = (settings: DynamicSettings) => {
const errors: any = {};
if (settings.certAgeThreshold <= 0) {
errors.certAgeThreshold = VALUE_MUST_BE_GREATER_THEN_ZEO;
}
if (settings.certExpirationThreshold <= 0) {
errors.certExpirationThreshold = VALUE_MUST_BE_GREATER_THEN_ZEO;
}
if (errors.certAgeThreshold || errors.certExpirationThreshold) {
return errors;
}
};

export const createPostDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) => ({
method: 'POST',
path: '/api/uptime/dynamic_settings',
validate: {
body: schema.object({}, { unknowns: 'allow' }),
body: schema.object({
heartbeatIndices: schema.string(),
certAgeThreshold: schema.number(),
certExpirationThreshold: schema.number(),
}),
},
writeAccess: true,
options: {
tags: ['access:uptime-write'],
},
handler: async ({ savedObjectsClient }, _context, request, response): Promise<any> => {
const decoded = DynamicSettingsType.decode(request.body);
if (isRight(decoded)) {
const certThresholdErrors = validateCertsValues(request.body as DynamicSettings);

if (isRight(decoded) && !certThresholdErrors) {
const newSettings: DynamicSettings = decoded.right;
await savedObjectsAdapter.setUptimeDynamicSettings(savedObjectsClient, newSettings);

Expand All @@ -47,7 +64,7 @@ export const createPostDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSe
} else {
const error = PathReporter.report(decoded).join(', ');
return response.badRequest({
body: error,
body: JSON.stringify(certThresholdErrors) || error,
});
}
},
Expand Down

0 comments on commit 1d96e26

Please sign in to comment.