From 387593d7231bc6db7e9b7ba1c8063985b132ce22 Mon Sep 17 00:00:00 2001 From: Nathan L Smith Date: Sat, 7 Nov 2020 19:15:37 -0600 Subject: [PATCH] Copy `dateAsStringRt` to observability plugin (#82839) Observability was importing `dateAsStringRt` from APM, which creates an implicit circular dependency between the two plugins. Copy that function into where it was being used in observability to remove the dependency. Related to #80508. --- .../observability/common/annotations.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability/common/annotations.ts b/x-pack/plugins/observability/common/annotations.ts index 6aea4d3d92f9..f7ab243cf73f 100644 --- a/x-pack/plugins/observability/common/annotations.ts +++ b/x-pack/plugins/observability/common/annotations.ts @@ -5,7 +5,24 @@ */ import * as t from 'io-ts'; -import { dateAsStringRt } from '../../apm/common/runtime_types/date_as_string_rt'; +import { either } from 'fp-ts/lib/Either'; + +/** + * Checks whether a string is a valid ISO timestamp, + * but doesn't convert it into a Date object when decoding. + * + * Copied from x-pack/plugins/apm/common/runtime_types/date_as_string_rt.ts. + */ +const dateAsStringRt = new t.Type( + 'DateAsString', + t.string.is, + (input, context) => + either.chain(t.string.validate(input, context), (str) => { + const date = new Date(str); + return isNaN(date.getTime()) ? t.failure(input, context) : t.success(str); + }), + t.identity +); export const createAnnotationRt = t.intersection([ t.type({