Skip to content

Commit

Permalink
[Infra UI] Remove Joi validation from Metrics Explorer url validation (
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker authored May 20, 2019
1 parent f11d7f7 commit 1bfd0d4
Showing 1 changed file with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';
import { set, values } from 'lodash';
import React, { useContext, useMemo } from 'react';
import * as t from 'io-ts';
import { ThrowReporter } from 'io-ts/lib/ThrowReporter';
import { MetricsExplorerColor } from '../../../common/color_palette';
import { MetricsExplorerAggregation } from '../../../server/routes/metrics_explorer/types';
import { UrlStateContainer } from '../../utils/url_state';
import {
MetricsExplorerOptions,
Expand Down Expand Up @@ -64,39 +64,55 @@ export const WithMetricsExplorerOptionsUrlState = () => {
};

function isMetricExplorerOptions(subject: any): subject is MetricsExplorerOptions {
const schema = Joi.object({
limit: Joi.number()
.min(1)
.default(9),
groupBy: Joi.string(),
filterQuery: Joi.string().allow(''),
aggregation: Joi.string().required(),
metrics: Joi.array()
.items(
Joi.object().keys({
aggregation: Joi.string()
.valid(values(MetricsExplorerAggregation))
.required(),
field: Joi.string(),
rate: Joi.bool().default(false),
color: Joi.string().valid(values(MetricsExplorerColor)),
label: Joi.string(),
})
)
.required(),
const MetricRequired = t.type({
aggregation: t.string,
});
const validation = Joi.validate(subject, schema);
return validation.error == null;

const MetricOptional = t.partial({
field: t.string,
rate: t.boolean,
color: t.union(values(MetricsExplorerColor).map(c => t.literal(c as string))),
label: t.string,
});

const Metric = t.intersection([MetricRequired, MetricOptional]);

const OptionsRequired = t.type({
aggregation: t.string,
metrics: t.array(Metric),
});

const OptionsOptional = t.partial({
limit: t.number,
groupBy: t.string,
filterQuery: t.string,
});

const Options = t.intersection([OptionsRequired, OptionsOptional]);

const result = Options.decode(subject);

try {
ThrowReporter.report(result);
return true;
} catch (e) {
return false;
}
}

function isMetricExplorerTimeOption(subject: any): subject is MetricsExplorerTimeOptions {
const schema = Joi.object({
from: Joi.string().required(),
to: Joi.string().required(),
interval: Joi.string().required(),
const TimeRange = t.type({
from: t.string,
to: t.string,
interval: t.string,
});
const validation = Joi.validate(subject, schema);
return validation.error == null;
const result = TimeRange.decode(subject);
try {
ThrowReporter.report(result);
return true;
} catch (e) {
return false;
}
}

const mapToUrlState = (value: any): MetricsExplorerUrlState | undefined => {
Expand Down

0 comments on commit 1bfd0d4

Please sign in to comment.