Skip to content

Commit

Permalink
updated types and tests to use dateRt
Browse files Browse the repository at this point in the history
  • Loading branch information
js-jankisalvi committed Oct 18, 2024
1 parent 1327c66 commit 45919d5
Show file tree
Hide file tree
Showing 27 changed files with 817 additions and 323 deletions.
37 changes: 0 additions & 37 deletions x-pack/plugins/cases/common/schema/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
limitedStringSchema,
NonEmptyString,
paginationSchema,
dateSchema,
} from '.';
import { MAX_DOCS_PER_PAGE } from '../constants';

Expand Down Expand Up @@ -320,40 +319,4 @@ describe('schema', () => {
`);
});
});

describe('dateSchema', () => {
it('succeeds with correct format YYYY-MM-DD', () => {
expect(PathReporter.report(dateSchema().decode('2024-09-30'))).toMatchInlineSnapshot(`
Array [
"No errors!",
]
`);
});

it('succeeds with format YYYY-MM-DD-HH:MM:SS', () => {
expect(PathReporter.report(dateSchema().decode('2024-10-01T15:11:48')))
.toMatchInlineSnapshot(`
Array [
"No errors!",
]
`);
});

it('fails when invalid date', () => {
expect(PathReporter.report(dateSchema().decode('13/05/2024 15:11:48.033Z')))
.toMatchInlineSnapshot(`
Array [
"13/05/2024 15:11:48.033Z is not a valid date.",
]
`);
});

it('fails when text passed', () => {
expect(PathReporter.report(dateSchema().decode('hello'))).toMatchInlineSnapshot(`
Array [
"hello is not a valid date.",
]
`);
});
});
});
16 changes: 0 additions & 16 deletions x-pack/plugins/cases/common/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import * as rt from 'io-ts';
import { either } from 'fp-ts/lib/Either';

import moment from 'moment';
import { MAX_DOCS_PER_PAGE } from '../constants';
import type { PartialPaginationType } from './types';
import { PaginationSchemaRt } from './types';
Expand Down Expand Up @@ -177,18 +176,3 @@ export const regexStringRt = ({ codec, pattern, message }: RegexStringSchemaType
}),
rt.identity
);

export const dateSchema = () =>
new rt.Type<string, string, unknown>(
'dateString',
rt.string.is,
(input, context) =>
either.chain(rt.string.validate(input, context), (value) => {
if (!moment(value, true).isValid()) {
return rt.failure(input, context, `${value} is not a valid date.`);
}

return rt.success(value);
}),
rt.identity
);
19 changes: 10 additions & 9 deletions x-pack/plugins/cases/common/types/api/case/v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
CasesPatchRequestRt,
CasesSearchRequestRt,
} from './v1';
import type { CustomFieldDate } from '../../domain/custom_field/v1';
import { CustomFieldTypes } from '../../domain/custom_field/v1';

const basicCase: Case = {
Expand Down Expand Up @@ -152,7 +153,7 @@ describe('CasePostRequestRt', () => {
{
key: 'third_custom_field_key',
type: CustomFieldTypes.DATE,
value: '2024-10-07',
value: '2024-10-07' as CustomFieldDate,
},
],
};
Expand Down Expand Up @@ -357,8 +358,8 @@ describe('CasePostRequestRt', () => {
},
],
})
)
).toContain('202 is not a valid date.');
)[0]
).toContain('Invalid value "202" supplied');
});

it('throws an error when a date customField has text value', () => {
Expand All @@ -374,8 +375,8 @@ describe('CasePostRequestRt', () => {
},
],
})
)
).toContain('hello is not a valid date.');
)[0]
).toContain('Invalid value "hello" supplied');
});
});

Expand Down Expand Up @@ -839,8 +840,8 @@ describe('CasePatchRequestRt', () => {
},
],
})
)
).toContain('-1 is not a valid date.');
)[0]
).toContain('Invalid value "-1" supplied');
});

it('throws an error when a date customField is text', () => {
Expand All @@ -856,8 +857,8 @@ describe('CasePatchRequestRt', () => {
},
],
})
)
).toContain('text is not a valid date.');
)[0]
).toContain('Invalid value "text" supplied');
});
});

Expand Down
14 changes: 4 additions & 10 deletions x-pack/plugins/cases/common/types/api/case/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as rt from 'io-ts';
import { dateRt } from '@kbn/io-ts-utils';
import {
MAX_DESCRIPTION_LENGTH,
MAX_LENGTH_PER_TAG,
Expand All @@ -28,11 +29,10 @@ import {
limitedArraySchema,
NonEmptyString,
paginationSchema,
dateSchema,
} from '../../../schema';
import {
CaseCustomFieldDateRt,
CaseCustomFieldToggleRt,
CustomFieldDateTypeRt,
CustomFieldTextTypeRt,
} from '../../domain';
import {
Expand All @@ -54,16 +54,10 @@ const CaseCustomFieldTextWithValidationRt = rt.strict({
value: rt.union([CaseCustomFieldTextWithValidationValueRt('value'), rt.null]),
});

const CaseCustomFieldDateWithValidationRt = rt.strict({
key: rt.string,
type: CustomFieldDateTypeRt,
value: rt.union([dateSchema(), rt.null]),
});

const CustomFieldRt = rt.union([
CaseCustomFieldTextWithValidationRt,
CaseCustomFieldToggleRt,
CaseCustomFieldDateWithValidationRt,
CaseCustomFieldDateRt,
]);

export const CaseRequestCustomFieldsRt = limitedArraySchema({
Expand Down Expand Up @@ -375,7 +369,7 @@ export const CasesSearchRequestRt = rt.intersection([
*/
customFields: rt.record(
rt.string,
rt.array(rt.union([rt.string, rt.boolean, rt.number, rt.null]))
rt.array(rt.union([rt.string, dateRt, rt.boolean, rt.number, rt.null]))
),
})
),
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/common/types/api/configure/v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ describe('configure', () => {
defaultValue: '30-08-2020',
})
)[0]
).toContain(`30-08-2020 is not a valid date.`);
).toContain(`Invalid value \"30-08-2020\" supplied`);
});

