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

express-validata

v0.1.3

Published

An efficient joi validator middleware

Downloads

127

Readme

express-validata logo

EXPRESS-VALIDATA - An efficient joi validator middleware

Express-validata is an efficient joi validator middleware for sanitizing inputs.

100% Free and Open Source!

What's included

  • Validates any input - It can sanitize any input defined in Joi schema

  • Inheritance - Supports inheritance. It's now time to write your joi schema in small re-usable component that can be inherited.

  • Any input source validation - it can validate any input source (e.g req.body, req.query, req.params).

Motivation

Repeating validation logic across routes isn't a good practice. Writing validation/sanitation logic alongside your business logic is not something you want to do in this era. Won't you prefer having the ability to compose your validation logic within a middleware? That's the genesis of express-validata. The ease of perfoming your validation without repeating yourself or needing to write countless lines of code just to sanitize inputs. With express-validata, you can easily leverage on a middleware that supports validating inputs from all sources ( req.body, req.query, req.params) with ability to define and re-use joi schema across routes.

Quick Start

Install express-validata:

npm i express-validata

or use yarn

yarn add express-validata

DEPENDS ON JOI - You must install and Define your schema with Joi

const express = require('express');
const bodyParser = require('body-parser');

const joi = require('joi'); // required to define schema

const { validata } = require('express-validata'); // validation middleware

// Create Express server
const app = express();

// Express configuration
app.set('port', Number(process.env.PORT) || 8080);
app.use(bodyParser.json());

// // // || JOI SCHEMA  || \\ \\
const baseSchema = joi.object({
  limit: joi.string(),
  page: joi.string(),
});

const bodySchema = joi.object({
  firstName: joi.string(),
  lastName: joi.string(),
});

const querySchema = joi.object({
  lang: joi.string(),
});

const paramsSchema = joi.object({
  id: joi.string(),
});

// // // || JOI SCHEMA  || \\ \\

app.post(
  '/test',
  validata(
    { body: bodySchema, query: querySchema, params: paramsSchema },
    {
      baseSchema: {
        query: [baseSchema],
      },
    },
  ),
  (req, res) => {
    return res.status(200).json({
      status: true,
      message: 'search successful',
      data: 'good',
    });
  },
);

app.listen(app.get('port'), () => {
  console.log(
    '  App is running at http://localhost:%d in %s mode',
    app.get('port'),
    app.get('env'),
  );
  console.log('  Press CTRL-C to stop\n');
});

Explanation

You can validate different data sources (e.g req.body, req.query, req.params) by defining joi schemas for each source. You could even define a generic schema that's re-usable across many routes and set it as baseSchema for the intended data source. These generic joi schema (re-usable joi schema component) gets concatenated to the mainSchema and therefore forms a whole enforcable joi schema

Testing

You can clone the repo and move the sandbox folder to a folder on the same level with express-validata. Run the following command in express-validata folder

npm link

Run the following command in sandbox folder

npm link express-validata

You can now play as you like in the sandbox!

How can I support the developers?

  • Star our GitHub repo :star:
  • Create pull requests, submit bugs, suggest new features or documentation updates :wrench:

License

MIT license.

From Developers

Made with :heart: by Tobbyas Techwares. Follow us on Twitter to get the latest news first! We're always happy to receive your feedback!