diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/common/normalizers.ts b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/common/normalizers.ts index 1d329a598f623b..243875cd1de2a8 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/common/normalizers.ts +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/common/normalizers.ts @@ -6,6 +6,7 @@ */ import { NewPackagePolicyInput } from '@kbn/fleet-plugin/common'; +import { parseJsonIfString } from '../helpers/parsers'; import { CommonFields, ConfigKey, DataStream } from '../types'; import { DEFAULT_COMMON_FIELDS, @@ -27,7 +28,8 @@ export type CommonNormalizerMap = Record value ? value.slice(0, value.length - 1) : null; -export const jsonToJavascriptNormalizer = (value: string) => (value ? JSON.parse(value) : null); +export const jsonToJavascriptNormalizer = (value: string) => + value ? parseJsonIfString(value) : null; export function getNormalizer(key: string, defaultValues: Fields): Normalizer { return (fields: NewPackagePolicyInput['vars']) => diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/helpers/parsers.ts b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/helpers/parsers.ts new file mode 100644 index 00000000000000..7dcba327386e62 --- /dev/null +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/helpers/parsers.ts @@ -0,0 +1,14 @@ +/* + * 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. + */ + +export function parseJsonIfString(value: T) { + if (typeof value === 'string') { + return JSON.parse(value); + } + + return value; +} diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/http/normalizers.ts b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/http/normalizers.ts index a4013a0e8024db..f7e7ad3eeac2ee 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/http/normalizers.ts +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/http/normalizers.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { parseJsonIfString } from '../helpers/parsers'; import { HTTPFields, ConfigKey, ContentType, contentTypesToMode } from '../types'; import { Normalizer, @@ -56,7 +57,7 @@ export const httpNormalizers: HTTPNormalizerMap = { const requestHeaders = fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value; if (requestBody) { const headers = requestHeaders - ? JSON.parse(fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value) + ? parseJsonIfString(fields?.[ConfigKey.REQUEST_HEADERS_CHECK]?.value) : defaultHTTPAdvancedFields[ConfigKey.REQUEST_HEADERS_CHECK]; const requestBodyValue = requestBody !== null && requestBody !== undefined diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/tls/normalizers.ts b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/tls/normalizers.ts index a4cf9f280e9a72..eb52494d2cb961 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/tls/normalizers.ts +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/fleet_package/tls/normalizers.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { parseJsonIfString } from '../helpers/parsers'; import { TLSFields, ConfigKey } from '../types'; import { Normalizer } from '../common/normalizers'; import { defaultTLSFields } from '../contexts'; @@ -41,4 +42,4 @@ export const tlsNormalizers: TLSNormalizerMap = { export const tlsStringToObjectNormalizer = (value: string = '', key: keyof TLSFields) => value ?? defaultTLSFields[key]; export const tlsJsonToObjectNormalizer = (value: string = '', key: keyof TLSFields) => - value ? JSON.parse(value) : defaultTLSFields[key]; + value ? parseJsonIfString(value) : defaultTLSFields[key];