@marblejs-contrib/middleware-joi
A Joi validation middleware for Marble.js.
Joi is an object schema description language and validator for JavaScript objects. Using its schema language, you can validate things like:
    HTTP request headers
    HTTP body parameters
    HTTP request query parameters
    URL parameters
You can find detailed API reference for Joi schemas here.
Deprecation warning
Since version 4.0 @marblejs-contrib/middleware-joi package is deprecated and won't be maintained anymore.
Since version 4.0, the middleware is a part of contrib packages. If you really want to use this middleware you can reach it via @marblejs-contrib/middleware-joi.
For more advanced request or event validation purposes we highly recommend to use @marblejs/middleware-io package instead. It can better handle type inference for complex schemas.

Installation

1
yarn add @marblejs-contrib/middleware-joi
Copied!
Requires @marblejs/core to be installed.

Importing

1
import { validator$ } from '@marblejs-contrib/middleware-joi';
Copied!

Type declaration

1
validator$ :: (Schema, Joi.ValidationOptions) -> HttpMiddlewareEffect
Copied!

Parameters

parameter
definition
schema
Schema
options
<optional> Joi.ValidationOptions (see: Joi API reference)

Schema

parameter
definition
headers
<optional> any
params
<optional> any
query
<optional> any
body
<optional> any

Usage

1. Example of using middleware on a GET route to validate query parameters:
foo.effect.ts
1
import { r } from '@marblejs/http';
2
import { validator$, Joi } from '@marblejs-contrib/middleware-joi';
3
4
const foo$ = r.pipe(
5
r.matchPath('/'),
6
r.matchType('GET'),
7
r.useEffect(req$ => req$.pipe(
8
use(validator$({
9
query: Joi.object({
10
id: Joi.number().min(1).max(10),
11
})
12
}));
13
// ...
14
)));
Copied!
Example above will validate each incoming request connected with foo$ Effect. The validation blueprint defines that the id query parameter should be a number between <1..10>. If the schema requirements are not satisfied the middleware will throw an error with description what went wrong.
1
{
2
error: {
3
status: 400,
4
message: '"id" is required'
5
}
6
}
Copied!
2. Example of validating all incoming requests:
app.ts
1
import { validator$, Joi } from '@marblejs-contrib/middleware-joi';
2
3
const middlewares = [
4
// ...
5
validator$(
6
{
7
headers: Joi.object({
8
token: Joi.string().token().required(),
9
accept: Joi.string().default('application/json')
10
})
11
},
12
{ stripUnknown: true },
13
)
14
];
15
16
const effects = [
17
endpoint1$,
18
endpoint2$,
19
...
20
];
21
22
const app = httpListener({ middlewares, effects });
Copied!

Credits

Middleware author: Lucio Rubens
Last modified 1mo ago