1.1.0 • Published 6 years ago

tcomb-validation-middleware v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Tcomb Validation Middleware

Express Middleware that performs runtime type checking of objects. It is primerily meant for type checking in development.

Build CircleCI

Tests Coverage Status

Installation

npm i tcomb-validation-middleware

Usage

Use the library tcomb to define the model of the data structure

const t = require('tcomb');

const test = t.struct({
  test: t.String,
  bool: t.Boolean,
});

module.exports = {
  test,
};

Then add as middleware to the route of the endpoint

const t = require('tcomb-validation-middleware');

... //Express setup stuff

app.post('/test', t(test), (req, res) => {
  res.send({ message: 'schema was obviously valid' });
});

The module also accepts an options object like so

t(test,{ active: true, verboseErrors: false })

Tests

To run the tests do

npm test

Performance

Applying strict type validation to your server will decrease it's performance slightly under heavy loads but the tradeoff of very usefull error messages can make it worth it. If in production in an environment where you control the client it's very easy to reuse your tcomb schemas. This allows you to move the validation client side without much development overhead.

The results in the table below are generated using artillery.

First HeaderScenarios launchedScenarios completedRequests completedRPS sentRequest latencyminmaxmedianp95p99Scenario durationminmaxmedianp95p99
Enabled1000100020000970.87->0.341.40.42.811.7->14.5447.218.798384.6
Disabled1000100020000970.4->0.224.20.41.63.5->11124.816.648.6108.1

If you would like to verify them clone the module from git then run the following commands.

cd express-tcomb-validation-middleware
npm install
node ./test/lib/index.js

Then in another window run

npm install -g artillery
artillery quick --count 1000 -n 20 http://localhost:4444/test/on/test/hi
artillery quick --count 1000 -n 20 http://localhost:4444/test/off/test/hi

Built with

Contributing

Make a github issue if you have any feature requests or problems. I welcome pull requests as long as they have the relevent tests included.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago