Skip to content

Commit

Permalink
removed api io-ts validation, can be discussed separatly
Browse files Browse the repository at this point in the history
  • Loading branch information
yctercero committed Sep 3, 2020
1 parent dcfb598 commit f713a28
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 150 deletions.
2 changes: 0 additions & 2 deletions x-pack/plugins/lists/common/shared_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ export {
} from './schemas';

export { ENDPOINT_LIST_ID } from './constants';

export { toPromise, toError } from './fp_utils';
File renamed without changes.
2 changes: 1 addition & 1 deletion x-pack/plugins/lists/public/lists/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../../common/schemas';
import { LIST_INDEX, LIST_ITEM_URL, LIST_PRIVILEGES_URL, LIST_URL } from '../../common/constants';
import { validateEither } from '../../common/shared_imports';
import { toError, toPromise } from '../../common/fp_utils';
import { toError, toPromise } from '../common/fp_utils';

import {
ApiParams,
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/lists/public/shared_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ export {
Pagination,
UseExceptionListSuccess,
} from './exceptions/types';
export { toPromise, toError } from '../common/fp_utils';
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import { getUpdateRulesSchemaMock } from '../../../../../common/detection_engine
import { getPatchRulesSchemaMock } from '../../../../../common/detection_engine/schemas/request/patch_rules_schema.mock';
import { rulesMock } from './mock';
import { buildEsQuery } from 'src/plugins/data/common';
import {
UpdateRulesSchema,
CreateRulesSchema,
PatchRulesSchema,
} from '../../../../../common/detection_engine/schemas/request';
import { CreateRulesSchema } from '../../../../../common/detection_engine/schemas/request';
const abortCtrl = new AbortController();
const mockKibanaServices = KibanaServices.get as jest.Mock;
jest.mock('../../../../common/lib/kibana');
Expand All @@ -56,41 +52,6 @@ describe('Detections Rules API', () => {
signal: abortCtrl.signal,
});
});

it('rejects with an error if request payload is invalid (and does not make API call)', async () => {
const payload: Partial<CreateRulesSchema> = {
rule_id: 'rule-1',
description: 'some description',
from: 'now-5m',
to: 'now',
name: 'some-name',
severity: 'low',
type: 'query',
interval: '5m',
index: ['index-1'],
};

await expect(
createRule({
rule: (payload as unknown) as CreateRulesSchema,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "risk_score"'));
expect(fetchMock).not.toHaveBeenCalled();
});

it('rejects with an error if response payload is invalid', async () => {
const payload: CreateRulesSchema = getCreateRulesSchemaMock();
const badResponse = { ...getRulesSchemaMock(), id: undefined };
fetchMock.mockResolvedValue(badResponse);

await expect(
createRule({
rule: payload,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"'));
});
});

describe('updateRule', () => {
Expand All @@ -109,33 +70,6 @@ describe('Detections Rules API', () => {
signal: abortCtrl.signal,
});
});

it('rejects with an error if request payload is invalid (and does not make API call)', async () => {
const payload: Omit<UpdateRulesSchema, 'note'> & {
note: number;
} = { ...getUpdateRulesSchemaMock(), note: 23 };

await expect(
updateRule({
rule: (payload as unknown) as UpdateRulesSchema,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "23" supplied to "note"'));
expect(fetchMock).not.toHaveBeenCalled();
});

it('rejects with an error if response payload is invalid', async () => {
const payload: UpdateRulesSchema = getUpdateRulesSchemaMock();
const badResponse = { ...getRulesSchemaMock(), id: undefined };
fetchMock.mockResolvedValue(badResponse);

await expect(
updateRule({
rule: payload,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"'));
});
});

describe('patchRule', () => {
Expand All @@ -153,33 +87,6 @@ describe('Detections Rules API', () => {
signal: abortCtrl.signal,
});
});

it('rejects with an error if request payload is invalid (and does not make API call)', async () => {
const payload: Omit<PatchRulesSchema, 'note'> & {
note: number;
} = { ...getPatchRulesSchemaMock(), note: 3 };

await expect(
patchRule({
ruleProperties: (payload as unknown) as PatchRulesSchema,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "3" supplied to "note"'));
expect(fetchMock).not.toHaveBeenCalled();
});

it('rejects with an error if response payload is invalid', async () => {
const payload: PatchRulesSchema = getPatchRulesSchemaMock();
const badResponse = { ...getRulesSchemaMock(), id: undefined };
fetchMock.mockResolvedValue(badResponse);

await expect(
patchRule({
ruleProperties: payload,
signal: abortCtrl.signal,
})
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"'));
});
});

describe('fetchRules', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { chain, fromEither, tryCatch } from 'fp-ts/lib/TaskEither';
import { flow } from 'fp-ts/lib/function';
import { pipe } from 'fp-ts/lib/pipeable';

import { HttpStart } from '../../../../../../../../src/core/public';
import {
DETECTION_ENGINE_RULES_URL,
Expand Down Expand Up @@ -36,13 +32,8 @@ import {
} from './types';
import { KibanaServices } from '../../../../common/lib/kibana';
import * as i18n from '../../../pages/detection_engine/rules/translations';
import { RulesSchema, rulesSchema } from '../../../../../common/detection_engine/schemas/response';
import { validateEither } from '../../../../../common';
import {
createRulesSchema,
updateRulesSchema,
patchRulesSchema,
} from '../../../../../common/detection_engine/schemas/request';
import { RulesSchema } from '../../../../../common/detection_engine/schemas/response';
import { updateRulesSchema } from '../../../../../common/detection_engine/schemas/request';
import { toError, toPromise } from '../../../../../common/shared_imports';

/**
Expand All @@ -53,24 +44,13 @@ import { toError, toPromise } from '../../../../../common/shared_imports';
*
* @throws An error if response is not OK
*/
const createRule = async ({ rule, signal }: CreateRulesProps): Promise<RulesSchema> =>
export const createRule = async ({ rule, signal }: CreateRulesProps): Promise<RulesSchema> =>
KibanaServices.get().http.fetch<RulesSchema>(DETECTION_ENGINE_RULES_URL, {
method: 'POST',
body: JSON.stringify(rule),
signal,
});

const createRuleWithValidation = async ({ rule, signal }: CreateRulesProps): Promise<RulesSchema> =>
pipe(
rule,
(body) => fromEither(validateEither(createRulesSchema, body)),
chain((payload) => tryCatch(() => createRule({ signal, rule: { ...payload } }), toError)),
chain((response) => fromEither(validateEither(rulesSchema, response))),
flow(toPromise)
);

export { createRuleWithValidation as createRule };

/**
* Update provided Rule using PUT
*
Expand All @@ -79,24 +59,13 @@ export { createRuleWithValidation as createRule };
*
* @throws An error if response is not OK
*/
const updateRule = async ({ rule, signal }: UpdateRulesProps): Promise<RulesSchema> =>
export const updateRule = async ({ rule, signal }: UpdateRulesProps): Promise<RulesSchema> =>
KibanaServices.get().http.fetch<RulesSchema>(DETECTION_ENGINE_RULES_URL, {
method: 'PUT',
body: JSON.stringify(rule),
signal,
});

const updateRuleWithValidation = async ({ rule, signal }: UpdateRulesProps): Promise<RulesSchema> =>
pipe(
rule,
(body) => fromEither(validateEither(updateRulesSchema, body)),
chain((payload) => tryCatch(() => updateRule({ signal, rule: { ...payload } }), toError)),
chain((response) => fromEither(validateEither(rulesSchema, response))),
flow(toPromise)
);

export { updateRuleWithValidation as updateRule };

/**
* Patch provided rule
* NOTE: The rule edit flow does NOT use patch as it relies on the
Expand All @@ -108,29 +77,13 @@ export { updateRuleWithValidation as updateRule };
*
* @throws An error if response is not OK
*/
const patchRule = async ({ ruleProperties, signal }: PatchRuleProps): Promise<RulesSchema> =>
export const patchRule = async ({ ruleProperties, signal }: PatchRuleProps): Promise<RulesSchema> =>
KibanaServices.get().http.fetch<RulesSchema>(DETECTION_ENGINE_RULES_URL, {
method: 'PATCH',
body: JSON.stringify(ruleProperties),
signal,
});

const patchRuleWithValidation = async ({
ruleProperties,
signal,
}: PatchRuleProps): Promise<RulesSchema> =>
pipe(
ruleProperties,
(body) => fromEither(validateEither(patchRulesSchema, body)),
chain((payload) =>
tryCatch(() => patchRule({ signal, ruleProperties: { ...payload } }), toError)
),
chain((response) => fromEither(validateEither(rulesSchema, response))),
flow(toPromise)
);

export { patchRuleWithValidation as patchRule };

/**
* Fetches all rules from the Detection Engine API
*
Expand Down

0 comments on commit f713a28

Please sign in to comment.