mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[EEM] Convert route validation to Zod (#188691)
## Summary This PR closes https://github.com/elastic/kibana/issues/188171 by converting the route validate to Zod for `get`, `reset`, and `delete` APIs. This also changes the validation for the `create` API to use `buildRouteValidationWithZod` along with adding `strict()` to each of the schemas. Closes https://github.com/elastic/elastic-entity-model/issues/103 --------- Co-authored-by: Kevin Lacabane <kevin.lacabane@elastic.co>
This commit is contained in:
parent
8fb8c27fac
commit
375c6ffd61
8 changed files with 58 additions and 24 deletions
|
@ -8,3 +8,6 @@
|
|||
export * from './src/schema/entity_definition';
|
||||
export * from './src/schema/entity';
|
||||
export * from './src/schema/common';
|
||||
export * from './src/rest_spec/delete';
|
||||
export * from './src/rest_spec/reset';
|
||||
export * from './src/rest_spec/get';
|
||||
|
|
16
x-pack/packages/kbn-entities-schema/src/rest_spec/delete.ts
Normal file
16
x-pack/packages/kbn-entities-schema/src/rest_spec/delete.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* 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 'zod';
|
||||
|
||||
export const deleteEntityDefinitionParamsSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
||||
|
||||
export const deleteEntityDefinitionQuerySchema = z.object({
|
||||
deleteData: z.optional(z.coerce.boolean().default(false)),
|
||||
});
|
13
x-pack/packages/kbn-entities-schema/src/rest_spec/get.ts
Normal file
13
x-pack/packages/kbn-entities-schema/src/rest_spec/get.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* 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 'zod';
|
||||
|
||||
export const getEntityDefinitionQuerySchema = z.object({
|
||||
page: z.optional(z.coerce.number()),
|
||||
perPage: z.optional(z.coerce.number()),
|
||||
});
|
11
x-pack/packages/kbn-entities-schema/src/rest_spec/reset.ts
Normal file
11
x-pack/packages/kbn-entities-schema/src/rest_spec/reset.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* 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 'zod';
|
||||
|
||||
export const resetEntityDefinitionParamsSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import { RequestHandlerContext } from '@kbn/core/server';
|
||||
import { EntityDefinition, entityDefinitionSchema } from '@kbn/entities-schema';
|
||||
import { stringifyZodError } from '@kbn/zod-helpers';
|
||||
import { buildRouteValidationWithZod } from '@kbn/zod-helpers';
|
||||
import { SetupRouteOptions } from '../types';
|
||||
import { EntityIdConflict } from '../../lib/entities/errors/entity_id_conflict_error';
|
||||
import { EntitySecurityException } from '../../lib/entities/errors/entity_security_exception';
|
||||
|
@ -23,13 +23,7 @@ export function createEntityDefinitionRoute<T extends RequestHandlerContext>({
|
|||
{
|
||||
path: '/internal/entities/definition',
|
||||
validate: {
|
||||
body: (body, res) => {
|
||||
try {
|
||||
return res.ok(entityDefinitionSchema.parse(body));
|
||||
} catch (e) {
|
||||
return res.badRequest(stringifyZodError(e));
|
||||
}
|
||||
},
|
||||
body: buildRouteValidationWithZod(entityDefinitionSchema.strict()),
|
||||
},
|
||||
},
|
||||
async (context, req, res) => {
|
||||
|
|
|
@ -6,7 +6,11 @@
|
|||
*/
|
||||
|
||||
import { RequestHandlerContext } from '@kbn/core/server';
|
||||
import { schema } from '@kbn/config-schema';
|
||||
import { buildRouteValidationWithZod } from '@kbn/zod-helpers';
|
||||
import {
|
||||
deleteEntityDefinitionParamsSchema,
|
||||
deleteEntityDefinitionQuerySchema,
|
||||
} from '@kbn/entities-schema';
|
||||
import { SetupRouteOptions } from '../types';
|
||||
import { EntitySecurityException } from '../../lib/entities/errors/entity_security_exception';
|
||||
import { InvalidTransformError } from '../../lib/entities/errors/invalid_transform_error';
|
||||
|
@ -22,12 +26,8 @@ export function deleteEntityDefinitionRoute<T extends RequestHandlerContext>({
|
|||
{
|
||||
path: '/internal/entities/definition/{id}',
|
||||
validate: {
|
||||
params: schema.object({
|
||||
id: schema.string(),
|
||||
}),
|
||||
query: schema.object({
|
||||
deleteData: schema.maybe(schema.boolean({ defaultValue: false })),
|
||||
}),
|
||||
params: buildRouteValidationWithZod(deleteEntityDefinitionParamsSchema.strict()),
|
||||
query: buildRouteValidationWithZod(deleteEntityDefinitionQuerySchema.strict()),
|
||||
},
|
||||
},
|
||||
async (context, req, res) => {
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
*/
|
||||
|
||||
import { RequestHandlerContext } from '@kbn/core/server';
|
||||
import { schema } from '@kbn/config-schema';
|
||||
import { buildRouteValidationWithZod } from '@kbn/zod-helpers';
|
||||
import { getEntityDefinitionQuerySchema } from '@kbn/entities-schema';
|
||||
import { SetupRouteOptions } from '../types';
|
||||
import { findEntityDefinitions } from '../../lib/entities/find_entity_definition';
|
||||
|
||||
|
@ -17,10 +18,7 @@ export function getEntityDefinitionRoute<T extends RequestHandlerContext>({
|
|||
{
|
||||
path: '/internal/entities/definition',
|
||||
validate: {
|
||||
query: schema.object({
|
||||
page: schema.maybe(schema.number()),
|
||||
perPage: schema.maybe(schema.number()),
|
||||
}),
|
||||
query: buildRouteValidationWithZod(getEntityDefinitionQuerySchema.strict()),
|
||||
},
|
||||
},
|
||||
async (context, req, res) => {
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
*/
|
||||
|
||||
import { RequestHandlerContext } from '@kbn/core/server';
|
||||
import { schema } from '@kbn/config-schema';
|
||||
import { buildRouteValidationWithZod } from '@kbn/zod-helpers';
|
||||
import { resetEntityDefinitionParamsSchema } from '@kbn/entities-schema';
|
||||
import { SetupRouteOptions } from '../types';
|
||||
import { EntitySecurityException } from '../../lib/entities/errors/entity_security_exception';
|
||||
import { InvalidTransformError } from '../../lib/entities/errors/invalid_transform_error';
|
||||
|
@ -39,9 +40,7 @@ export function resetEntityDefinitionRoute<T extends RequestHandlerContext>({
|
|||
{
|
||||
path: '/internal/entities/definition/{id}/_reset',
|
||||
validate: {
|
||||
params: schema.object({
|
||||
id: schema.string(),
|
||||
}),
|
||||
params: buildRouteValidationWithZod(resetEntityDefinitionParamsSchema.strict()),
|
||||
},
|
||||
},
|
||||
async (context, req, res) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue