[Security Solutions] Replaces most deprecated io-ts alerting and list types (#100234) (#100246)

## Summary

Replaces most of the deprecated io-ts alerting and list types within securitysolution as part of Phase 3 of 4 phases outlined in earlier PR's such as https://github.com/elastic/kibana/pull/99260

### Checklist

- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios

Co-authored-by: Frank Hassanabad <frank.hassanabad@elastic.co>
This commit is contained in:
Kibana Machine 2021-05-18 03:23:28 -04:00 committed by GitHub
parent c0f46471c1
commit 18e6085370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
323 changed files with 788 additions and 5647 deletions

View file

@ -1,11 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ElasticsearchClient } from 'src/core/server';
import { ElasticsearchClient } from '../elasticsearch_client';
interface AliasesResponse {
[indexName: string]: {

View file

@ -1,11 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ElasticsearchClient } from 'src/core/server';
import { ElasticsearchClient } from '../elasticsearch_client';
/**
* Retrieves the count of documents in a given index

View file

@ -12,9 +12,12 @@ export * from './delete_all_index';
export * from './delete_policy';
export * from './delete_template';
export * from './elasticsearch_client';
export * from './get_index_aliases';
export * from './get_index_count';
export * from './get_index_exists';
export * from './get_policy_exists';
export * from './get_template_exists';
export * from './read_index';
export * from './read_privileges';
export * from './set_policy';
export * from './set_template';

View file

@ -1,11 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ElasticsearchClient } from 'kibana/server';
import { ElasticsearchClient } from '../elasticsearch_client';
export const readIndex = async (esClient: ElasticsearchClient, index: string): Promise<unknown> => {
return esClient.indices.get({

View file

@ -24,3 +24,6 @@ export const from = new t.Type<string, string, unknown>(
t.identity
);
export type From = t.TypeOf<typeof from>;
export const fromOrUndefined = t.union([from, t.undefined]);
export type FromOrUndefined = t.TypeOf<typeof fromOrUndefined>;

View file

@ -24,6 +24,7 @@ export * from './default_to_string';
export * from './default_uuid';
export * from './from';
export * from './language';
export * from './machine_learning_job_id';
export * from './max_signals';
export * from './normalized_ml_job_id';
export * from './references_default_array';
@ -38,3 +39,4 @@ export * from './threat_subtechnique';
export * from './threat_tactic';
export * from './threat_technique';
export * from './throttle';
export * from './type';

View file

@ -10,3 +10,6 @@ import * as t from 'io-ts';
export const language = t.keyof({ eql: null, kuery: null, lucene: null });
export type Language = t.TypeOf<typeof language>;
export const languageOrUndefined = t.union([language, t.undefined]);
export type LanguageOrUndefined = t.TypeOf<typeof languageOrUndefined>;

View file

@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
/* eslint-disable @typescript-eslint/naming-convention */
import * as t from 'io-ts';
import { machine_learning_job_id_normalized } from '../normalized_ml_job_id';
export const machine_learning_job_id = t.union([t.string, machine_learning_job_id_normalized]);
export type MachineLearningJobId = t.TypeOf<typeof machine_learning_job_id>;
export const machineLearningJobIdOrUndefined = t.union([machine_learning_job_id, t.undefined]);
export type MachineLearningJobIdOrUndefined = t.TypeOf<typeof machineLearningJobIdOrUndefined>;

View file

@ -13,3 +13,6 @@ import { PositiveIntegerGreaterThanZero } from '@kbn/securitysolution-io-ts-type
export const max_signals = PositiveIntegerGreaterThanZero;
export type MaxSignals = t.TypeOf<typeof max_signals>;
export const maxSignalsOrUndefined = t.union([max_signals, t.undefined]);
export type MaxSignalsOrUndefined = t.TypeOf<typeof maxSignalsOrUndefined>;

View file

@ -6,6 +6,8 @@
* Side Public License, v 1.
*/
/* eslint-disable @typescript-eslint/naming-convention */
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
@ -26,3 +28,9 @@ export const RiskScore = new t.Type<number, number, unknown>(
);
export type RiskScoreC = typeof RiskScore;
export const risk_score = RiskScore;
export type RiskScore = t.TypeOf<typeof risk_score>;
export const riskScoreOrUndefined = t.union([risk_score, t.undefined]);
export type RiskScoreOrUndefined = t.TypeOf<typeof riskScoreOrUndefined>;

View file

@ -10,10 +10,7 @@
import * as t from 'io-ts';
import { operator } from '@kbn/securitysolution-io-ts-types';
import { RiskScore } from '../risk_score';
export const riskScoreOrUndefined = t.union([RiskScore, t.undefined]);
export type RiskScoreOrUndefined = t.TypeOf<typeof riskScoreOrUndefined>;
import { riskScoreOrUndefined } from '../risk_score';
export const risk_score_mapping_field = t.string;
export const risk_score_mapping_value = t.string;
@ -28,3 +25,6 @@ export const risk_score_mapping_item = t.exact(
export const risk_score_mapping = t.array(risk_score_mapping_item);
export type RiskScoreMapping = t.TypeOf<typeof risk_score_mapping>;
export const riskScoreMappingOrUndefined = t.union([risk_score_mapping, t.undefined]);
export type RiskScoreMappingOrUndefined = t.TypeOf<typeof riskScoreMappingOrUndefined>;

View file

@ -32,3 +32,6 @@ export type Threat = t.TypeOf<typeof threat>;
export const threats = t.array(threat);
export type Threats = t.TypeOf<typeof threats>;
export const threatsOrUndefined = t.union([threats, t.undefined]);
export type ThreatsOrUndefined = t.TypeOf<typeof threatsOrUndefined>;

View file

@ -21,3 +21,5 @@ export const threat_subtechnique = t.type({
});
export const threat_subtechniques = t.array(threat_subtechnique);
export type ThreatSubtechnique = t.TypeOf<typeof threat_subtechnique>;

View file

@ -30,3 +30,5 @@ export const threat_technique = t.intersection([
),
]);
export const threat_techniques = t.array(threat_technique);
export type ThreatTechnique = t.TypeOf<typeof threat_technique>;

View file

@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import * as t from 'io-ts';
export const type = t.keyof({
eql: null,
machine_learning: null,
query: null,
saved_query: null,
threshold: null,
threat_match: null,
});
export type Type = t.TypeOf<typeof type>;
export const typeOrUndefined = t.union([type, t.undefined]);
export type TypeOrUndefined = t.TypeOf<typeof typeOrUndefined>;

View file

@ -16,7 +16,6 @@ export * from './default_create_comments_array';
export * from './default_namespace';
export * from './default_namespace_array';
export * from './default_update_comments_array';
export * from './default_version_number';
export * from './description';
export * from './endpoint';
export * from './entries';
@ -43,4 +42,3 @@ export * from './type';
export * from './update_comment';
export * from './updated_at';
export * from './updated_by';
export * from './version';

View file

@ -40,3 +40,4 @@ export const type = t.keyof({
export const typeOrUndefined = t.union([type, t.undefined]);
export type Type = t.TypeOf<typeof type>;
export type TypeOrUndefined = t.TypeOf<typeof typeOrUndefined>;

View file

@ -13,6 +13,7 @@ export * from './default_empty_string';
export * from './default_string_array';
export * from './default_string_boolean_false';
export * from './default_uuid';
export * from './default_version_number';
export * from './empty_string_array';
export * from './iso_date_string';
export * from './non_empty_array';
@ -26,3 +27,4 @@ export * from './positive_integer';
export * from './positive_integer_greater_than_zero';
export * from './string_to_positive_number';
export * from './uuid';
export * from './version';

View file

@ -7,7 +7,7 @@
*/
import * as t from 'io-ts';
import { PositiveIntegerGreaterThanZero } from '@kbn/securitysolution-io-ts-types';
import { PositiveIntegerGreaterThanZero } from '../positive_integer_greater_than_zero';
/**
* Note this is just a positive number, but we use it as a type here which is still ok.
@ -16,3 +16,6 @@ import { PositiveIntegerGreaterThanZero } from '@kbn/securitysolution-io-ts-type
*/
export const version = PositiveIntegerGreaterThanZero;
export type Version = t.TypeOf<typeof version>;
export const versionOrUndefined = t.union([version, t.undefined]);
export type VersionOrUndefined = t.TypeOf<typeof versionOrUndefined>;

View file

@ -17,10 +17,10 @@ import {
entriesNested,
} from '@kbn/securitysolution-io-ts-list-types';
import { Filter } from '../../../../../src/plugins/data/common';
import { CreateExceptionListItemSchema, ExceptionListItemSchema } from '../schemas';
import type { Filter } from '../../../../../src/plugins/data/common';
import type { CreateExceptionListItemSchema, ExceptionListItemSchema } from '../schemas';
import { BooleanFilter, NestedFilter } from './types';
import type { BooleanFilter, NestedFilter } from './types';
import { hasLargeValueList } from './utils';
type NonListEntry = EntryMatch | EntryMatchAny | EntryNested | EntryExists;

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { EntriesArray } from '@kbn/securitysolution-io-ts-list-types';
import type { EntriesArray } from '@kbn/securitysolution-io-ts-list-types';
export const hasLargeValueList = (entries: EntriesArray): boolean => {
const found = entries.filter(({ type }) => type === 'list');

View file

@ -11,18 +11,6 @@ import * as t from 'io-ts';
import { NonEmptyString } from '@kbn/securitysolution-io-ts-types';
import { DefaultNamespace } from '@kbn/securitysolution-io-ts-list-types';
/**
* @deprecated Directly use the type from the package and not from here
*/
export {
DefaultNamespace,
Type,
OsType,
OsTypeArray,
listOperator as operator,
NonEmptyEntriesArray,
} from '@kbn/securitysolution-io-ts-list-types';
export const list_id = NonEmptyString;
export type ListId = t.TypeOf<typeof list_id>;
export const list_idOrUndefined = t.union([list_id, t.undefined]);
@ -91,12 +79,6 @@ export const _version = t.string;
export const _versionOrUndefined = t.union([_version, t.undefined]);
export type _VersionOrUndefined = t.TypeOf<typeof _versionOrUndefined>;
export const version = t.number;
export type Version = t.TypeOf<typeof version>;
export const versionOrUndefined = t.union([version, t.undefined]);
export type VersionOrUndefined = t.TypeOf<typeof versionOrUndefined>;
export const immutable = t.boolean;
export type Immutable = t.TypeOf<typeof immutable>;

View file

@ -7,8 +7,6 @@
import * as t from 'io-ts';
import {
DefaultVersionNumber,
DefaultVersionNumberDecoded,
NamespaceType,
OsTypeArray,
Tags,
@ -19,7 +17,11 @@ import {
osTypeArrayOrUndefined,
tags,
} from '@kbn/securitysolution-io-ts-list-types';
import { DefaultUuid } from '@kbn/securitysolution-io-ts-types';
import {
DefaultUuid,
DefaultVersionNumber,
DefaultVersionNumberDecoded,
} from '@kbn/securitysolution-io-ts-types';
import { ListId, namespace_type } from '../common/schemas';
import { RequiredKeepUndefined } from '../../types';

View file

@ -9,12 +9,8 @@ import * as t from 'io-ts';
import {
DefaultVersionNumber,
DefaultVersionNumberDecoded,
description,
id,
meta,
name,
type,
} from '@kbn/securitysolution-io-ts-list-types';
} from '@kbn/securitysolution-io-ts-types';
import { description, id, meta, name, type } from '@kbn/securitysolution-io-ts-list-types';
import { deserializer, serializer } from '../common/schemas';
import { RequiredKeepUndefined } from '../../types';

View file

@ -7,8 +7,9 @@
import * as t from 'io-ts';
import { description, id, meta, name } from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import { _version, version } from '../common/schemas';
import { _version } from '../common/schemas';
import { RequiredKeepUndefined } from '../../types';
export const patchListSchema = t.intersection([

View file

@ -18,8 +18,9 @@ import {
osTypeArrayOrUndefined,
tags,
} from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import { _version, list_id, namespace_type, version } from '../common/schemas';
import { _version, list_id, namespace_type } from '../common/schemas';
import { RequiredKeepUndefined } from '../../types';
export const updateExceptionListSchema = t.intersection([

View file

@ -6,7 +6,8 @@
*/
import * as t from 'io-ts';
import { description, id, meta, name, version } from '@kbn/securitysolution-io-ts-list-types';
import { description, id, meta, name } from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import { _version } from '../common/schemas';
import { RequiredKeepUndefined } from '../../types';

View file

@ -19,6 +19,7 @@ import {
updated_at,
updated_by,
} from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import {
_versionOrUndefined,
@ -26,7 +27,6 @@ import {
list_id,
namespace_type,
tie_breaker_id,
version,
} from '../common/schemas';
export const exceptionListSchema = t.exact(

View file

@ -17,6 +17,7 @@ import {
updated_at,
updated_by,
} from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import {
_versionOrUndefined,
@ -24,7 +25,6 @@ import {
immutable,
serializerOrUndefined,
tie_breaker_id,
version,
} from '../common/schemas';
export const listSchema = t.exact(

View file

@ -5,16 +5,13 @@
* 2.0.
*/
export {
export type {
ListSchema,
ExceptionListSchema,
ExceptionListItemSchema,
CreateExceptionListSchema,
CreateExceptionListItemSchema,
UpdateExceptionListItemSchema,
exceptionListItemSchema,
createExceptionListItemSchema,
listSchema,
} from './schemas';
export { buildExceptionFilter } from './exceptions';

View file

@ -23,7 +23,7 @@ import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
exceptionListItemSchema,
} from '../../../../common';
} from '../../../../common/schemas';
import { AutocompleteStart, IIndexPattern } from '../../../../../../../src/plugins/data/public';
import { AndOrBadge } from '../and_or_badge';

View file

@ -29,7 +29,7 @@ import {
ListSchema,
createExceptionListItemSchema,
exceptionListItemSchema,
} from '../../../../common';
} from '../../../../common/schemas';
import { IFieldType, IIndexPattern } from '../../../../../../../src/plugins/data/public';
import {
EXCEPTION_OPERATORS,

View file

@ -7,11 +7,11 @@
export * from './shared_exports';
import { PluginInitializerContext } from '../../../../src/core/public';
import type { PluginInitializerContext } from '../../../../src/core/public';
import { Plugin } from './plugin';
import { PluginSetup, PluginStart } from './types';
import type { PluginSetup, PluginStart } from './types';
export const plugin = (context: PluginInitializerContext): Plugin => new Plugin(context);
export { Plugin, PluginSetup, PluginStart };
export type { Plugin, PluginSetup, PluginStart };

View file

@ -16,13 +16,13 @@ import {
updated_at,
updated_by,
} from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import {
deserializerOrUndefined,
immutable,
serializerOrUndefined,
tie_breaker_id,
version,
} from '../../../common/schemas';
export const indexEsListSchema = t.exact(

View file

@ -16,13 +16,13 @@ import {
updated_at,
updated_by,
} from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import {
deserializerOrUndefined,
immutable,
serializerOrUndefined,
tie_breaker_id,
version,
} from '../../../common/schemas';
export const searchEsListSchema = t.exact(

View file

@ -20,6 +20,7 @@ import {
tags,
updated_by,
} from '@kbn/securitysolution-io-ts-list-types';
import { versionOrUndefined } from '@kbn/securitysolution-io-ts-types';
import {
immutableOrUndefined,
@ -27,7 +28,6 @@ import {
list_id,
list_type,
tie_breaker_id,
versionOrUndefined,
} from '../../../common/schemas';
/**

View file

@ -7,13 +7,14 @@
import { SavedObjectsClientContract } from 'kibana/server';
import uuid from 'uuid';
import { Version } from '@kbn/securitysolution-io-ts-types';
import {
ENDPOINT_EVENT_FILTERS_LIST_DESCRIPTION,
ENDPOINT_EVENT_FILTERS_LIST_ID,
ENDPOINT_EVENT_FILTERS_LIST_NAME,
} from '../../../common/constants';
import { ExceptionListSchema, Version } from '../../../common/schemas';
import { ExceptionListSchema } from '../../../common/schemas';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';
import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils';

View file

@ -7,13 +7,14 @@
import { SavedObjectsClientContract } from 'kibana/server';
import uuid from 'uuid';
import { Version } from '@kbn/securitysolution-io-ts-types';
import {
ENDPOINT_LIST_DESCRIPTION,
ENDPOINT_LIST_ID,
ENDPOINT_LIST_NAME,
} from '../../../common/constants';
import { ExceptionListSchema, Version } from '../../../common/schemas';
import { ExceptionListSchema } from '../../../common/schemas';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';
import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils';

View file

@ -7,13 +7,14 @@
import { SavedObjectsClientContract } from 'kibana/server';
import uuid from 'uuid';
import { Version } from '@kbn/securitysolution-io-ts-types';
import {
ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION,
ENDPOINT_TRUSTED_APPS_LIST_ID,
ENDPOINT_TRUSTED_APPS_LIST_NAME,
} from '../../../common/constants';
import { ExceptionListSchema, Version } from '../../../common/schemas';
import { ExceptionListSchema } from '../../../common/schemas';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';
import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils';

View file

@ -15,8 +15,9 @@ import {
NamespaceType,
Tags,
} from '@kbn/securitysolution-io-ts-list-types';
import { Version } from '@kbn/securitysolution-io-ts-types';
import { ExceptionListSchema, Immutable, ListId, Version } from '../../../common/schemas';
import { ExceptionListSchema, Immutable, ListId } from '../../../common/schemas';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';
import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils';

View file

@ -30,6 +30,8 @@ import {
import {
EmptyStringArrayDecoded,
NonEmptyStringArrayDecoded,
Version,
VersionOrUndefined,
} from '@kbn/securitysolution-io-ts-types';
import {
@ -43,8 +45,6 @@ import {
PerPageOrUndefined,
SortFieldOrUndefined,
SortOrderOrUndefined,
Version,
VersionOrUndefined,
_VersionOrUndefined,
} from '../../../common/schemas';

View file

@ -16,11 +16,11 @@ import {
OsTypeArray,
TagsOrUndefined,
} from '@kbn/securitysolution-io-ts-list-types';
import { VersionOrUndefined } from '@kbn/securitysolution-io-ts-types';
import {
ExceptionListSchema,
ListIdOrUndefined,
VersionOrUndefined,
_VersionOrUndefined,
} from '../../../common/schemas';
import { ExceptionListSoSchema } from '../../schemas/saved_objects';

View file

@ -9,6 +9,7 @@ import { Readable } from 'stream';
import { ElasticsearchClient } from 'kibana/server';
import { MetaOrUndefined, Type } from '@kbn/securitysolution-io-ts-list-types';
import { Version } from '@kbn/securitysolution-io-ts-types';
import { createListIfItDoesNotExist } from '../lists/create_list_if_it_does_not_exist';
import {
@ -16,7 +17,6 @@ import {
ListIdOrUndefined,
ListSchema,
SerializerOrUndefined,
Version,
} from '../../../common/schemas';
import { ConfigType } from '../../config';

View file

@ -14,6 +14,7 @@ import {
Name,
Type,
} from '@kbn/securitysolution-io-ts-list-types';
import { Version } from '@kbn/securitysolution-io-ts-types';
import { encodeHitVersion } from '../utils/encode_hit_version';
import {
@ -21,7 +22,6 @@ import {
Immutable,
ListSchema,
SerializerOrUndefined,
Version,
} from '../../../common/schemas';
import { IndexEsListSchema } from '../../schemas/elastic_query';

View file

@ -13,13 +13,13 @@ import {
Name,
Type,
} from '@kbn/securitysolution-io-ts-list-types';
import { Version } from '@kbn/securitysolution-io-ts-types';
import {
DeserializerOrUndefined,
Immutable,
ListSchema,
SerializerOrUndefined,
Version,
} from '../../../common/schemas';
import { getList } from './get_list';

View file

@ -18,6 +18,7 @@ import {
NameOrUndefined,
Type,
} from '@kbn/securitysolution-io-ts-list-types';
import { Version, VersionOrUndefined } from '@kbn/securitysolution-io-ts-types';
import {
DeserializerOrUndefined,
@ -30,8 +31,6 @@ import {
SerializerOrUndefined,
SortFieldOrUndefined,
SortOrderOrUndefined,
Version,
VersionOrUndefined,
_VersionOrUndefined,
} from '../../../common/schemas';
import { ConfigType } from '../../config';

View file

@ -12,10 +12,11 @@ import {
MetaOrUndefined,
NameOrUndefined,
} from '@kbn/securitysolution-io-ts-list-types';
import { VersionOrUndefined } from '@kbn/securitysolution-io-ts-types';
import { decodeVersion } from '../utils/decode_version';
import { encodeHitVersion } from '../utils/encode_hit_version';
import { ListSchema, VersionOrUndefined, _VersionOrUndefined } from '../../../common/schemas';
import { ListSchema, _VersionOrUndefined } from '../../../common/schemas';
import { UpdateEsListSchema } from '../../schemas/elastic_query';
import { getList } from '.';

View file

@ -1,77 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { addIdToItem, removeIdFromItem } from './add_remove_id_to_item';
jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('123'),
}));
describe('add_remove_id_to_item', () => {
afterEach(() => {
jest.clearAllMocks();
});
describe('addIdToItem', () => {
test('it adds an id to an empty item', () => {
expect(addIdToItem({})).toEqual({ id: '123' });
});
test('it adds a complex object', () => {
expect(
addIdToItem({
field: '',
type: 'mapping',
value: '',
})
).toEqual({
id: '123',
field: '',
type: 'mapping',
value: '',
});
});
test('it adds an id to an existing item', () => {
expect(addIdToItem({ test: '456' })).toEqual({ id: '123', test: '456' });
});
test('it does not change the id if it already exists', () => {
expect(addIdToItem({ id: '456' })).toEqual({ id: '456' });
});
test('it returns the same reference if it has an id already', () => {
const obj = { id: '456' };
expect(addIdToItem(obj)).toBe(obj);
});
test('it returns a new reference if it adds an id to an item', () => {
const obj = { test: '456' };
expect(addIdToItem(obj)).not.toBe(obj);
});
});
describe('removeIdFromItem', () => {
test('it removes an id from an item', () => {
expect(removeIdFromItem({ id: '456' })).toEqual({});
});
test('it returns a new reference if it removes an id from an item', () => {
const obj = { id: '123', test: '456' };
expect(removeIdFromItem(obj)).not.toBe(obj);
});
test('it does not effect an item without an id', () => {
expect(removeIdFromItem({ test: '456' })).toEqual({ test: '456' });
});
test('it returns the same reference if it does not have an id already', () => {
const obj = { test: '456' };
expect(removeIdFromItem(obj)).toBe(obj);
});
});
});

View file

@ -1,50 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import uuid from 'uuid';
/**
* This is useful for when you have arrays without an ID and need to add one for
* ReactJS keys. I break the types slightly by introducing an id to an arbitrary item
* but then cast it back to the regular type T.
* Usage of this could be considered tech debt as I am adding an ID when the backend
* could be doing the same thing but it depends on how you want to model your data and
* if you view modeling your data with id's to please ReactJS a good or bad thing.
* @param item The item to add an id to.
*/
type NotArray<T> = T extends unknown[] ? never : T;
export const addIdToItem = <T>(item: NotArray<T>): T => {
const maybeId: typeof item & { id?: string } = item;
if (maybeId.id != null) {
return item;
} else {
return { ...item, id: uuid.v4() };
}
};
/**
* This is to reverse the id you added to your arrays for ReactJS keys.
* @param item The item to remove the id from.
*/
export const removeIdFromItem = <T>(
item: NotArray<T>
):
| T
| Pick<
T & {
id?: string | undefined;
},
Exclude<keyof T, 'id'>
> => {
const maybeId: typeof item & { id?: string } = item;
if (maybeId.id != null) {
const { id, ...noId } = maybeId;
return noId;
} else {
return item;
}
};

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import { Language } from '@kbn/securitysolution-io-ts-alerting-types';
import {
Filter,
IIndexPattern,
@ -17,7 +18,7 @@ import {
} from '../../../lists/common/schemas';
import { ESBoolQuery } from '../typed_json';
import { buildExceptionFilter } from '../shared_imports';
import { Query, Language, Index, TimestampOverrideOrUndefined } from './schemas/common/schemas';
import { Query, Index, TimestampOverrideOrUndefined } from './schemas/common/schemas';
export const getQueryFilter = (
query: Query,

View file

@ -8,21 +8,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
import {
SavedObjectAttributes,
SavedObjectAttribute,
SavedObjectAttributeSingle,
} from 'src/core/types';
import { RiskScore } from '../types/risk_score';
import { UUID } from '../types/uuid';
import { IsoDateString } from '../types/iso_date_string';
import { PositiveIntegerGreaterThanZero } from '../types/positive_integer_greater_than_zero';
import { PositiveInteger } from '../types/positive_integer';
import { NonEmptyString } from '../types/non_empty_string';
import { parseScheduleDates } from '../../parse_schedule_dates';
import { machine_learning_job_id_normalized } from '../types/normalized_ml_job_id';
UUID,
NonEmptyString,
IsoDateString,
PositiveIntegerGreaterThanZero,
PositiveInteger,
} from '@kbn/securitysolution-io-ts-types';
export const author = t.array(t.string);
export type Author = t.TypeOf<typeof author>;
@ -76,79 +69,6 @@ export type Filters = t.TypeOf<typeof filters>; // Filters are not easily type-a
export const filtersOrUndefined = t.union([filters, t.undefined]);
export type FiltersOrUndefined = t.TypeOf<typeof filtersOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const saved_object_attribute_single: t.Type<SavedObjectAttributeSingle> = t.recursion(
'saved_object_attribute_single',
() => t.union([t.string, t.number, t.boolean, t.null, t.undefined, saved_object_attributes])
);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const saved_object_attribute: t.Type<SavedObjectAttribute> = t.recursion(
'saved_object_attribute',
() => t.union([saved_object_attribute_single, t.array(saved_object_attribute_single)])
);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const saved_object_attributes: t.Type<SavedObjectAttributes> = t.recursion(
'saved_object_attributes',
() => t.record(t.string, saved_object_attribute)
);
/**
* Params is an "object", since it is a type of AlertActionParams which is action templates.
* @see x-pack/plugins/alerting/common/alert.ts
*/
export const action_group = t.string;
export const action_id = t.string;
export const action_action_type_id = t.string;
export const action_params = saved_object_attributes;
export const action = t.exact(
t.type({
group: action_group,
id: action_id,
action_type_id: action_action_type_id,
params: action_params,
})
);
export const actions = t.array(action);
export type Actions = t.TypeOf<typeof actions>;
export const actionsCamel = t.array(
t.exact(
t.type({
group: action_group,
id: action_id,
actionTypeId: action_action_type_id,
params: action_params,
})
)
);
export type ActionsCamel = t.TypeOf<typeof actions>;
const stringValidator = (input: unknown): input is string => typeof input === 'string';
export const from = new t.Type<string, string, unknown>(
'From',
t.string.is,
(input, context): Either<t.Errors, string> => {
if (stringValidator(input) && parseScheduleDates(input) == null) {
return t.failure(input, context, 'Failed to parse "from" on rule param');
}
return t.string.validate(input, context);
},
t.identity
);
export type From = t.TypeOf<typeof from>;
export const fromOrUndefined = t.union([from, t.undefined]);
export type FromOrUndefined = t.TypeOf<typeof fromOrUndefined>;
export const immutable = t.boolean;
export type Immutable = t.TypeOf<typeof immutable>;
@ -183,26 +103,6 @@ export type Query = t.TypeOf<typeof query>;
export const queryOrUndefined = t.union([query, t.undefined]);
export type QueryOrUndefined = t.TypeOf<typeof queryOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const language = t.keyof({ eql: null, kuery: null, lucene: null });
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Language = t.TypeOf<typeof language>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const languageOrUndefined = t.union([language, t.undefined]);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type LanguageOrUndefined = t.TypeOf<typeof languageOrUndefined>;
export const license = t.string;
export type License = t.TypeOf<typeof license>;
@ -241,27 +141,12 @@ export type TimestampOverride = t.TypeOf<typeof timestamp_override>;
export const timestampOverrideOrUndefined = t.union([timestamp_override, t.undefined]);
export type TimestampOverrideOrUndefined = t.TypeOf<typeof timestampOverrideOrUndefined>;
export const throttle = t.string;
export type Throttle = t.TypeOf<typeof throttle>;
export const throttleOrNull = t.union([throttle, t.null]);
export type ThrottleOrNull = t.TypeOf<typeof throttleOrNull>;
export const throttleOrNullOrUndefined = t.union([throttle, t.null, t.undefined]);
export type ThrottleOrUndefinedOrNull = t.TypeOf<typeof throttleOrNullOrUndefined>;
export const anomaly_threshold = PositiveInteger;
export type AnomalyThreshold = t.TypeOf<typeof PositiveInteger>;
export const anomalyThresholdOrUndefined = t.union([anomaly_threshold, t.undefined]);
export type AnomalyThresholdOrUndefined = t.TypeOf<typeof anomalyThresholdOrUndefined>;
export const machine_learning_job_id = t.union([t.string, machine_learning_job_id_normalized]);
export type MachineLearningJobId = t.TypeOf<typeof machine_learning_job_id>;
export const machineLearningJobIdOrUndefined = t.union([machine_learning_job_id, t.undefined]);
export type MachineLearningJobIdOrUndefined = t.TypeOf<typeof machineLearningJobIdOrUndefined>;
/**
* Note that this is a non-exact io-ts type as we allow extra meta information
* to be added to the meta object
@ -271,158 +156,18 @@ export type Meta = t.TypeOf<typeof meta>;
export const metaOrUndefined = t.union([meta, t.undefined]);
export type MetaOrUndefined = t.TypeOf<typeof metaOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const max_signals = PositiveIntegerGreaterThanZero;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type MaxSignals = t.TypeOf<typeof max_signals>;
export const maxSignalsOrUndefined = t.union([max_signals, t.undefined]);
export type MaxSignalsOrUndefined = t.TypeOf<typeof maxSignalsOrUndefined>;
export const name = NonEmptyString;
export type Name = t.TypeOf<typeof name>;
export const nameOrUndefined = t.union([name, t.undefined]);
export type NameOrUndefined = t.TypeOf<typeof nameOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const operator = t.keyof({
equals: null,
});
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Operator = t.TypeOf<typeof operator>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export enum OperatorEnum {
EQUALS = 'equals',
}
export const risk_score = RiskScore;
export type RiskScore = t.TypeOf<typeof risk_score>;
export const riskScoreOrUndefined = t.union([risk_score, t.undefined]);
export type RiskScoreOrUndefined = t.TypeOf<typeof riskScoreOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const risk_score_mapping_field = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const risk_score_mapping_value = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const risk_score_mapping_item = t.exact(
t.type({
field: risk_score_mapping_field,
value: risk_score_mapping_value,
operator,
risk_score: riskScoreOrUndefined,
})
);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const risk_score_mapping = t.array(risk_score_mapping_item);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type RiskScoreMapping = t.TypeOf<typeof risk_score_mapping>;
export const riskScoreMappingOrUndefined = t.union([risk_score_mapping, t.undefined]);
export type RiskScoreMappingOrUndefined = t.TypeOf<typeof riskScoreMappingOrUndefined>;
export const rule_name_override = t.string;
export type RuleNameOverride = t.TypeOf<typeof rule_name_override>;
export const ruleNameOverrideOrUndefined = t.union([rule_name_override, t.undefined]);
export type RuleNameOverrideOrUndefined = t.TypeOf<typeof ruleNameOverrideOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severity = t.keyof({ low: null, medium: null, high: null, critical: null });
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Severity = t.TypeOf<typeof severity>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severityOrUndefined = t.union([severity, t.undefined]);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type SeverityOrUndefined = t.TypeOf<typeof severityOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severity_mapping_field = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severity_mapping_value = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severity_mapping_item = t.exact(
t.type({
field: severity_mapping_field,
operator,
value: severity_mapping_value,
severity,
})
);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type SeverityMappingItem = t.TypeOf<typeof severity_mapping_item>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severity_mapping = t.array(severity_mapping_item);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type SeverityMapping = t.TypeOf<typeof severity_mapping>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const severityMappingOrUndefined = t.union([severity_mapping, t.undefined]);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type SeverityMappingOrUndefined = t.TypeOf<typeof severityMappingOrUndefined>;
export const status = t.keyof({ open: null, closed: null, 'in-progress': null });
export type Status = t.TypeOf<typeof status>;
@ -445,19 +190,6 @@ export type To = t.TypeOf<typeof to>;
export const toOrUndefined = t.union([to, t.undefined]);
export type ToOrUndefined = t.TypeOf<typeof toOrUndefined>;
export const type = t.keyof({
eql: null,
machine_learning: null,
query: null,
saved_query: null,
threshold: null,
threat_match: null,
});
export type Type = t.TypeOf<typeof type>;
export const typeOrUndefined = t.union([type, t.undefined]);
export type TypeOrUndefined = t.TypeOf<typeof typeOrUndefined>;
export const queryFilter = t.string;
export type QueryFilter = t.TypeOf<typeof queryFilter>;
@ -511,152 +243,6 @@ export type Fields = t.TypeOf<typeof fields>;
export const fieldsOrUndefined = t.union([fields, t.undefined]);
export type FieldsOrUndefined = t.TypeOf<typeof fieldsOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_framework = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_tactic_id = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_tactic_name = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_tactic_reference = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_tactic = t.type({
id: threat_tactic_id,
name: threat_tactic_name,
reference: threat_tactic_reference,
});
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type ThreatTactic = t.TypeOf<typeof threat_tactic>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_subtechnique_id = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_subtechnique_name = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_subtechnique_reference = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_subtechnique = t.type({
id: threat_subtechnique_id,
name: threat_subtechnique_name,
reference: threat_subtechnique_reference,
});
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type ThreatSubtechnique = t.TypeOf<typeof threat_subtechnique>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_subtechniques = t.array(threat_subtechnique);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_technique_id = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_technique_name = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_technique_reference = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_technique = t.intersection([
t.exact(
t.type({
id: threat_technique_id,
name: threat_technique_name,
reference: threat_technique_reference,
})
),
t.exact(
t.partial({
subtechnique: threat_subtechniques,
})
),
]);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type ThreatTechnique = t.TypeOf<typeof threat_technique>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat_techniques = t.array(threat_technique);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threat = t.intersection([
t.exact(
t.type({
framework: threat_framework,
tactic: threat_tactic,
})
),
t.exact(
t.partial({
technique: threat_techniques,
})
),
]);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Threat = t.TypeOf<typeof threat>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const threats = t.array(threat);
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Threats = t.TypeOf<typeof threats>;
export const threatsOrUndefined = t.union([threats, t.undefined]);
export type ThreatsOrUndefined = t.TypeOf<typeof threatsOrUndefined>;
export const thresholdField = t.exact(
t.type({
field: t.union([t.string, t.array(t.string)]), // Covers pre- and post-7.12
@ -707,43 +293,19 @@ export type ThresholdNormalized = t.TypeOf<typeof thresholdNormalized>;
export const thresholdNormalizedOrUndefined = t.union([thresholdNormalized, t.undefined]);
export type ThresholdNormalizedOrUndefined = t.TypeOf<typeof thresholdNormalizedOrUndefined>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const created_at = IsoDateString;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const updated_at = IsoDateString;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const updated_by = t.string;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const created_by = t.string;
export const updatedByOrNull = t.union([updated_by, t.null]);
export type UpdatedByOrNull = t.TypeOf<typeof updatedByOrNull>;
export const createdByOrNull = t.union([created_by, t.null]);
export type CreatedByOrNull = t.TypeOf<typeof createdByOrNull>;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const version = PositiveIntegerGreaterThanZero;
/**
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export type Version = t.TypeOf<typeof version>;
export const versionOrUndefined = t.union([version, t.undefined]);
export type VersionOrUndefined = t.TypeOf<typeof versionOrUndefined>;
export const last_success_at = IsoDateString;
export type LastSuccessAt = t.TypeOf<typeof IsoDateString>;

View file

@ -8,46 +8,20 @@
import * as t from 'io-ts';
import {
description,
anomaly_threshold,
filters,
index,
saved_id,
timeline_id,
timeline_title,
meta,
Actions,
DefaultActionsArray,
DefaultFromString,
DefaultIntervalString,
DefaultMaxSignalsNumber,
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
DefaultThreatArray,
DefaultThrottleNull,
DefaultToString,
From,
RiskScoreMapping,
machine_learning_job_id,
risk_score,
MaxSignals,
name,
severity,
Tags,
To,
type,
Threats,
threshold,
ThrottleOrNull,
note,
References,
Actions,
Enabled,
FalsePositives,
From,
Interval,
language,
query,
rule_id,
version,
building_block_type,
license,
rule_name_override,
timestamp_override,
Author,
RiskScoreMapping,
SeverityMapping,
event_category_override,
} from '../common/schemas';
import {
threat_index,
concurrent_searches,
items_per_search,
@ -56,23 +30,48 @@ import {
threat_mapping,
threat_language,
threat_indicator_path,
} from '../types/threat_mapping';
Threats,
type,
language,
severity,
SeverityMapping,
ThrottleOrNull,
MaxSignals,
} from '@kbn/securitysolution-io-ts-alerting-types';
import {
version,
DefaultStringArray,
DefaultActionsArray,
DefaultBooleanFalse,
DefaultFromString,
DefaultIntervalString,
DefaultMaxSignalsNumber,
DefaultToString,
DefaultThreatArray,
DefaultThrottleNull,
DefaultListArray,
ListArray,
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
} from '../types';
} from '@kbn/securitysolution-io-ts-types';
import { DefaultListArray, ListArray } from '@kbn/securitysolution-io-ts-list-types';
import {
description,
anomaly_threshold,
filters,
index,
saved_id,
timeline_id,
timeline_title,
meta,
name,
Tags,
To,
threshold,
note,
References,
Enabled,
FalsePositives,
Interval,
query,
rule_id,
building_block_type,
license,
rule_name_override,
timestamp_override,
Author,
event_category_override,
} from '../common/schemas';
/**
* Big differences between this schema and the createRulesSchema

View file

@ -11,9 +11,8 @@ import {
AddPrepackagedRulesSchema,
} from './add_prepackaged_rules_schema';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
import {
getAddPrepackagedRulesSchemaMock,

View file

@ -6,9 +6,7 @@
*/
import { createRulesBulkSchema, CreateRulesBulkSchema } from './create_rules_bulk_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight } from '../../../test_utils';
import { formatErrors } from '../../../format_errors';
import { exactCheck, foldLeftRight, formatErrors } from '@kbn/securitysolution-io-ts-utils';
import { getCreateRulesSchemaMock } from './rule_schemas.mock';
// only the basics of testing are here.

View file

@ -7,8 +7,8 @@
import * as t from 'io-ts';
import { PositiveInteger, PositiveIntegerGreaterThanZero } from '@kbn/securitysolution-io-ts-types';
import { index } from '../common/schemas';
import { PositiveInteger, PositiveIntegerGreaterThanZero } from '../types';
export const signalsReindexOptions = t.partial({
requests_per_second: t.number,

View file

@ -12,9 +12,8 @@ import {
ExportRulesQuerySchema,
ExportRulesQuerySchemaDecoded,
} from './export_rules_schema';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
describe('create rules schema', () => {

View file

@ -7,11 +7,10 @@
import * as t from 'io-ts';
import { DefaultExportFileName } from '@kbn/securitysolution-io-ts-alerting-types';
import { DefaultStringBooleanFalse } from '@kbn/securitysolution-io-ts-types';
import { rule_id, FileName, ExcludeExportDetails } from '../common/schemas';
import { DefaultExportFileName } from '../types/default_export_file_name';
import { DefaultStringBooleanFalse } from '../types/default_string_boolean_false';
const objects = t.array(t.exact(t.type({ rule_id })));
export const exportRulesSchema = t.union([t.exact(t.type({ objects })), t.null]);
export type ExportRulesSchema = t.TypeOf<typeof exportRulesSchema>;

View file

@ -5,9 +5,8 @@
* 2.0.
*/
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
import { FindRulesSchema, findRulesSchema } from './find_rules_schema';

View file

@ -7,9 +7,8 @@
import * as t from 'io-ts';
import { DefaultPerPage, DefaultPage } from '@kbn/securitysolution-io-ts-alerting-types';
import { queryFilter, fields, sort_field, sort_order, PerPage, Page } from '../common/schemas';
import { DefaultPerPage } from '../types/default_per_page';
import { DefaultPage } from '../types/default_page';
export const findRulesSchema = t.exact(
t.partial({

View file

@ -7,7 +7,7 @@
import * as t from 'io-ts';
import { from } from '../common/schemas';
import { from } from '@kbn/securitysolution-io-ts-alerting-types';
export const getSignalsMigrationStatusSchema = t.exact(
t.type({

View file

@ -5,9 +5,8 @@
* 2.0.
*/
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
import {
ImportRulesSchema,

View file

@ -7,6 +7,47 @@
import * as t from 'io-ts';
import {
Actions,
DefaultActionsArray,
DefaultFromString,
DefaultIntervalString,
DefaultMaxSignalsNumber,
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
DefaultThreatArray,
DefaultThrottleNull,
DefaultToString,
From,
machine_learning_job_id,
risk_score,
RiskScoreMapping,
threat_index,
items_per_search,
concurrent_searches,
threat_query,
threat_filters,
threat_mapping,
threat_language,
threat_indicator_path,
Threats,
type,
language,
severity,
SeverityMapping,
ThrottleOrNull,
MaxSignals,
} from '@kbn/securitysolution-io-ts-alerting-types';
import {
DefaultVersionNumber,
Version,
DefaultStringArray,
DefaultBooleanTrue,
OnlyFalseAllowed,
DefaultStringBooleanFalse,
} from '@kbn/securitysolution-io-ts-types';
import { DefaultListArray, ListArray } from '@kbn/securitysolution-io-ts-list-types';
import {
description,
anomaly_threshold,
@ -18,26 +59,15 @@ import {
timeline_id,
timeline_title,
meta,
machine_learning_job_id,
risk_score,
MaxSignals,
name,
severity,
Tags,
To,
type,
Threats,
threshold,
ThrottleOrNull,
note,
Version,
References,
Actions,
Enabled,
FalsePositives,
From,
Interval,
language,
query,
rule_id,
id,
@ -50,39 +80,8 @@ import {
rule_name_override,
timestamp_override,
Author,
RiskScoreMapping,
SeverityMapping,
event_category_override,
} from '../common/schemas';
import {
threat_index,
items_per_search,
concurrent_searches,
threat_query,
threat_filters,
threat_mapping,
threat_language,
threat_indicator_path,
} from '../types/threat_mapping';
import {
DefaultStringArray,
DefaultActionsArray,
DefaultBooleanTrue,
DefaultFromString,
DefaultIntervalString,
DefaultMaxSignalsNumber,
DefaultToString,
DefaultThreatArray,
DefaultThrottleNull,
DefaultVersionNumber,
OnlyFalseAllowed,
DefaultStringBooleanFalse,
DefaultListArray,
ListArray,
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
} from '../types';
/**
* Differences from this and the createRulesSchema are

View file

@ -6,9 +6,7 @@
*/
import { patchRulesBulkSchema, PatchRulesBulkSchema } from './patch_rules_bulk_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight } from '../../../test_utils';
import { formatErrors } from '../../../format_errors';
import { exactCheck, formatErrors, foldLeftRight } from '@kbn/securitysolution-io-ts-utils';
import { PatchRulesSchema } from './patch_rules_schema';
// only the basics of testing are here.

View file

@ -7,9 +7,8 @@
import { patchRulesSchema, PatchRulesSchema, PatchRulesSchemaDecoded } from './patch_rules_schema';
import { getPatchRulesSchemaMock, getPatchRulesSchemaDecodedMock } from './patch_rules_schema.mock';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
import { getListArrayMock } from '../types/lists.mock';

View file

@ -7,6 +7,32 @@
import * as t from 'io-ts';
import {
actions,
from,
machine_learning_job_id,
risk_score,
risk_score_mapping,
threat_index,
concurrent_searches,
items_per_search,
threat_query,
threat_filters,
threat_mapping,
threat_language,
threat_indicator_path,
threats,
type,
language,
severity,
severity_mapping,
max_signals,
throttle,
} from '@kbn/securitysolution-io-ts-alerting-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import { listArrayOrUndefined } from '@kbn/securitysolution-io-ts-list-types';
import {
description,
anomaly_threshold,
@ -17,27 +43,16 @@ import {
timeline_id,
timeline_title,
meta,
machine_learning_job_id,
risk_score,
rule_id,
name,
severity,
type,
note,
version,
actions,
false_positives,
interval,
max_signals,
from,
enabled,
tags,
threats,
threshold,
throttle,
references,
to,
language,
query,
id,
building_block_type,
@ -45,21 +60,8 @@ import {
license,
rule_name_override,
timestamp_override,
risk_score_mapping,
severity_mapping,
event_category_override,
} from '../common/schemas';
import {
threat_index,
concurrent_searches,
items_per_search,
threat_query,
threat_filters,
threat_mapping,
threat_language,
threat_indicator_path,
} from '../types/threat_mapping';
import { listArrayOrUndefined } from '../types/lists';
/**
* All of the patch elements should default to undefined if not set

View file

@ -6,9 +6,7 @@
*/
import { queryRulesBulkSchema, QueryRulesBulkSchema } from './query_rules_bulk_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight } from '../../../test_utils';
import { formatErrors } from '../../../format_errors';
import { exactCheck, formatErrors, foldLeftRight } from '@kbn/securitysolution-io-ts-utils';
// only the basics of testing are here.
// see: query_rules_schema.test.ts for the bulk of the validation tests

View file

@ -6,9 +6,8 @@
*/
import { queryRulesSchema, QueryRulesSchema } from './query_rules_schema';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
describe('query_rules_schema', () => {

View file

@ -6,9 +6,8 @@
*/
import { QuerySignalsSchema, querySignalsSchema } from './query_signals_index_schema';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
describe('query, aggs, size, _source and track_total_hits on signals index', () => {

View file

@ -6,7 +6,7 @@
*/
import * as t from 'io-ts';
import { PositiveIntegerGreaterThanZero } from '../types/positive_integer_greater_than_zero';
import { PositiveIntegerGreaterThanZero } from '@kbn/securitysolution-io-ts-types';
export const querySignalsSchema = t.exact(
t.partial({

View file

@ -6,9 +6,8 @@
*/
import { createRulesSchema, CreateRulesSchema, SavedQueryCreateSchema } from './rule_schemas';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
import {
getCreateSavedQueryRulesSchemaMock,

View file

@ -7,8 +7,12 @@
import * as t from 'io-ts';
import { listArray } from '../types/lists';
import {
actions,
from,
risk_score,
machine_learning_job_id,
risk_score_mapping,
threat_filters,
threat_query,
threat_mapping,
@ -16,14 +20,20 @@ import {
threat_indicator_path,
concurrent_searches,
items_per_search,
} from '../types/threat_mapping';
threats,
severity_mapping,
severity,
max_signals,
throttle,
} from '@kbn/securitysolution-io-ts-alerting-types';
import { listArray } from '@kbn/securitysolution-io-ts-list-types';
import { version } from '@kbn/securitysolution-io-ts-types';
import {
id,
index,
filters,
event_category_override,
risk_score_mapping,
severity_mapping,
building_block_type,
note,
license,
@ -35,25 +45,17 @@ import {
author,
description,
false_positives,
from,
rule_id,
immutable,
output_index,
query,
machine_learning_job_id,
max_signals,
risk_score,
severity,
threats,
to,
references,
version,
saved_id,
threshold,
anomaly_threshold,
name,
tags,
actions,
interval,
enabled,
updated_at,
@ -66,7 +68,6 @@ import {
last_success_message,
last_failure_at,
last_failure_message,
throttle,
} from '../common/schemas';
const createSchema = <

View file

@ -6,9 +6,8 @@
*/
import { setSignalsStatusSchema, SetSignalsStatusSchema } from './set_signal_status_schema';
import { exactCheck } from '../../../exact_check';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { pipe } from 'fp-ts/lib/pipeable';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { left } from 'fp-ts/lib/Either';
describe('set signal status schema', () => {

View file

@ -6,9 +6,7 @@
*/
import { updateRulesBulkSchema, UpdateRulesBulkSchema } from './update_rules_bulk_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight } from '../../../test_utils';
import { formatErrors } from '../../../format_errors';
import { exactCheck, formatErrors, foldLeftRight } from '@kbn/securitysolution-io-ts-utils';
import { getUpdateRulesSchemaMock } from './rule_schemas.mock';
import { UpdateRulesSchema } from './rule_schemas';

View file

@ -9,8 +9,7 @@ import { left } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import { errorSchema, ErrorSchema } from './error_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { getErrorSchemaMock } from './error_schema.mocks';
describe('error_schema', () => {

View file

@ -10,8 +10,7 @@ import { left, Either } from 'fp-ts/lib/Either';
import { ImportRulesSchema, importRulesSchema } from './import_rules_schema';
import { ErrorSchema } from './error_schema';
import { Errors } from 'io-ts';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
describe('import_rules_schema', () => {
test('it should validate an empty import response with no errors', () => {

View file

@ -11,8 +11,7 @@ import {
PrePackagedRulesAndTimelinesSchema,
prePackagedRulesAndTimelinesSchema,
} from './prepackaged_rules_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
describe('prepackaged_rules_schema', () => {
test('it should validate an empty prepackaged response with defaults', () => {

View file

@ -11,8 +11,7 @@ import {
PrePackagedRulesAndTimelinesStatusSchema,
prePackagedRulesAndTimelinesStatusSchema,
} from './prepackaged_rules_status_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
describe('prepackaged_rules_schema', () => {
test('it should validate an empty prepackaged response with defaults', () => {

View file

@ -11,8 +11,7 @@ import { pipe } from 'fp-ts/lib/pipeable';
import { RulesBulkSchema, rulesBulkSchema } from './rules_bulk_schema';
import { RulesSchema } from './rules_schema';
import { ErrorSchema } from './error_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { getRulesSchemaMock } from './rules_schema.mocks';
import { getErrorSchemaMock } from './error_schema.mocks';

View file

@ -21,8 +21,7 @@ import {
addThreatMatchFields,
addEqlFields,
} from './rules_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
import { TypeAndTimelineOnly } from './type_timeline_only_schema';
import {
getRulesSchemaMock,
@ -30,7 +29,7 @@ import {
getThreatMatchingSchemaMock,
getRulesEqlSchemaMock,
} from './rules_schema.mocks';
import { ListArray } from '../types/lists';
import { ListArray } from '@kbn/securitysolution-io-ts-list-types';
export const ANCHOR_DATE = '2020-02-20T03:57:54.037Z';

View file

@ -10,59 +10,13 @@ import { isObject } from 'lodash/fp';
import { Either, left, fold } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import { isMlRule } from '../../../machine_learning/helpers';
import { isThresholdRule } from '../../utils';
import {
actions,
anomaly_threshold,
description,
enabled,
event_category_override,
false_positives,
from,
id,
immutable,
index,
interval,
rule_id,
language,
name,
output_index,
max_signals,
machine_learning_job_id,
query,
references,
severity,
updated_by,
tags,
to,
risk_score,
created_at,
created_by,
updated_at,
saved_id,
timeline_id,
timeline_title,
type,
threats,
threshold,
throttle,
job_status,
status_date,
last_success_at,
last_success_message,
last_failure_at,
last_failure_message,
version,
filters,
meta,
note,
building_block_type,
license,
rule_name_override,
timestamp_override,
} from '../common/schemas';
import {
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
threat_index,
concurrent_searches,
items_per_search,
@ -71,14 +25,58 @@ import {
threat_mapping,
threat_language,
threat_indicator_path,
} from '../types/threat_mapping';
threats,
type,
language,
severity,
throttle,
max_signals,
} from '@kbn/securitysolution-io-ts-alerting-types';
import { DefaultStringArray, version } from '@kbn/securitysolution-io-ts-types';
import { DefaultListArray } from '../types/lists_default_array';
import { DefaultListArray } from '@kbn/securitysolution-io-ts-list-types';
import { isMlRule } from '../../../machine_learning/helpers';
import { isThresholdRule } from '../../utils';
import {
DefaultStringArray,
DefaultRiskScoreMappingArray,
DefaultSeverityMappingArray,
} from '../types';
anomaly_threshold,
description,
enabled,
event_category_override,
false_positives,
id,
immutable,
index,
interval,
rule_id,
name,
output_index,
query,
references,
updated_by,
tags,
to,
created_at,
created_by,
updated_at,
saved_id,
timeline_id,
timeline_title,
threshold,
job_status,
status_date,
last_success_at,
last_success_message,
last_failure_at,
last_failure_message,
filters,
meta,
note,
building_block_type,
license,
rule_name_override,
timestamp_override,
} from '../common/schemas';
import { typeAndTimelineOnlySchema, TypeAndTimelineOnly } from './type_timeline_only_schema';
/**

View file

@ -9,8 +9,7 @@ import { left } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import { TypeAndTimelineOnly, typeAndTimelineOnlySchema } from './type_timeline_only_schema';
import { exactCheck } from '../../../exact_check';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';
describe('prepackaged_rule_schema', () => {
test('it should validate a a type and timeline_id together', () => {

View file

@ -7,7 +7,8 @@
import * as t from 'io-ts';
import { timeline_id, type } from '../common/schemas';
import { type } from '@kbn/securitysolution-io-ts-alerting-types';
import { timeline_id } from '../common/schemas';
/**
* Special schema type that is only the type and the timeline_id.

View file

@ -1,51 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultBooleanTrue } from './default_boolean_true';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_boolean_true', () => {
test('it should validate a boolean false', () => {
const payload = false;
const decoded = DefaultBooleanTrue.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should validate a boolean true', () => {
const payload = true;
const decoded = DefaultBooleanTrue.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultBooleanTrue.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultBooleanTrue"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default true', () => {
const payload = null;
const decoded = DefaultBooleanTrue.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(true);
});
});

View file

@ -1,42 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultFromString } from './default_from_string';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_from_string', () => {
test('it should validate a from string', () => {
const payload = 'now-20m';
const decoded = DefaultFromString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultFromString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultFromString"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default of "now-6m"', () => {
const payload = null;
const decoded = DefaultFromString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual('now-6m');
});
});

View file

@ -1,57 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultActionsArray } from './default_actions_array';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
import { Actions } from '../common/schemas';
describe('default_actions_array', () => {
test('it should validate an empty array', () => {
const payload: string[] = [];
const decoded = DefaultActionsArray.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should validate an array of actions', () => {
const payload: Actions = [
{ id: '123', group: 'group', action_type_id: 'action_type_id', params: {} },
];
const decoded = DefaultActionsArray.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate an array with a number', () => {
const payload = [
{ id: '123', group: 'group', action_type_id: 'action_type_id', params: {} },
5,
];
const decoded = DefaultActionsArray.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultActionsArray"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default array entry', () => {
const payload = null;
const decoded = DefaultActionsArray.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual([]);
});
});

View file

@ -1,23 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
import { actions, Actions } from '../common/schemas';
/**
* Types the DefaultStringArray as:
* - If undefined, then a default action array will be set
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultActionsArray = new t.Type<Actions, Actions | undefined, unknown>(
'DefaultActionsArray',
actions.is,
(input, context): Either<t.Errors, Actions> =>
input == null ? t.success([]) : actions.validate(input, context),
t.identity
);

View file

@ -1,81 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { DefaultArray } from './default_array';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
const testSchema = t.keyof({
valid: true,
also_valid: true,
});
type TestSchema = t.TypeOf<typeof testSchema>;
const defaultArraySchema = DefaultArray(testSchema);
describe('default_array', () => {
test('it should validate an empty array', () => {
const payload: string[] = [];
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should validate an array of testSchema', () => {
const payload: TestSchema[] = ['valid'];
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should validate an array of valid testSchema strings', () => {
const payload = ['valid', 'also_valid'];
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate an array with a number', () => {
const payload = ['valid', 123];
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "123" supplied to "DefaultArray"',
]);
expect(message.schema).toEqual({});
});
test('it should not validate an array with an invalid string', () => {
const payload = ['valid', 'invalid'];
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "invalid" supplied to "DefaultArray"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default array entry', () => {
const payload = null;
const decoded = defaultArraySchema.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual([]);
});
});

View file

@ -1,27 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultArray<C> as:
* - If undefined, then a default array will be set
* - If an array is sent in, then the array will be validated to ensure all elements are type C
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultArray = <C extends t.Mixed>(codec: C) => {
const arrType = t.array(codec);
type ArrType = t.TypeOf<typeof arrType>;
return new t.Type<ArrType, ArrType | undefined, unknown>(
'DefaultArray',
arrType.is,
(input, context): Either<t.Errors, ArrType> =>
input == null ? t.success([]) : arrType.validate(input, context),
t.identity
);
};

View file

@ -1,51 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultBooleanFalse } from './default_boolean_false';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_boolean_false', () => {
test('it should validate a boolean false', () => {
const payload = false;
const decoded = DefaultBooleanFalse.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should validate a boolean true', () => {
const payload = true;
const decoded = DefaultBooleanFalse.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultBooleanFalse.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultBooleanFalse"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default false', () => {
const payload = null;
const decoded = DefaultBooleanFalse.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(false);
});
});

View file

@ -1,22 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultBooleanFalse as:
* - If null or undefined, then a default false will be set
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultBooleanFalse = new t.Type<boolean, boolean | undefined, unknown>(
'DefaultBooleanFalse',
t.boolean.is,
(input, context): Either<t.Errors, boolean> =>
input == null ? t.success(false) : t.boolean.validate(input, context),
t.identity
);

View file

@ -1,22 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultBooleanTrue as:
* - If null or undefined, then a default true will be set
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultBooleanTrue = new t.Type<boolean, boolean | undefined, unknown>(
'DefaultBooleanTrue',
t.boolean.is,
(input, context): Either<t.Errors, boolean> =>
input == null ? t.success(true) : t.boolean.validate(input, context),
t.identity
);

View file

@ -1,42 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultEmptyString } from './default_empty_string';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_empty_string', () => {
test('it should validate a regular string', () => {
const payload = 'some string';
const decoded = DefaultEmptyString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultEmptyString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultEmptyString"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default of ""', () => {
const payload = null;
const decoded = DefaultEmptyString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual('');
});
});

View file

@ -1,22 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultEmptyString as:
* - If null or undefined, then a default of an empty string "" will be used
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultEmptyString = new t.Type<string, string | undefined, unknown>(
'DefaultEmptyString',
t.string.is,
(input, context): Either<t.Errors, string> =>
input == null ? t.success('') : t.string.validate(input, context),
t.identity
);

View file

@ -1,42 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultExportFileName } from './default_export_file_name';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_export_file_name', () => {
test('it should validate a regular string', () => {
const payload = 'some string';
const decoded = DefaultExportFileName.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultExportFileName.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultExportFileName"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default of "export.ndjson"', () => {
const payload = null;
const decoded = DefaultExportFileName.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual('export.ndjson');
});
});

View file

@ -1,22 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultExportFileName as:
* - If null or undefined, then a default of "export.ndjson" will be used
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultExportFileName = new t.Type<string, string | undefined, unknown>(
'DefaultExportFileName',
t.string.is,
(input, context): Either<t.Errors, string> =>
input == null ? t.success('export.ndjson') : t.string.validate(input, context),
t.identity
);

View file

@ -1,26 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
import { from } from '../common/schemas';
/**
* Types the DefaultFromString as:
* - If null or undefined, then a default of the string "now-6m" will be used
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultFromString = new t.Type<string, string | undefined, unknown>(
'DefaultFromString',
t.string.is,
(input, context): Either<t.Errors, string> => {
if (input == null) {
return t.success('now-6m');
}
return from.validate(input, context);
},
t.identity
);

View file

@ -1,42 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DefaultIntervalString } from './default_interval_string';
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';
import { foldLeftRight, getPaths } from '../../../test_utils';
describe('default_interval_string', () => {
test('it should validate a interval string', () => {
const payload = '20m';
const decoded = DefaultIntervalString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});
test('it should not validate a number', () => {
const payload = 5;
const decoded = DefaultIntervalString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "5" supplied to "DefaultIntervalString"',
]);
expect(message.schema).toEqual({});
});
test('it should return a default of "5m"', () => {
const payload = null;
const decoded = DefaultIntervalString.decode(payload);
const message = pipe(decoded, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual('5m');
});
});

View file

@ -1,22 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/lib/Either';
/**
* Types the DefaultIntervalString as:
* - If null or undefined, then a default of the string "5m" will be used
* @deprecated Use packages/kbn-securitysolution-io-ts-utils
*/
export const DefaultIntervalString = new t.Type<string, string | undefined, unknown>(
'DefaultIntervalString',
t.string.is,
(input, context): Either<t.Errors, string> =>
input == null ? t.success('5m') : t.string.validate(input, context),
t.identity
);

Some files were not shown because too many files have changed in this diff Show more