Import zod from @kbn/zod and add an eslint rule (#190581)

Closes #187638

## Summary

In this [PR](https://github.com/elastic/kibana/pull/186190), we
introduced @kbn/zod package and an OAS convertor to automatically
generate Open API Specifications for the routes that use zod for their
validation. In this PR, we add an eslint rule to enforce importing from
@kbn/zod instead of zod directly.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Maryam Saeidi 2024-08-21 11:14:55 +02:00 committed by GitHub
parent d9da7f6f16
commit 51736afd24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
336 changed files with 367 additions and 336 deletions

View file

@ -115,6 +115,12 @@ module.exports = {
disallowedMessage: `import from @kbn/i18n-react instead`,
exclude: [/packages[\/\\]kbn-i18n-react[\/\\]/],
},
{
from: 'zod',
to: '@kbn/zod',
disallowedMessage: `import from @kbn/zod instead`,
exclude: [/packages[\/\\]kbn-zod[\/\\]/],
},
{
from: 'styled-components',
to: false,

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type PlatformErrorResponse = z.infer<typeof PlatformErrorResponse>;
export const PlatformErrorResponse = z.object({

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
/**
* A string that is not empty and does not contain only whitespace

View file

@ -6,5 +6,7 @@
"exclude": ["target/**/*"],
"extends": "../../tsconfig.base.json",
"include": ["**/*.ts"],
"kbn_references": []
"kbn_references": [
"@kbn/zod",
]
}

View file

@ -67,7 +67,7 @@ By default it uses the `zod_operation_schema` template which produces runtime ty
Example of generated code:
```ts
import { z } from 'zod';
import { z } from '@kbn/zod';
/*
* NOTICE: Do not edit this file manually.

View file

@ -210,7 +210,7 @@ components:
Running OpenAPI Generator on that file will produce the following `.gen.ts` file
```ts
import { z } from 'zod';
import { z } from '@kbn/zod';
export type EntityId = z.infer<typeof EntityId>;
export const EntityId = z.string();

View file

@ -7,13 +7,13 @@
{{> disclaimer}}
import type { ZodTypeDef } from 'zod';
import { z } from 'zod';
import type { ZodTypeDef } from '@kbn/zod';
import { z } from '@kbn/zod';
import { requiredOptional, isValidDateMath, ArrayFromString, BooleanFromString } from '@kbn/zod-helpers';
{{#each imports}}
import {
{{#each this}}{{.}},{{/each}}
import {
{{#each this}}{{.}},{{/each}}
} from "{{@key}}"
{{/each}}

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { schema } from '@kbn/config-schema';
export const sharedOas = {

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { EndpointList } from '../model/endpoint_list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemHumanId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import { EndpointListItem } from '../model/endpoint_list_common.gen';

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionList,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemId,

View file

@ -6,5 +6,9 @@
"exclude": ["target/**/*"],
"extends": "../../tsconfig.base.json",
"include": ["**/*.ts"],
"kbn_references": ["@kbn/securitysolution-exceptions-common", "@kbn/openapi-common"]
"kbn_references": [
"@kbn/securitysolution-exceptions-common",
"@kbn/openapi-common",
"@kbn/zod",
]
}

View file

@ -9,7 +9,7 @@
import { errors } from '@elastic/elasticsearch';
import Boom from '@hapi/boom';
import { stringifyZodError } from '@kbn/zod-helpers';
import { ZodError } from 'zod';
import { ZodError } from '@kbn/zod';
import { BadRequestError } from '../bad_request_error';
export interface OutputError {

View file

@ -15,5 +15,6 @@
],
"kbn_references": [
"@kbn/zod-helpers",
"@kbn/zod",
]
}

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListHumanId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import {

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { UUID, NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import {

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListName,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListHumanId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { ExceptionNamespaceType, ExceptionList } from '../model/exception_list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { BooleanFromString } from '@kbn/zod-helpers';
import {

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import { ExceptionListItemEntryArray } from './exception_list_item_entry.gen';

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import { ListId, ListType } from '@kbn/securitysolution-lists-common/api/model/list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListItemId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ExceptionListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import {

View file

@ -9,6 +9,7 @@
"kbn_references": [
"@kbn/openapi-common",
"@kbn/zod-helpers",
"@kbn/securitysolution-lists-common"
"@kbn/securitysolution-lists-common",
"@kbn/zod"
]
}

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type CreateListIndexResponse = z.infer<typeof CreateListIndexResponse>;
export const CreateListIndexResponse = z.object({

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListItemId, ListId, ListItemValue, ListItemMetadata } from '../model/list_common.gen';
import { ListItem } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { BooleanFromString } from '@kbn/zod-helpers';
import { ListId } from '../model/list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type DeleteListIndexResponse = z.infer<typeof DeleteListIndexResponse>;
export const DeleteListIndexResponse = z.object({

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId } from '../model/list_common.gen';
import { ListItem } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId } from '../model/list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import { ListId } from '../model/list_common.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';
import { List } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId, ListType } from '../model/list_common.gen';
import { List } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '@kbn/openapi-common/schemas/primitives.gen';

View file

@ -15,7 +15,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ListId,

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId, ListName, ListDescription, ListMetadata } from '../model/list_common.gen';
import { List } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListItemId, ListItemValue, ListItemMetadata } from '../model/list_common.gen';
import { ListItem } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId } from '../model/list_common.gen';
import { List } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type ReadListIndexResponse = z.infer<typeof ReadListIndexResponse>;
export const ReadListIndexResponse = z.object({

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId } from '../model/list_common.gen';
import { ListItem } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type ListPrivileges = z.infer<typeof ListPrivileges>;
export const ListPrivileges = z.object({

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListId, ListName, ListDescription, ListMetadata } from '../model/list_common.gen';
import { List } from '../model/list_schemas.gen';

View file

@ -15,7 +15,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ListItemId, ListItemValue, ListItemMetadata } from '../model/list_common.gen';
import { ListItem } from '../model/list_schemas.gen';

View file

@ -6,5 +6,9 @@
"exclude": ["target/**/*"],
"extends": "../../tsconfig.base.json",
"include": ["**/*.ts"],
"kbn_references": ["@kbn/zod-helpers", "@kbn/openapi-common"]
"kbn_references": [
"@kbn/zod-helpers",
"@kbn/openapi-common",
"@kbn/zod",
]
}

View file

@ -7,7 +7,7 @@
*/
import { ArrayFromString } from './array_from_string';
import * as z from 'zod';
import * as z from '@kbn/zod';
describe('ArrayFromString', () => {
const itemsSchema = z.string();

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import * as z from 'zod';
import * as z from '@kbn/zod';
/**
* This is a helper schema to convert comma separated strings to arrays. Useful

View file

@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import * as z from 'zod';
import * as z from '@kbn/zod';
/**
* This is a helper schema to convert a boolean string ("true" or "false") to a

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import type { TypeOf, ZodType } from 'zod';
import type { TypeOf, ZodType } from '@kbn/zod';
import type { RouteValidationFunction, RouteValidationResultFactory } from '@kbn/core/server';
import { stringifyZodError } from './stringify_zod_error';

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import type { SafeParseError, SafeParseReturnType } from 'zod';
import type { SafeParseError, SafeParseReturnType } from '@kbn/zod';
export function expectParseError<Input, Output>(
result: SafeParseReturnType<Input, Output>

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import type { SafeParseReturnType, SafeParseSuccess } from 'zod';
import type { SafeParseReturnType, SafeParseSuccess } from '@kbn/zod';
import { stringifyZodError } from './stringify_zod_error';
export function expectParseSuccess<Input, Output>(

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import * as z from 'zod';
import * as z from '@kbn/zod';
import dateMath from '@kbn/datemath';
function validateDateMath(time: string): boolean {

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import * as z from 'zod';
import * as z from '@kbn/zod';
/**
* Safely parse a payload against a schema, returning the output or undefined.

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { ZodError, ZodIssue } from 'zod';
import { ZodError, ZodIssue } from '@kbn/zod';
const MAX_ERRORS = 5;

View file

@ -6,5 +6,9 @@
"exclude": ["target/**/*"],
"extends": "../../tsconfig.base.json",
"include": ["**/*.ts"],
"kbn_references": ["@kbn/datemath", "@kbn/core"]
"kbn_references": [
"@kbn/datemath",
"@kbn/core",
"@kbn/zod",
]
}

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '../common_attributes.gen';
import { Replacements } from '../conversations/common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { SortOrder } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '../common_attributes.gen';
import { AttackDiscoveryResponse } from './common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString, User } from '../common_attributes.gen';
import { Replacements, ApiConfig } from '../conversations/common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '../common_attributes.gen';
import { AttackDiscoveryResponse, AttackDiscoveryStat } from './common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { AnonymizationFieldResponse } from '../anonymization_fields/bulk_crud_anonymization_fields_route.gen';
import { ApiConfig, Replacements } from '../conversations/common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type GetCapabilitiesResponse = z.infer<typeof GetCapabilitiesResponse>;
export const GetCapabilitiesResponse = z.object({

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type RootContext = z.infer<typeof RootContext>;
export const RootContext = z.literal('security');

View file

@ -14,7 +14,7 @@ import type {
RouteValidationResultFactory,
RouteValidationError,
} from '@kbn/core/server';
import type { TypeOf, ZodType } from 'zod';
import type { TypeOf, ZodType } from '@kbn/zod';
import { stringifyZodError } from '@kbn/zod-helpers';
type RequestValidationResult<T> =

View file

@ -14,7 +14,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
/**
* A string that is not empty and does not contain only whitespace

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString } from '../common_attributes.gen';
import { ConversationMessageCreateProps, ConversationResponse } from './common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ConversationCreateProps,

View file

@ -14,7 +14,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString, User } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
ConversationCreateProps,

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { SortOrder } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export type GetEvaluateResponse = z.infer<typeof GetEvaluateResponse>;
export const GetEvaluateResponse = z.object({

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { Replacements } from '../conversations/common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
KnowledgeBaseEntryCreateProps,

View file

@ -14,7 +14,7 @@
* version: not applicable
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString, User } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
/**
* AI assistant KnowledgeBase.

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import {
KnowledgeBaseEntryCreateProps,

View file

@ -14,7 +14,7 @@
* version: 1
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { SortOrder } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { NonEmptyString, User } from '../common_attributes.gen';

View file

@ -14,7 +14,7 @@
* version: 2023-10-31
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { ArrayFromString } from '@kbn/zod-helpers';
import { SortOrder } from '../common_attributes.gen';

View file

@ -20,5 +20,6 @@
"@kbn/securitysolution-io-ts-utils",
"@kbn/core",
"@kbn/logging",
"@kbn/zod",
]
}

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export const createEntityDefinitionQuerySchema = z.object({
installOnly: z.optional(z.coerce.boolean()).default(false),

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export const deleteEntityDefinitionParamsSchema = z.object({
id: z.string(),

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export const getEntityDefinitionQuerySchema = z.object({
page: z.optional(z.coerce.number()),

View file

@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
export const resetEntityDefinitionParamsSchema = z.object({
id: z.string(),

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import moment from 'moment';
export const arrayOfStringsSchema = z.array(z.string());

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from 'zod';
import { z } from '@kbn/zod';
import { arrayOfStringsSchema } from './common';
export const entityBaseSchema = z.object({

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