npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

restify-api-validation

v1.4.13

Published

This node module is develop for restify api validation .

Downloads

179

Readme

restify-api-validation

npm install restify-api-validation --save

Contributition and enhancement to repository are most welcome.

Also you can check examples in the test folder how to write validation and see the below details how to use it. Happy coding.

Also let me know if you want enhancement . Tested with latest node.js version 10.15 +

You can use this like this

file: 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;

file: app.js

'use strict';

const restify = require("restify");
var validate = require('restify-api-validation');
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

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

If you have json structure like this

{
	"email":"[email protected]",
	"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.

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

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

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

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()
  }
}