API validation for Restify framework ( node.js )

himanshu sharma
2 min readNov 28, 2017

--

Restify-api-validation

While working on Restify framework of node.js . I come to approach how can i validate the data coming from outside to api . I explore the Restify API validator or you ay call it Restify validator . I want a module which can even work for complex api json.

Like array inside object . {xyz:[{},{}]} . And also validate each field of the api with many extra feature like email validation , length also.

So here is the module that does work for Restify Framework. It use well define module joi for validation of json api data.

You can install it like this .

npm install restify-api-validation — save

Then Lets check how it work .

allValidation.js

'use strict';
var Joi = require('joi');
var validate = {
login :{
options: { flatten: true },
body: {
email: Joi.string().required().email(),
password: Joi.string().required().min(6).max(10)
}
},
register : {
options: { flatten: true },
body: {
email: Joi.string().required().email(),
password: Joi.string().required().min(6).max(10)
}
}};
module.exports.validate =validate;

app.js

'use strict';
const restify = require('restify');
var validate = require('restify-api-validation');
var http = require('http');
var validation = require('./allValidator').validate;
global.httpErrors = restify.errors;
global.server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({
mapParams: false
}));
server.use(restify.plugins.fullResponse());
server.on('restifyError', function(req, res, err, next) {
// handle all errors passed to next here, whether it's Error or NotFoundError or anything that is an instance of Error
res.status(err.status);
res.json(err.errors); //res.send(err);
});
server.listen(3000, function () {
'use strict';
console.log('server is up at 3000');
});
// generates a response function sending back to user the specified req[key]
function respondWith (key) {
return function (req, res) {
res.json(req[key]);
};
}
function respond200 (req, res) {
res.json(200);
}
server.post('/login', validate(validation.login), respond200);
server.post('/register', validate(validation.register), respond200);
// default errorhandler for express-validation
server.use(function (err, req, res, next) {
res.status(400).json(err);
});
module.exports = server;

#Make sure to use this for error catching , tested in restify 5

```javascript
server.on('restifyError', function(req, res, err, next) {
// handle all errors passed to next here, whether it's Error or
//NotFoundError or anything that is an instance of Error
res.status(err.status);
res.json(err.errors); //res.send(err);
});
```

For more complex structure like this you can do validation. Like if you want validation for more complex api json you can check this out also . This validate all the object inside the array .

If you have json structure like this

{
"email":"himmsharma.99@gmail.com",
"password":"12345",
"userinfo":[
{ "nickname":"himanshu"},
{"nickname":"himm"}],
"complexinfo":{
"name":[
{"firstname":"ram"},
{"firstname":"shyam"}]
}
}

Then you can make validation json in the allValidation.js file like this.
info is the object like login and register in allValidation.js file . allValidation.js file is above.

You can take any name not necessary allValidation.js should be the only file name.


‘use strict’;
var nickname = Joi.object().keys({
nickname: Joi.string().required()
});

var firstname = Joi.object().keys({
firstname: Joi.string().required()
});

var Joi = require(‘joi’);
var validate = {
info : {
options: { flatten: true ,contextRequest : true},
headers: {
authorization: Joi.string().required()
},
body: {
email: Joi.string().required().email(),
userinfo :Joi.array().items(nickname).required(),
complexinfo :Joi.object().keys({
name: Joi.array().items(firstname).required()
}).required()
}
}

While working with header reminder to write in `lower case` letter.

Github repo

--

--