Skip to content

Commit

Permalink
lifting validation builder function
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Apr 14, 2020
1 parent 9283eff commit f8b3b03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@ import { getExportTimelineByObjectIds } from './utils/export_timelines';
import {
exportTimelinesQuerySchema,
exportTimelinesRequestBodySchema,
buildRouteValidation,
} from './schemas/export_timelines_schema';
import { ExportTimelinesQuery, ExportTimelinesRequestBody } from '../types';
import { buildRouteValidation } from '../../../utils/build_validation/route_validation';

export const exportTimelinesRoute = (router: IRouter, config: LegacyServices['config']) => {
router.post(
{
path: TIMELINE_EXPORT_URL,
validate: {
query: buildRouteValidation<ExportTimelinesQuery, ExportTimelinesQuery, unknown>(
exportTimelinesQuerySchema
),
body: buildRouteValidation<ExportTimelinesRequestBody, ExportTimelinesRequestBody, unknown>(
exportTimelinesRequestBodySchema
),
query: buildRouteValidation(exportTimelinesQuerySchema),
body: buildRouteValidation(exportTimelinesRequestBodySchema),
},
options: {
tags: ['access:siem'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';

import { Readable } from 'stream';
import {
buildSiemResponse,
createBulkErrorObject,
Expand Down Expand Up @@ -43,17 +42,11 @@ import { Timeline } from '../saved_object';
import { validate } from '../../detection_engine/routes/rules/validate';
import { FrameworkRequest } from '../../framework';
import { throwErrors } from '../../../../../../../plugins/case/common/api';
import { buildRouteValidation } from './schemas/export_timelines_schema';
import { buildRouteValidation } from '../../../utils/build_validation/route_validation';
const CHUNK_PARSED_OBJECT_SIZE = 10;

const timelineLib = new Timeline();

interface ImportTimelinesRequestBodySchema {
file: Readable & {
hapi: { filename: string };
};
}

export const importTimelinesRoute = (
router: IRouter,
config: LegacyServices['config'],
Expand All @@ -63,11 +56,7 @@ export const importTimelinesRoute = (
{
path: `${TIMELINE_IMPORT_URL}`,
validate: {
body: buildRouteValidation<
ImportTimelinesRequestBodySchema,
ImportTimelinesRequestBodySchema,
unknown
>(ImportTimelinesPayloadSchemaRt),
body: buildRouteValidation(ImportTimelinesPayloadSchemaRt),
},
options: {
tags: ['access:siem'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { fold } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import * as rt from 'io-ts';
import { failure } from 'io-ts/lib/PathReporter';

import {
RouteValidationFunction,
RouteValidationResultFactory,
} from '../../../../../../../../../src/core/server';
import { RequestValidationResult } from '../../types';

const ids = rt.array(rt.string);
export const exportTimelinesSchema = rt.type({ ids });
Expand All @@ -26,19 +17,3 @@ export const exportTimelinesQuerySchema = rt.type({
export const exportTimelinesRequestBodySchema = rt.type({
ids,
});

export const decodeOrThrow = <A, O, I>(runtimeType: rt.Type<A, O, I>) => (
inputValue: I,
validationResult: RouteValidationResultFactory
) =>
pipe(
runtimeType.decode(inputValue),
fold<rt.Errors, A, RequestValidationResult<A>>(
(errors: rt.Errors) => validationResult.badRequest(failure(errors).join('\n')),
(validatedInput: A) => validationResult.ok(validatedInput)
)
);

export const buildRouteValidation = <A, O, I>(
schema: rt.Type<A, O, I>
): RouteValidationFunction<A> => decodeOrThrow(schema);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 { fold } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import * as rt from 'io-ts';
import { failure } from 'io-ts/lib/PathReporter';
import {
RouteValidationFunction,
RouteValidationResultFactory,
} from '../../../../../../../src/core/server';
import { RequestValidationResult } from '../../lib/timeline/types';

export const buildRouteValidation = <T extends rt.Mixed, A = rt.TypeOf<T>>(
schema: T
): RouteValidationFunction<A> => (
inputValue: unknown,
validationResult: RouteValidationResultFactory
) =>
pipe(
schema.decode(inputValue),
fold<rt.Errors, A, RequestValidationResult<A>>(
(errors: rt.Errors) => validationResult.badRequest(failure(errors).join('\n')),
(validatedInput: A) => validationResult.ok(validatedInput)
)
);

0 comments on commit f8b3b03

Please sign in to comment.