Skip to content

Commit

Permalink
[8.x] [Streams] Small type changes for advanced mapping parameters (#…
Browse files Browse the repository at this point in the history
…211817) (#211906)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Streams] Small type changes for advanced mapping parameters
(#211817)](#211817)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Kerry
Gallagher","email":"kerry.gallagher@elastic.co"},"sourceCommit":{"committedDate":"2025-02-20T13:21:58Z","message":"[Streams]
Small type changes for advanced mapping parameters (#211817)\n\n##
Summary\n\nSwitches to the `recursiveRecord` schema so we don't get
these in\nconsole:\n\n```\n[2025-02-19T15:47:07.556+01:00][WARN
][plugins.streams] Warning for PUT /api/streams/{name}: schema
ZodUnknown at body.stream.ingest.wired.fields is not inspectable and
could lead to runtime exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/schema/fields_simulation: schema ZodUnknown
at body.field_definitions is not inspectable and could lead to runtime
exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/processing/_simulate: schema ZodUnknown at
body.detected_fields is not inspectable and could lead to runtime
exceptions, convert it to a supported schema\n```\n\nI had to move the
schema definition / types into another file otherwise\na circular
dependency was introduced with the `fields/index.ts` file,\ncausing a
`Cannot read properties of undefined (reading '_parse')`\nerror.\n\nAs
far as I can see the `recursiveRecord` schema should handle / cover\nthe
ES `MappingProperty` type
fine.","sha":"670da35d4101fc64139876fbdf2aee5975036399","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","Feature:Streams","v9.1.0","v8.19.0"],"title":"[Streams]
Small type changes for advanced mapping
parameters","number":211817,"url":"https://github.com/elastic/kibana/pull/211817","mergeCommit":{"message":"[Streams]
Small type changes for advanced mapping parameters (#211817)\n\n##
Summary\n\nSwitches to the `recursiveRecord` schema so we don't get
these in\nconsole:\n\n```\n[2025-02-19T15:47:07.556+01:00][WARN
][plugins.streams] Warning for PUT /api/streams/{name}: schema
ZodUnknown at body.stream.ingest.wired.fields is not inspectable and
could lead to runtime exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/schema/fields_simulation: schema ZodUnknown
at body.field_definitions is not inspectable and could lead to runtime
exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/processing/_simulate: schema ZodUnknown at
body.detected_fields is not inspectable and could lead to runtime
exceptions, convert it to a supported schema\n```\n\nI had to move the
schema definition / types into another file otherwise\na circular
dependency was introduced with the `fields/index.ts` file,\ncausing a
`Cannot read properties of undefined (reading '_parse')`\nerror.\n\nAs
far as I can see the `recursiveRecord` schema should handle / cover\nthe
ES `MappingProperty` type
fine.","sha":"670da35d4101fc64139876fbdf2aee5975036399"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/211817","number":211817,"mergeCommit":{"message":"[Streams]
Small type changes for advanced mapping parameters (#211817)\n\n##
Summary\n\nSwitches to the `recursiveRecord` schema so we don't get
these in\nconsole:\n\n```\n[2025-02-19T15:47:07.556+01:00][WARN
][plugins.streams] Warning for PUT /api/streams/{name}: schema
ZodUnknown at body.stream.ingest.wired.fields is not inspectable and
could lead to runtime exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/schema/fields_simulation: schema ZodUnknown
at body.field_definitions is not inspectable and could lead to runtime
exceptions, convert it to a supported
schema\n[2025-02-19T15:47:07.557+01:00][WARN ][plugins.streams] Warning
for POST /api/streams/{name}/processing/_simulate: schema ZodUnknown at
body.detected_fields is not inspectable and could lead to runtime
exceptions, convert it to a supported schema\n```\n\nI had to move the
schema definition / types into another file otherwise\na circular
dependency was introduced with the `fields/index.ts` file,\ncausing a
`Cannot read properties of undefined (reading '_parse')`\nerror.\n\nAs
far as I can see the `recursiveRecord` schema should handle / cover\nthe
ES `MappingProperty` type
fine.","sha":"670da35d4101fc64139876fbdf2aee5975036399"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Kerry Gallagher <kerry.gallagher@elastic.co>
  • Loading branch information
kibanamachine and Kerry350 authored Feb 20, 2025
1 parent b846cc9 commit 83aa968
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,3 @@ export const streamDefinitionSchema: z.Schema<StreamDefinition> = z.union([
]);

export const isStreamDefinition = createIsNarrowSchema(z.unknown(), streamDefinitionSchema);

export type Primitive = string | number | boolean | null | undefined;

export const primitive: z.ZodType<Primitive> = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
z.undefined(),
]);

export interface RecursiveRecord {
[key: PropertyKey]: Primitive | Primitive[] | RecursiveRecord;
}

export const recursiveRecord: z.ZodType<RecursiveRecord> = z.lazy(() =>
z.record(z.union([primitive, z.array(primitive), recursiveRecord]))
);

export type FlattenRecord = Record<PropertyKey, Primitive | Primitive[]>;

export const flattenRecord: z.ZodType<FlattenRecord> = z.record(
z.union([primitive, z.array(primitive)])
);

export const sampleDocument = recursiveRecord;

export type SampleDocument = RecursiveRecord;
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './api';
export * from './core';
export * from './helpers';
export * from './group';
export * from './record_types';
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { MappingProperty } from '@elastic/elasticsearch/lib/api/types';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/zod-helpers';
import { recursiveRecord } from '../../record_types';

export const FIELD_DEFINITION_TYPES = [
'keyword',
Expand All @@ -34,7 +35,7 @@ export type FieldDefinitionConfigAdvancedParameters = Omit<
>;

export const fieldDefinitionConfigSchema: z.Schema<FieldDefinitionConfig> = z.intersection(
z.record(z.string(), z.unknown()),
recursiveRecord,
z.object({
type: z.enum(FIELD_DEFINITION_TYPES),
format: z.optional(NonEmptyString),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { z } from '@kbn/zod';

export type Primitive = string | number | boolean | null | undefined;

export const primitive: z.ZodType<Primitive> = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
z.undefined(),
]);

export interface RecursiveRecord {
[key: PropertyKey]: Primitive | Primitive[] | RecursiveRecord;
}

export const recursiveRecord: z.ZodType<RecursiveRecord> = z.lazy(() =>
z.record(z.union([primitive, z.array(primitive), recursiveRecord]))
);

export type FlattenRecord = Record<PropertyKey, Primitive | Primitive[]>;

export const flattenRecord: z.ZodType<FlattenRecord> = z.record(
z.union([primitive, z.array(primitive)])
);

export const sampleDocument = recursiveRecord;

export type SampleDocument = RecursiveRecord;

0 comments on commit 83aa968

Please sign in to comment.