-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NP] Allow custom validations in HTTP Routes apart from @kbn/config-s…
…chema (#51919) * [NP] Allow custom validations in HTTP Routes apart from @kbn/config-schema * API docs * Allow validate function in the route handler (run-code validation) * Prefix RouteXXX + Params and Body Validation Aliases * Fix test broken by lodash * Update API docs * Add default types for simpler manual declaration * Add run-time validation of the RouteValidateSpec * Expose RouteValidationError instead of SchemaTypeError * RouteValidator as a class to match config-schema interface * Test for not-inline handler (need to check IRouter for #47047) * Add preValidation of the input for a safer custom validation * Better types for RouteHandlers * [NP] Move route validation to RouteValidator wrapper * Use the class only internally but maintain the same API * Fix types * Ensure RouteValidator instance in KibanaRequest.from * Fix validator.tests (Buffer.from instead of new Buffer) * Default precheck should allow null values * Also allow undefined in preChecks * MR feedback fixes * Provide RouteValidationResolver to the validation function * Add functional tests * Fix new functional tests * Fix validator additional test * Fix test with new resolver * Remove unused import * Rename ValidationResolver to ValidationResultFactory and change the interface to look more like the KibanaResponseFactory Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
e582277
commit 3bdbcd0
Showing
60 changed files
with
1,369 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
docs/development/core/server/kibana-plugin-server.routeschemas.body.md
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
docs/development/core/server/kibana-plugin-server.routeschemas.md
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
docs/development/core/server/kibana-plugin-server.routeschemas.params.md
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
docs/development/core/server/kibana-plugin-server.routeschemas.query.md
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
...elopment/core/server/kibana-plugin-server.routevalidationerror._constructor_.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationError](./kibana-plugin-server.routevalidationerror.md) > [(constructor)](./kibana-plugin-server.routevalidationerror._constructor_.md) | ||
|
||
## RouteValidationError.(constructor) | ||
|
||
Constructs a new instance of the `RouteValidationError` class | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
constructor(error: Error | string, path?: string[]); | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| error | <code>Error | string</code> | | | ||
| path | <code>string[]</code> | | | ||
|
20 changes: 20 additions & 0 deletions
20
docs/development/core/server/kibana-plugin-server.routevalidationerror.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationError](./kibana-plugin-server.routevalidationerror.md) | ||
|
||
## RouteValidationError class | ||
|
||
Error to return when the validation is not successful. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare class RouteValidationError extends SchemaTypeError | ||
``` | ||
## Constructors | ||
| Constructor | Modifiers | Description | | ||
| --- | --- | --- | | ||
| [(constructor)(error, path)](./kibana-plugin-server.routevalidationerror._constructor_.md) | | Constructs a new instance of the <code>RouteValidationError</code> class | | ||
42 changes: 42 additions & 0 deletions
42
docs/development/core/server/kibana-plugin-server.routevalidationfunction.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationFunction](./kibana-plugin-server.routevalidationfunction.md) | ||
|
||
## RouteValidationFunction type | ||
|
||
The custom validation function if @<!-- -->kbn/config-schema is not a valid solution for your specific plugin requirements. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type RouteValidationFunction<T> = (data: any, validationResult: RouteValidationResultFactory) => { | ||
value: T; | ||
error?: never; | ||
} | { | ||
value?: never; | ||
error: RouteValidationError; | ||
}; | ||
``` | ||
|
||
## Example | ||
|
||
The validation should look something like: | ||
|
||
```typescript | ||
interface MyExpectedBody { | ||
bar: string; | ||
baz: number; | ||
} | ||
|
||
const myBodyValidation: RouteValidationFunction<MyExpectedBody> = (data, validationResult) => { | ||
const { ok, badRequest } = validationResult; | ||
const { bar, baz } = data || {}; | ||
if (typeof bar === 'string' && typeof baz === 'number') { | ||
return ok({ bar, baz }); | ||
} else { | ||
return badRequest('Wrong payload', ['body']); | ||
} | ||
} | ||
|
||
``` | ||
|
13 changes: 13 additions & 0 deletions
13
...ent/core/server/kibana-plugin-server.routevalidationresultfactory.badrequest.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationResultFactory](./kibana-plugin-server.routevalidationresultfactory.md) > [badRequest](./kibana-plugin-server.routevalidationresultfactory.badrequest.md) | ||
|
||
## RouteValidationResultFactory.badRequest property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
badRequest: (error: Error | string, path?: string[]) => { | ||
error: RouteValidationError; | ||
}; | ||
``` |
23 changes: 23 additions & 0 deletions
23
docs/development/core/server/kibana-plugin-server.routevalidationresultfactory.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationResultFactory](./kibana-plugin-server.routevalidationresultfactory.md) | ||
|
||
## RouteValidationResultFactory interface | ||
|
||
Validation result factory to be used in the custom validation function to return the valid data or validation errors | ||
|
||
See [RouteValidationFunction](./kibana-plugin-server.routevalidationfunction.md)<!-- -->. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface RouteValidationResultFactory | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [badRequest](./kibana-plugin-server.routevalidationresultfactory.badrequest.md) | <code>(error: Error | string, path?: string[]) => {</code><br/><code> error: RouteValidationError;</code><br/><code> }</code> | | | ||
| [ok](./kibana-plugin-server.routevalidationresultfactory.ok.md) | <code><T>(value: T) => {</code><br/><code> value: T;</code><br/><code> }</code> | | | ||
|
13 changes: 13 additions & 0 deletions
13
...development/core/server/kibana-plugin-server.routevalidationresultfactory.ok.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationResultFactory](./kibana-plugin-server.routevalidationresultfactory.md) > [ok](./kibana-plugin-server.routevalidationresultfactory.ok.md) | ||
|
||
## RouteValidationResultFactory.ok property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
ok: <T>(value: T) => { | ||
value: T; | ||
}; | ||
``` |
15 changes: 15 additions & 0 deletions
15
docs/development/core/server/kibana-plugin-server.routevalidationspec.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidationSpec](./kibana-plugin-server.routevalidationspec.md) | ||
|
||
## RouteValidationSpec type | ||
|
||
Allowed property validation options: either @<!-- -->kbn/config-schema validations or custom validation functions | ||
|
||
See [RouteValidationFunction](./kibana-plugin-server.routevalidationfunction.md) for custom validation. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type RouteValidationSpec<T> = ObjectType | Type<T> | RouteValidationFunction<T>; | ||
``` |
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-server.routevalidatorconfig.body.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [RouteValidatorConfig](./kibana-plugin-server.routevalidatorconfig.md) > [body](./kibana-plugin-server.routevalidatorconfig.body.md) | ||
|
||
## RouteValidatorConfig.body property | ||
|
||
Validation logic for the body payload | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
body?: RouteValidationSpec<B>; | ||
``` |
Oops, something went wrong.