Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HTTP/OAS] zod support #186190

Merged
merged 43 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
51bd6b0
add support for zod in oas converter (pass #1)
jloleysens Jun 13, 2024
2591207
allow zod for response validation
jloleysens Jun 13, 2024
82aee47
use zod validation
jloleysens Jun 13, 2024
751ef07
[do not keep]
jloleysens Jun 13, 2024
ee3cd73
Fix type
maryam-saeidi Jun 24, 2024
8c45b04
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jun 24, 2024
00862c3
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jun 26, 2024
bff4536
Add test for zod OAS generation
maryam-saeidi Jun 28, 2024
f47b965
Add unit test for zod oas_conventor
maryam-saeidi Jun 28, 2024
61ad417
Revert changes to status API
maryam-saeidi Jun 28, 2024
7b79fe3
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jun 28, 2024
3ea82d7
unified utils a bit
jloleysens Jun 28, 2024
75ee9f9
remove unused funcs
jloleysens Jun 28, 2024
d2ccdb1
fix types and better colocation
jloleysens Jun 28, 2024
b18b68d
Fix issue with scripts/yarn_deduplicate script
maryam-saeidi Jul 2, 2024
72ac550
Rename generate_oas.test.shared.fixture filename
maryam-saeidi Jul 2, 2024
165f57c
Remove recursive test
maryam-saeidi Jul 2, 2024
3f52cb4
reintroduce ObjectType
jloleysens Jul 2, 2024
be536a7
Import zod from @kbn/zod
maryam-saeidi Jul 3, 2024
310e8e7
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jul 3, 2024
5a706be
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Jul 3, 2024
01c29fa
Undo changes to packages/kbn-server-route-repository/src/typings.ts
maryam-saeidi Jul 3, 2024
1f181b9
Merge branch 'oas/zod-poc' of https://github.com/elastic/kibana into …
maryam-saeidi Jul 3, 2024
061434f
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jul 3, 2024
732d380
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jul 4, 2024
6f3171d
updated isZod util and moved to to @kbn/zod
jloleysens Jul 5, 2024
b8dda79
expose util
jloleysens Jul 5, 2024
9f9416f
added integration test for zod
jloleysens Jul 5, 2024
35ad402
use @kbn/zod isZod utility in oas converter
jloleysens Jul 5, 2024
44b7370
use isZod utility in core
jloleysens Jul 5, 2024
9f22d63
address nit
jloleysens Jul 5, 2024
3a07464
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Jul 5, 2024
ec733dc
update error message
jloleysens Jul 5, 2024
749235f
migrate ZodEsque to @kbn/zod
jloleysens Jul 5, 2024
1106bca
added validator test
jloleysens Jul 5, 2024
55557e4
delete ZodEsque from route validator
jloleysens Jul 5, 2024
33b95f5
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Jul 5, 2024
598735f
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jul 8, 2024
dc37dea
Merge branch 'main' into oas/zod-poc
jloleysens Jul 12, 2024
df883f8
Merge branch 'main' into oas/zod-poc
jloleysens Jul 15, 2024
4a2301e
Merge branch 'main' into oas/zod-poc
jloleysens Jul 17, 2024
fc8e847
Merge branch 'main' into oas/zod-poc
jloleysens Jul 18, 2024
f1501bc
Merge branch 'main' into oas/zod-poc
maryam-saeidi Jul 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ packages/kbn-web-worker-stub @elastic/kibana-operations
packages/kbn-whereis-pkg-cli @elastic/kibana-operations
packages/kbn-xstate-utils @elastic/obs-ux-logs-team
packages/kbn-yarn-lock-validator @elastic/kibana-operations
packages/kbn-zod @elastic/kibana-core
packages/kbn-zod-helpers @elastic/security-detection-rule-management
####
## Everything below this line overrides the default assignments for each package.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@
"@kbn/visualizations-plugin": "link:src/plugins/visualizations",
"@kbn/watcher-plugin": "link:x-pack/plugins/watcher",
"@kbn/xstate-utils": "link:packages/kbn-xstate-utils",
"@kbn/zod": "link:packages/kbn-zod",
"@kbn/zod-helpers": "link:packages/kbn-zod-helpers",
"@langchain/community": "^0.2.4",
"@langchain/core": "0.2.3",
Expand Down Expand Up @@ -1755,7 +1756,7 @@
"xmlbuilder": "13.0.2",
"yargs": "^15.4.1",
"yarn-deduplicate": "^6.0.2",
"zod-to-json-schema": "^3.22.3"
"zod-to-json-schema": "^3.23.0"
},
"packageManager": "yarn@1.22.21"
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
RouteRegistrar,
} from '@kbn/core-http-server';
import { validBodyOutput, getRequestValidation } from '@kbn/core-http-server';
import { RouteValidator } from './validator';
import { isZod, RouteValidator } from './validator';
import { CoreVersionedRouter } from './versioned_router';
import { CoreKibanaRequest } from './request';
import { kibanaResponseFactory } from './response';
Expand Down Expand Up @@ -73,7 +73,7 @@ function routeSchemasFromRouteConfig<P, Q, B>(
if (route.validate !== false) {
const validation = getRequestValidation(route.validate);
Object.entries(validation).forEach(([key, schema]) => {
if (!(isConfigSchema(schema) || typeof schema === 'function')) {
if (!(isConfigSchema(schema) || isZod(schema) || typeof schema === 'function')) {
throw new Error(
`Expected a valid validation logic declared with '@kbn/config-schema' package or a RouteValidationFunction at key: [${key}].`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { once } from 'lodash';
import {
isFullValidatorContainer,
ZodEsque,
type RouteConfig,
type RouteMethod,
type RouteValidator,
Expand All @@ -20,7 +21,7 @@ function isStatusCode(key: string) {
}

interface ResponseValidation {
[statusCode: number]: { body: () => ObjectType | Type<unknown> };
[statusCode: number]: { body: () => ObjectType | Type<unknown> | ZodEsque<unknown> };
}

export function prepareResponseValidation(validation: ResponseValidation): ResponseValidation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { Stream } from 'stream';
import { z } from '@kbn/zod';
import { ValidationError, schema, isConfigSchema } from '@kbn/config-schema';
import type {
RouteValidationSpec,
Expand All @@ -18,6 +19,10 @@ import type {
} from '@kbn/core-http-server';
import { RouteValidationError } from '@kbn/core-http-server';

export function isZod<T>(rule: RouteValidationSpec<T>): rule is z.ZodType<T> {
return Boolean('_def' in rule && 'typeName' in (rule as { _def: {} })._def);
jloleysens marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Route validator class to define the validation logic for each new route.
*
Expand Down Expand Up @@ -119,6 +124,8 @@ export class RouteValidator<P = {}, Q = {}, B = {}> {
): T {
if (isConfigSchema(validationRule)) {
return validationRule.validate(data, {}, namespace);
} else if (isZod(validationRule)) {
maryam-saeidi marked this conversation as resolved.
Show resolved Hide resolved
return validationRule.parse(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace is not passed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is part of why Zod is not a drop-in replacement for Joi, no passing context down via the parse method.

} else if (typeof validationRule === 'function') {
return this.validateFunction(validationRule, data, namespace);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@kbn/std",
"@kbn/utility-types",
"@kbn/config-schema",
"@kbn/zod",
"@kbn/es-errors",
"@kbn/core-http-server",
"@kbn/hapi-mocks",
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type {
RouteValidatorFullConfigResponse,
LazyValidator,
RouteAccess,
ZodEsque,
} from './src/router';
export {
validBodyOutput,
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type {
RouteValidator,
RouteValidatorRequestAndResponses,
LazyValidator,
ZodEsque,
} from './route_validator';
export { RouteValidationError } from './route_validator';
export type { IRouter, RouteRegistrar, RouterRoute } from './router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,22 @@ export type RouteValidationFunction<T> = (
error: RouteValidationError;
};

export interface ZodEsque<V> {
_output: V;
}

/**
* Allowed property validation options: either @kbn/config-schema validations or custom validation functions
*
* See {@link RouteValidationFunction} for custom validation.
*
* @public
*/
export type RouteValidationSpec<T> = ObjectType | Type<T> | RouteValidationFunction<T>;
export type RouteValidationSpec<T> =
| ObjectType
| Type<T>
| ZodEsque<T>
| RouteValidationFunction<T>;

/**
* The configuration object to the RouteValidator class.
Expand Down Expand Up @@ -208,4 +216,4 @@ export type RouteValidator<P, Q, B> =
* @return A @kbn/config-schema schema
* @public
*/
export type LazyValidator = () => Type<unknown>;
export type LazyValidator = () => Type<unknown> | ZodEsque<unknown>;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading