@micromq/expect

1.0.2 • Public • Published

@micromq/expect

Simple middleware for validate params.

Install

$ npm i @micromq/expect

Tests

$ npm test

Usage

const MicroMQ = require('micromq');
const paramsCollector = require('@micromq/params-collector');
const expect = require('@micromq/expect');

const app = new MicroMQ({
  name: process.env.MICROSERVICE_NAME,
  rabbit: {
    url: process.env.RABBIT_URL,
  },
});

app.use(paramsCollector);

app.use(async (req, res, next) => {
  try {
    await next();
  } catch (err) {
    console.error(err.status);    // 400
    console.error(err.message);   // 'city is invalid'
  }
});

app.post(
  '/create',
  expect({
    // required, string type
    name: String,
    
    // required, string/number type, must be greater than 18
    age: {
      // may a string or a number
      type: [String, Number],
      required: true,
      validate: age => +age > 18,
    },
    
    // not required, string type, length must be greater than 10
    city: {
      type: String,
      validate: city => city.length > 10,
    },
  }),
  (req, res) => {
    // creating something good...
  },
);

app.start();

Package Sidebar

Install

npm i @micromq/expect

Weekly Downloads

1

Version

1.0.2

License

Apache-2.0

Unpacked Size

4.59 kB

Total Files

6

Last publish

Collaborators

  • bifot