Skip to content

Commit

Permalink
Add response runtime type check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry350 committed Jun 12, 2020
1 parent 971e4bd commit c9e56a2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/infra/common/alerting/logs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const LogDocumentCountAlertParamsRT = rt.intersection([

export type LogDocumentCountAlertParams = rt.TypeOf<typeof LogDocumentCountAlertParamsRT>;

const UngroupedSearchQueryResponseRT = rt.intersection([
export const UngroupedSearchQueryResponseRT = rt.intersection([
commonSearchSuccessResponseFieldsRT,
rt.type({
hits: rt.type({
Expand All @@ -134,7 +134,7 @@ const UngroupedSearchQueryResponseRT = rt.intersection([

export type UngroupedSearchQueryResponse = rt.TypeOf<typeof UngroupedSearchQueryResponseRT>;

const GroupedSearchQueryResponseRT = rt.intersection([
export const GroupedSearchQueryResponseRT = rt.intersection([
commonSearchSuccessResponseFieldsRT,
rt.type({
aggregations: rt.type({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
Comparator,
LogDocumentCountAlertParams,
Criterion,
GroupedSearchQueryResponseRT,
UngroupedSearchQueryResponseRT,
UngroupedSearchQueryResponse,
GroupedSearchQueryResponse,
LogDocumentCountAlertParamsRT,
Expand Down Expand Up @@ -324,7 +326,7 @@ const getUngroupedResults = async (
index: string,
callCluster: AlertServices['callCluster']
) => {
return await callCluster('search', query);
return decodeOrThrow(UngroupedSearchQueryResponseRT)(await callCluster('search', query));
};

const getGroupedResults = async (
Expand All @@ -338,9 +340,8 @@ const getGroupedResults = async (
while (true) {
const queryWithAfterKey = { ...query };
queryWithAfterKey.body.aggregations.groups.composite.after = lastAfterKey;
const groupResponse: GroupedSearchQueryResponse = await callCluster(
'search',
queryWithAfterKey
const groupResponse: GroupedSearchQueryResponse = decodeOrThrow(GroupedSearchQueryResponseRT)(
await callCluster('search', queryWithAfterKey)
);
compositeGroupBuckets = [
...compositeGroupBuckets,
Expand Down

0 comments on commit c9e56a2

Please sign in to comment.