Skip to content

Commit

Permalink
[ML] Use unknown instead of any for type guards. (#94090)
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra authored Mar 10, 2021
1 parent 2d60d23 commit 066e47e
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 58 deletions.
32 changes: 19 additions & 13 deletions x-pack/plugins/transform/common/api_schemas/type_guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,47 @@ const isGenericResponseSchema = <T>(arg: any): arg is T => {
);
};

export const isGetTransformsResponseSchema = (arg: any): arg is GetTransformsResponseSchema => {
export const isGetTransformsResponseSchema = (arg: unknown): arg is GetTransformsResponseSchema => {
return isGenericResponseSchema<GetTransformsResponseSchema>(arg);
};

export const isGetTransformsStatsResponseSchema = (
arg: any
arg: unknown
): arg is GetTransformsStatsResponseSchema => {
return isGenericResponseSchema<GetTransformsStatsResponseSchema>(arg);
};

export const isDeleteTransformsResponseSchema = (
arg: any
arg: unknown
): arg is DeleteTransformsResponseSchema => {
return (
isPopulatedObject(arg) &&
Object.values(arg).every((d) => ({}.hasOwnProperty.call(d, 'transformDeleted')))
);
};

export const isEsIndices = (arg: any): arg is EsIndex[] => {
export const isEsIndices = (arg: unknown): arg is EsIndex[] => {
return Array.isArray(arg);
};

export const isEsSearchResponse = (arg: any): arg is SearchResponse7 => {
export const isEsSearchResponse = (arg: unknown): arg is SearchResponse7 => {
return isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'hits');
};

export const isFieldHistogramsResponseSchema = (arg: any): arg is FieldHistogramsResponseSchema => {
export const isFieldHistogramsResponseSchema = (
arg: unknown
): arg is FieldHistogramsResponseSchema => {
return Array.isArray(arg);
};

export const isGetTransformsAuditMessagesResponseSchema = (
arg: any
arg: unknown
): arg is GetTransformsAuditMessagesResponseSchema => {
return Array.isArray(arg);
};

export const isPostTransformsPreviewResponseSchema = (
arg: any
arg: unknown
): arg is PostTransformsPreviewResponseSchema => {
return (
isPopulatedObject(arg) &&
Expand All @@ -85,12 +87,12 @@ export const isPostTransformsPreviewResponseSchema = (
};

export const isPostTransformsUpdateResponseSchema = (
arg: any
arg: unknown
): arg is PostTransformsUpdateResponseSchema => {
return isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'id') && typeof arg.id === 'string';
};

export const isPutTransformsResponseSchema = (arg: any): arg is PutTransformsResponseSchema => {
export const isPutTransformsResponseSchema = (arg: unknown): arg is PutTransformsResponseSchema => {
return (
isPopulatedObject(arg) &&
{}.hasOwnProperty.call(arg, 'transformsCreated') &&
Expand All @@ -100,13 +102,17 @@ export const isPutTransformsResponseSchema = (arg: any): arg is PutTransformsRes
);
};

const isGenericSuccessResponseSchema = (arg: any) =>
const isGenericSuccessResponseSchema = (arg: unknown) =>
isPopulatedObject(arg) && Object.values(arg).every((d) => ({}.hasOwnProperty.call(d, 'success')));

export const isStartTransformsResponseSchema = (arg: any): arg is StartTransformsResponseSchema => {
export const isStartTransformsResponseSchema = (
arg: unknown
): arg is StartTransformsResponseSchema => {
return isGenericSuccessResponseSchema(arg);
};

export const isStopTransformsResponseSchema = (arg: any): arg is StopTransformsResponseSchema => {
export const isStopTransformsResponseSchema = (
arg: unknown
): arg is StopTransformsResponseSchema => {
return isGenericSuccessResponseSchema(arg);
};
11 changes: 5 additions & 6 deletions x-pack/plugins/transform/common/types/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { EuiComboBoxOptionOption } from '@elastic/eui/src/components/combo_box/types';
import type { LatestFunctionConfig, PutTransformsRequestSchema } from '../api_schemas/transforms';
import { isPopulatedObject } from '../utils/object_utils';
import { PivotGroupByDict } from './pivot_group_by';
import { PivotAggDict } from './pivot_aggs';

Expand Down Expand Up @@ -44,14 +45,12 @@ export type TransformLatestConfig = Omit<TransformBaseConfig, 'pivot'> & {

export type TransformConfigUnion = TransformPivotConfig | TransformLatestConfig;

export function isPivotTransform(
transform: TransformBaseConfig
): transform is TransformPivotConfig {
return transform.hasOwnProperty('pivot');
export function isPivotTransform(transform: unknown): transform is TransformPivotConfig {
return isPopulatedObject(transform) && transform.hasOwnProperty('pivot');
}

export function isLatestTransform(transform: any): transform is TransformLatestConfig {
return transform.hasOwnProperty('latest');
export function isLatestTransform(transform: unknown): transform is TransformLatestConfig {
return isPopulatedObject(transform) && transform.hasOwnProperty('latest');
}

export interface LatestFunctionConfigUI {
Expand Down
12 changes: 7 additions & 5 deletions x-pack/plugins/transform/common/types/transform_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { TransformState, TRANSFORM_STATE } from '../constants';
import { isPopulatedObject } from '../utils/object_utils';
import { TransformId } from './transform';

export interface TransformStats {
Expand Down Expand Up @@ -55,11 +56,12 @@ export interface TransformStats {
state: TransformState;
}

export function isTransformStats(arg: any): arg is TransformStats {
function isTransformState(arg: unknown): arg is TransformState {
return typeof arg === 'string' && Object.values(TRANSFORM_STATE).includes(arg as TransformState);
}

export function isTransformStats(arg: unknown): arg is TransformStats {
return (
typeof arg === 'object' &&
arg !== null &&
{}.hasOwnProperty.call(arg, 'state') &&
Object.values(TRANSFORM_STATE).includes(arg.state)
isPopulatedObject(arg) && {}.hasOwnProperty.call(arg, 'state') && isTransformState(arg.state)
);
}
10 changes: 6 additions & 4 deletions x-pack/plugins/transform/common/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { isPopulatedObject } from './object_utils';

export interface ErrorResponse {
body: {
statusCode: number;
Expand All @@ -15,16 +17,16 @@ export interface ErrorResponse {
name: string;
}

export function isErrorResponse(arg: any): arg is ErrorResponse {
return arg?.body?.error !== undefined && arg?.body?.message !== undefined;
export function isErrorResponse(arg: unknown): arg is ErrorResponse {
return isPopulatedObject(arg) && isPopulatedObject(arg.body) && arg?.body?.message !== undefined;
}

export function getErrorMessage(error: any) {
export function getErrorMessage(error: unknown) {
if (isErrorResponse(error)) {
return `${error.body.error}: ${error.body.message}`;
}

if (typeof error === 'object' && typeof error.message === 'string') {
if (isPopulatedObject(error) && typeof error.message === 'string') {
return error.message;
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/utils/object_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va
return obj;
};

export const isPopulatedObject = <T = Record<string, any>>(arg: any): arg is T => {
export const isPopulatedObject = <T = Record<string, unknown>>(arg: unknown): arg is T => {
return typeof arg === 'object' && arg !== null && Object.keys(arg).length > 0;
};
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/public/app/common/aggregations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { composeValidators, patternValidator } from '../../../common/shared_impo

import { AggName } from '../../../common/types/aggregations';

export function isAggName(arg: any): arg is AggName {
export function isAggName(arg: unknown): arg is AggName {
// allow all characters except `[]>` and must not start or end with a space.
const validatorFn = composeValidators(
patternValidator(/^[^\s]/),
Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/transform/public/app/common/pivot_aggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import type { Dictionary } from '../../../common/types/common';
import type { EsFieldName } from '../../../common/types/fields';
import type { PivotAgg, PivotSupportedAggs } from '../../../common/types/pivot_aggs';
import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs';
import { isPopulatedObject } from '../../../common/utils/object_utils';

import { getAggFormConfig } from '../sections/create_transform/components/step_define/common/get_agg_form_config';
import { PivotAggsConfigFilter } from '../sections/create_transform/components/step_define/common/filter_agg/types';

export function isPivotSupportedAggs(arg: any): arg is PivotSupportedAggs {
return Object.values(PIVOT_SUPPORTED_AGGS).includes(arg);
export function isPivotSupportedAggs(arg: unknown): arg is PivotSupportedAggs {
return (
typeof arg === 'string' &&
Object.values(PIVOT_SUPPORTED_AGGS).includes(arg as PivotSupportedAggs)
);
}

export const PERCENTILES_AGG_DEFAULT_PERCENTS = [1, 5, 25, 50, 75, 95, 99];
Expand Down Expand Up @@ -160,8 +164,9 @@ export type PivotAggsConfigWithUiSupport =
| PivotAggsConfigPercentiles
| PivotAggsConfigWithExtendedForm;

export function isPivotAggsConfigWithUiSupport(arg: any): arg is PivotAggsConfigWithUiSupport {
export function isPivotAggsConfigWithUiSupport(arg: unknown): arg is PivotAggsConfigWithUiSupport {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('aggName') &&
arg.hasOwnProperty('dropDownName') &&
Expand All @@ -175,12 +180,13 @@ export function isPivotAggsConfigWithUiSupport(arg: any): arg is PivotAggsConfig
*/
type PivotAggsConfigWithExtendedForm = PivotAggsConfigFilter;

export function isPivotAggsWithExtendedForm(arg: any): arg is PivotAggsConfigWithExtendedForm {
return arg.hasOwnProperty('AggFormComponent');
export function isPivotAggsWithExtendedForm(arg: unknown): arg is PivotAggsConfigWithExtendedForm {
return isPopulatedObject(arg) && arg.hasOwnProperty('AggFormComponent');
}

export function isPivotAggsConfigPercentiles(arg: any): arg is PivotAggsConfigPercentiles {
export function isPivotAggsConfigPercentiles(arg: unknown): arg is PivotAggsConfigPercentiles {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('percents') &&
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/transform/public/app/common/pivot_group_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AggName } from '../../../common/types/aggregations';
import { Dictionary } from '../../../common/types/common';
import { EsFieldName } from '../../../common/types/fields';
import { GenericAgg } from '../../../common/types/pivot_group_by';
import { isPopulatedObject } from '../../../common/utils/object_utils';
import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/common';
import { PivotAggsConfigWithUiSupport } from './pivot_aggs';

Expand Down Expand Up @@ -81,33 +82,36 @@ export type PivotGroupByConfig =
export type PivotGroupByConfigWithUiSupportDict = Dictionary<GroupByConfigWithUiSupport>;
export type PivotGroupByConfigDict = Dictionary<PivotGroupByConfig>;

export function isGroupByDateHistogram(arg: any): arg is GroupByDateHistogram {
export function isGroupByDateHistogram(arg: unknown): arg is GroupByDateHistogram {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('calendar_interval') &&
arg.agg === PIVOT_SUPPORTED_GROUP_BY_AGGS.DATE_HISTOGRAM
);
}

export function isGroupByHistogram(arg: any): arg is GroupByHistogram {
export function isGroupByHistogram(arg: unknown): arg is GroupByHistogram {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.hasOwnProperty('interval') &&
arg.agg === PIVOT_SUPPORTED_GROUP_BY_AGGS.HISTOGRAM
);
}

export function isGroupByTerms(arg: any): arg is GroupByTerms {
export function isGroupByTerms(arg: unknown): arg is GroupByTerms {
return (
isPopulatedObject(arg) &&
arg.hasOwnProperty('agg') &&
arg.hasOwnProperty('field') &&
arg.agg === PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS
);
}

export function isPivotGroupByConfigWithUiSupport(arg: any): arg is GroupByConfigWithUiSupport {
export function isPivotGroupByConfigWithUiSupport(arg: unknown): arg is GroupByConfigWithUiSupport {
return isGroupByDateHistogram(arg) || isGroupByHistogram(arg) || isGroupByTerms(arg);
}

Expand All @@ -119,6 +123,6 @@ export function getEsAggFromGroupByConfig(groupByConfig: GroupByConfigBase): Gen
};
}

export function isPivotAggConfigWithUiSupport(arg: any): arg is PivotAggsConfigWithUiSupport {
return arg.hasOwnProperty('agg') && arg.hasOwnProperty('field');
export function isPivotAggConfigWithUiSupport(arg: unknown): arg is PivotAggsConfigWithUiSupport {
return isPopulatedObject(arg) && arg.hasOwnProperty('agg') && arg.hasOwnProperty('field');
}
16 changes: 11 additions & 5 deletions x-pack/plugins/transform/public/app/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ export function getPivotQuery(search: string | SavedSearchQuery): PivotQuery {
return search;
}

export function isSimpleQuery(arg: any): arg is SimpleQuery {
return arg.query_string !== undefined;
export function isSimpleQuery(arg: unknown): arg is SimpleQuery {
return isPopulatedObject(arg) && arg.hasOwnProperty('query_string');
}

export const matchAllQuery = { match_all: {} };
export function isMatchAllQuery(query: any): boolean {
return query.match_all !== undefined && Object.keys(query.match_all).length === 0;
export function isMatchAllQuery(query: unknown): boolean {
return (
isPopulatedObject(query) &&
query.hasOwnProperty('match_all') &&
typeof query.match_all === 'object' &&
query.match_all !== null &&
Object.keys(query.match_all).length === 0
);
}

export const defaultQuery: PivotQuery = { query_string: { query: '*' } };
Expand Down Expand Up @@ -94,7 +100,7 @@ export function getCombinedRuntimeMappings(
}
}

if (isPopulatedObject(combinedRuntimeMappings)) {
if (isPopulatedObject<StepDefineExposedState['runtimeMappings']>(combinedRuntimeMappings)) {
return combinedRuntimeMappings;
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';

import { Privileges } from '../../../../../common/types/privileges';
import { isPopulatedObject } from '../../../../../common/utils/object_utils';

export interface Capabilities {
canGetTransform: boolean;
Expand All @@ -19,10 +20,9 @@ export interface Capabilities {

export type Privilege = [string, string];

function isPrivileges(arg: any): arg is Privileges {
function isPrivileges(arg: unknown): arg is Privileges {
return (
typeof arg === 'object' &&
arg !== null &&
isPopulatedObject(arg) &&
arg.hasOwnProperty('hasAllPrivileges') &&
typeof arg.hasAllPrivileges === 'boolean' &&
arg.hasOwnProperty('missingPrivileges') &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface StepDefineExposedState {
isRuntimeMappingsEditorEnabled: boolean;
}

export function isRuntimeField(arg: any): arg is RuntimeField {
export function isRuntimeField(arg: unknown): arg is RuntimeField {
return (
isPopulatedObject(arg) &&
((Object.keys(arg).length === 1 && arg.hasOwnProperty('type')) ||
Expand All @@ -84,18 +84,18 @@ export function isRuntimeField(arg: any): arg is RuntimeField {
Object.keys(arg.script).length === 1 &&
arg.script.hasOwnProperty('source') &&
typeof arg.script.source === 'string')))) &&
RUNTIME_FIELD_TYPES.includes(arg.type)
RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType)
);
}

export function isRuntimeMappings(arg: any): arg is RuntimeMappings {
export function isRuntimeMappings(arg: unknown): arg is RuntimeMappings {
return isPopulatedObject(arg) && Object.values(arg).every((d) => isRuntimeField(d));
}

export function isPivotPartialRequest(arg: any): arg is { pivot: PivotConfigDefinition } {
export function isPivotPartialRequest(arg: unknown): arg is { pivot: PivotConfigDefinition } {
return isPopulatedObject(arg) && arg.hasOwnProperty('pivot');
}

export function isLatestPartialRequest(arg: any): arg is { latest: LatestFunctionConfig } {
export function isLatestPartialRequest(arg: unknown): arg is { latest: LatestFunctionConfig } {
return isPopulatedObject(arg) && arg.hasOwnProperty('latest');
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const stringValidator: Validator = (value, isOptional = true) => {
return [];
};

function parseDurationAboveZero(arg: any, errorMessage: string): ParsedDuration | string[] {
function parseDurationAboveZero(arg: unknown, errorMessage: string): ParsedDuration | string[] {
if (typeof arg !== 'string' || arg === null) {
return [stringNotValidErrorMessage];
}
Expand Down
Loading

0 comments on commit 066e47e

Please sign in to comment.