All Projects → naveedn → vayder

naveedn / vayder

Licence: MIT license
Easy and concise validations for Express routes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vayder

node-screenlogic
Pentair ScreenLogic Javascript library using Node.JS
Stars: ✭ 38 (+46.15%)
Mutual labels:  node-js, node-module, nodejs-modules
FilterInputJs
Tiny and Powerful Library for limit an entry (text box,input) as number,string or more...
Stars: ✭ 37 (+42.31%)
Mutual labels:  validator, validate, validation-library
Vee Validate
✅ Form Validation for Vue.js
Stars: ✭ 8,820 (+33823.08%)
Mutual labels:  validator, validate, validation-library
ifto
A simple debugging module for AWS Lambda (λ) timeout
Stars: ✭ 72 (+176.92%)
Mutual labels:  node-js, node-module
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+3826.92%)
Mutual labels:  validator, validate
Validot
Validot is a performance-first, compact library for advanced model validation. Using a simple declarative fluent interface, it efficiently handles classes, structs, nested members, collections, nullables, plus any relation or combination of them. It also supports translations, custom logic extensions with tests, and DI containers.
Stars: ✭ 198 (+661.54%)
Mutual labels:  validator, validation-library
Validator.js
⁉️轻量级的 JavaScript 表单验证,字符串验证。没有依赖,支持 UMD ,~3kb。
Stars: ✭ 486 (+1769.23%)
Mutual labels:  validator, validate
volder
volder is powerful Object schema validation lets you describe your data using a simple and readable schema and transform a value to match the requirements
Stars: ✭ 106 (+307.69%)
Mutual labels:  validator, validation-library
reach-schema
Functional schema-driven JavaScript object validation library.
Stars: ✭ 34 (+30.77%)
Mutual labels:  validate, validation-library
teaching-nodejs-expressjs-framework-spring-2019-2020
Complete Node-Express Application
Stars: ✭ 16 (-38.46%)
Mutual labels:  node-js, node-module
biguint-format
Node.js module to format big uint numbers from a byte array or a Buffer
Stars: ✭ 16 (-38.46%)
Mutual labels:  node-js, node-module
valite
🔥 Concurrently execute your validators in a simple, practical and light validator engine.
Stars: ✭ 20 (-23.08%)
Mutual labels:  validator, validate
Fastest Validator
⚡️ The fastest JS validator library for NodeJS
Stars: ✭ 923 (+3450%)
Mutual labels:  validator, validate
napi-thread-safe-callback
C++ utility class to perform callbacks into JavaScript from any thread
Stars: ✭ 62 (+138.46%)
Mutual labels:  node-js, nodejs-modules
Nice Validator
Simple, smart and pleasant validation solution.
Stars: ✭ 587 (+2157.69%)
Mutual labels:  validator, validate
cron-validate
A cron-expression validator for TypeScript/JavaScript projects.
Stars: ✭ 40 (+53.85%)
Mutual labels:  validator, validation-library
national-code
Simple implementation of Iranian national code validation
Stars: ✭ 31 (+19.23%)
Mutual labels:  validator, validation-library
Validator.js
String validation
Stars: ✭ 18,842 (+72369.23%)
Mutual labels:  validator, validate
Validate
⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。
Stars: ✭ 378 (+1353.85%)
Mutual labels:  validator, validate
ATGValidator
iOS validation framework with form validation support
Stars: ✭ 51 (+96.15%)
Mutual labels:  validator, validation-library

VAYDER

A library that allows you to validate inputs on express routes via middlewares. It leverages the power of Joi and Celebrate with an easy, fluent interface for the middlewares to make it effortless to build a robust validation layer on your Express server.

How to Use

1) Install vayder

npm install --save vayder

2) Create a JOI schema

// schema.js

const Joi = require('joi');

module.exports = Joi.object().keys({
  foo: Joi.string().required(),
  bar: Joi.number().min(10).max(20),
});

3) Add it to your Express route

// app.js

const express = require('express');
const vayder = require('vayder');

const blahSchema = require('./schema.js');
const app = express();


app.post('/blah',
  vayder.validateBody(blahSchema),
  (req, res) => { res.send('Hello World!');}
);

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

API:

method description inputs
validateBody will validate the body of the request against the provided Joi Schema Joi Schema, Joi Schema Options [optional]
validateParams will validate the URL parameters passed into the request against the provided Joi Schema Joi Schema, Joi Schema Options [optional]
validateQuery will validate the request query string against the provided Joi Schema Joi Schema, Joi Schema Options [optional]
validateHeaders will validate the request Headers against the provided Joi Schema Joi schema, Joi Schema Options [optional]

Handling Errors

As per Joi docs, any validation failure that is caught by Joi will be thown as an error with the .isJoi property attached to it. It is recommended to have an error handling middleware at the bottom of your app.js to handle this:

app.use('*', (err, req, res, next) => {
    if(err.isJoi) {
      // do stuff
    }

    return next(err);
});

Advanced

Multiple Validations

You can do multiple validations in a very clear and concise syntax:

app.get('/',
  vayder.validateHeaders(AuthenticationSchema),
  vayder.validateQuery(paginationSchema),
  vayder.validateParams(userIdSchema),
  someController.doStuff);

Passing in Joi Options

Every method allows you to pass in an optional config of all the Joi options you want to run on the schema. For example:

app.post('/blah',
  vayder.validateBody(blahSchema, {abortEarly: false}),
  (req, res) => { res.send('Hello World!');}
);

Organizing Schemas

A useful way to organize / manage the schema imports is by creating a directory tree called models/validations in your project.

webapp
├── app.js
├── controllers
├── datasource
├── middlewares
├── models
│   └── validations
│       ├── accessToken.js
│       ├── accountSignUp.js
│       ├── index.js
│       ├── credentials.js
│       └── pagination.js
│
├── routes.js
├── serializers
├── services
└── utilities

Then, in the models/validations/index.js file, you can manage a single entry point to all the validations:

// index.js

module.exports = {
  accessToken: require('./accessToken'),
  accountSignUp: require('./accountSignUp'),
  credentials: require('./credentials'),
  pagination: require('./pagination'),
}

This makes for a cleaner interface when validating routes:

// routes.js
const vayder = require('vayder');
const schemas = require('./models/validations');

app.get('/',
  vayder.validateHeaders(schemas.accessToken),
  vayder.validateQuery(schemas.pagination),
  someController.doStuff
);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].