Kibana plugin. Is it possible to use joi validation in router?

the schema from @kibana/config-schema has too few features

yes, validation supports a custom validation function https://github.com/elastic/kibana/blob/4c49d5d1be0b959be515307131c1a420226c372b/src/core/server/http/http_server.test.ts#L454-L460

Just for our information, which joi feature do you need to use that is not supported by config-schema?

First of all, config-schema is not documented or I couldn't find the documentation.
In practice, I need more specific definitions of the schema of such entities as a string, for example. What string is this? Alphanumeric? Hostname? IP address? Email address?
Joi has a very large set of definitions. I can add Joi to my dependencies, import it, and use Joi.validate inside the route handle. But the code in this case is not so clear.

First of all, config-schema is not documented or I couldn't find the documentation

The documentation for config-schema is available in the package's readme: https://github.com/elastic/kibana/blob/9318862f1967d6fefdfca5e94417ff1617e5ba06/packages/kbn-config-schema/README.md

I need more specific definitions of the schema of such entities as a string, for example. What string is this? Alphanumeric? Hostname? IP address? Email address

We do have specific types for string, ip and uri, but we don't have out-of the box types for email or alphanum for example.

Now, all config-schema types support a custom validate option, that would answer this need:

const alphanum = schema.string({
   validate: (value) => {
      if(!alphanumRegexp.test(value)) {
          return 'my error message';
      }
   }
})

That being said, if you do want to use another validation lib such as joi, it is possible, as the route validation supports custom validation functions, as @Mikhail_Shustov mentioned

router.post(
    {
      path: '/',
      validate: {
        body: (body= {}, { ok, badRequest }) => {
           const { value, error } = myJoiSchema.validate(body);
           if (error) return badRequest(error.msg, ['body']);
           return ok(body);
        },
      },
    },
    handler
  );

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.