LogoLogo
ChangelogGitHubTwitterGitter
v3.x
v3.x
  • Marble.js
  • Getting started
    • Installation
    • Quick setup
  • HTTP
    • Effects
    • Middlewares
    • Routing
    • Errors
    • Output
    • Context
    • Advanced
      • Logging
      • Validation
      • Server Events
      • Streaming
      • Continuous mode
  • Messaging
    • Core concepts
      • Events
      • Effects
    • Microservices
      • AMQP (RabbitMQ)
      • Redis Pub/Sub
    • CQRS
    • WebSockets
  • Testing
    • HTTP routes testing
  • Other
    • How does it glue together?
    • Migration guides
      • Migration from version 2.x
      • Migration from version 1.x
    • API reference
      • core
        • bindTo
        • bindEagerlyTo
        • createEvent
        • createServer
        • combineRoutes
        • createContextToken
        • EffectFactory
        • r.pipe
        • httpListener
        • operator: matchEvent
        • operator: use
        • operator: act
      • messaging
        • eventBus
        • messagingClient
        • createMicroservice
        • reply
      • websockets
        • webSocketListener
        • operator: broadcast
        • operator: mapToServer
      • middleware-multipart
      • middleware-cors
      • middleware-joi
      • middleware-jwt
        • Token signing
      • middleware-io
      • middleware-logger
      • middleware-body
    • Style Guide
    • FAQ
Powered by GitBook
On this page
  • Installation
  • Importing
  • Type declaration
  • Parameters
  • Usage
  • Credits
  1. Other
  2. API reference

middleware-joi

A Joi validation middleware for Marble.js.

Previousmiddleware-corsNextmiddleware-jwt

Last updated 5 years ago

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

Installation

yarn add @marblejs/middleware-joi

Requires @marblejs/core to be installed.

Importing

import { validator$ } from '@marblejs/middleware-joi';

Type declaration

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

Parameters

parameter

definition

schema

Schema

options

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
import { r } from '@marblejs/core';
import { validator$, Joi } from '@marblejs/middleware-joi';

const foo$ = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    use(validator$({
      query: Joi.object({
        id: Joi.number().min(1).max(10),
      })
    }));
    // ...
  )));

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.

{
  error: {
    status: 400,
    message: '"id" is required'
  }
}

2. Example of validating all incoming requests:

app.ts
import { validator$, Joi } from '@marblejs/middleware-joi';

const middlewares = [
  // ...
  validator$(
    {
      headers: Joi.object({
        token: Joi.string().token().required(),
        accept: Joi.string().default('application/json')
      })
    },
    { stripUnknown: true },
  )
];

const effects = [
  endpoint1$,
  endpoint2$,
  ...
];

const app = httpListener({ middlewares, effects });

Credits

You can find detailed API reference for Joi schemas .

For more advanced request or event validation purposes we highly recommend to use package instead. It can better handle type inference for complex schemas.

<optional> Joi.ValidationOptions (see: )

Middleware author:

here
@marblejs/middleware-io
Lucio Rubens
Joi API reference
Joi