kibana/docs/development/core/server/kibana-plugin-server.routevalidationfunction.md
Alejandro Fernández Haro f3f1bd2ec6
[NP] Allow custom validations in HTTP Routes apart from @kbn/config-schema (#51919) (#53714)
* [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>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2019-12-21 00:02:35 +00:00

1.1 KiB

Home > kibana-plugin-server > RouteValidationFunction

RouteValidationFunction type

The custom validation function if @kbn/config-schema is not a valid solution for your specific plugin requirements.

Signature:

export declare type RouteValidationFunction<T> = (data: any, validationResult: RouteValidationResultFactory) => {
    value: T;
    error?: never;
} | {
    value?: never;
    error: RouteValidationError;
};

Example

The validation should look something like:

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']);
  }
}