it('throws an error if the default value is an empty string', () => {
Expand All @@ -581,7 +581,7 @@ describe('configure', () => {
defaultValue: '',
})
)[0]
).toContain(' is not a valid date.');
).toContain('Invalid value "" supplied');
});
});

Expand Down
10 changes: 3 additions & 7 deletions x-pack/plugins/cases/common/types/api/configure/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as rt from 'io-ts';
import { dateRt } from '@kbn/io-ts-utils';
import {
MAX_CUSTOM_FIELDS_PER_CASE,
MAX_CUSTOM_FIELD_KEY_LENGTH,
Expand All @@ -17,12 +18,7 @@ import {
MAX_TEMPLATE_NAME_LENGTH,
MAX_TEMPLATE_TAG_LENGTH,
} from '../../../constants';
import {
limitedArraySchema,
limitedStringSchema,
regexStringRt,
dateSchema,
} from '../../../schema';
import { limitedArraySchema, limitedStringSchema, regexStringRt } from '../../../schema';
import {
CustomFieldDateTypeRt,
CustomFieldTextTypeRt,
Expand Down Expand Up @@ -78,7 +74,7 @@ export const DateCustomFieldConfigurationRt = rt.intersection([
CustomFieldConfigurationWithoutTypeRt,
rt.exact(
rt.partial({
defaultValue: rt.union([dateSchema(), rt.null]),
defaultValue: rt.union([dateRt, rt.null]),
})
),
]);
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/cases/common/types/domain/configure/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as rt from 'io-ts';
import { dateRt } from '@kbn/io-ts-utils';
import { CaseConnectorRt, ConnectorMappingsRt } from '../connector/v1';
import { UserRt } from '../user/v1';
import {
Expand Down Expand Up @@ -60,7 +61,7 @@ export const DateCustomFieldConfigurationRt = rt.intersection([
CustomFieldConfigurationWithoutTypeRt,
rt.exact(
rt.partial({
defaultValue: rt.union([rt.string, rt.null]),
defaultValue: rt.union([dateRt, rt.null]),
})
),
]);
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/cases/common/types/domain/custom_field/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { dateRt } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

export enum CustomFieldTypes {
Expand Down Expand Up @@ -31,7 +32,7 @@ export const CaseCustomFieldToggleRt = rt.strict({
export const CaseCustomFieldDateRt = rt.strict({
key: rt.string,
type: CustomFieldDateTypeRt,
value: rt.union([rt.string, rt.null]),
value: rt.union([dateRt, rt.null]),
});

export const CaseCustomFieldRt = rt.union([
Expand All @@ -46,3 +47,4 @@ export type CaseCustomField = rt.TypeOf<typeof CaseCustomFieldRt>;
export type CaseCustomFieldToggle = rt.TypeOf<typeof CaseCustomFieldToggleRt>;
export type CaseCustomFieldText = rt.TypeOf<typeof CaseCustomFieldTextRt>;
export type CaseCustomFieldDate = rt.TypeOf<typeof CaseCustomFieldDateRt>;
export type CustomFieldDate = rt.TypeOf<typeof dateRt>;
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ describe('CaseFormFields', () => {
test_key_1: 'My text test value 1',
test_key_2: false,
test_key_4: false,
test_key_date_1: '2024-10-16T12:39:21.533Z',
},
},
true
Expand Down Expand Up @@ -268,6 +269,7 @@ describe('CaseFormFields', () => {
test_key_1: 'Test custom filed value',
test_key_2: true,
test_key_4: false,
test_key_date_1: '2024-10-16T12:39:21.533Z',
},
},
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Case View Page files tab', () => {
exact: false,
});

expect(customFields.length).toBe(4);
expect(customFields.length).toBe(6);

expect(await within(customFields[0]).findByRole('heading')).toHaveTextContent(
'My test label 1'
Expand All @@ -103,6 +103,12 @@ describe('Case View Page files tab', () => {
expect(await within(customFields[3]).findByRole('heading')).toHaveTextContent(
'My test label 4'
);
expect(await within(customFields[4]).findByRole('heading')).toHaveTextContent(
'My test label date 1'
);
expect(await within(customFields[5]).findByRole('heading')).toHaveTextContent(
'My test label date 2'
);
});

it('pass the permissions to custom fields correctly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,13 @@ describe('CommonFlyout ', () => {
type: 'text',
value: 'this is a sample text!',
},
{
key: 'test_key_2',
type: 'toggle',
value: true,
},
{
key: 'test_key_3',
type: 'text',
value: null,
},
{
key: 'test_key_4',
type: 'toggle',
value: false,
},
...customFieldsConfigurationMock
.slice(1)
.map(({ key, type, defaultValue, required }) => ({
key,
type,
value: required ? defaultValue : type === CustomFieldTypes.TOGGLE ? false : null,
})),
],
},
});
Expand Down
Loading

0 comments on commit 45919d5

Please sign in to comment.