Skip to content

aounalazzam/expressive-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expressive Validator

A Schema Based Validation Middleware For Body, Query, Params In Express JS

Usage

npm i expressive-validator

Features

  • Schema-Based & Memory Efficient
  • More extensibility features
  • Better JSON schema compliance
  • More String Validation Types Like (Emails, Without Symbols)
  • Can Validate Body, Query, Params In Same Time

Dev Timeline

  • Body
  • Query
  • Params
  • Email Validation
  • No Symbols Validation
  • Query XSS Detection
  • Custom Error Logger
  • URL Regex Matcher

.... More Features (In Planning)

Example

Schema schema.json

{
  "/api/login": {
    "body": {
      "username": { "type": "string(no-symbols)", "require": true },
      "email": { "type": "string(email)", "require": true },
      "type": { "type": "string", "require": false },
      "password": { "type": "string(not-empty)", "require": true, "minLength": 8 }
    },
    "query": {
      "sessionId": { "type": "string", "require": true }
    },
    "params":{
        ...
    },
  },
  // Specifying For Multiple HTTP Methods Per Route
  "/api/users": [
    {
      "method": "GET|DELETE",
      "body": {
        "id": { "type": "string", "require": true }
      }
    },
    {
      "method": "POST"
      ...
    }
  ],
    "/api/users/:id": [
    {
      "method": "DELETE",
      "params": {
        "id": { "type": "string", "require": true }
      }
    },
  ]
}

Middleware Usage app.js

const express = require("express");
const expressiveValidator = require("expressive-validator");

const schema = require("./schema.json");

const app = express();

app.use(express.json());
app.use(expressiveValidator(schema));

..

Add Custom Error Logger Middleware Usage app.js

const express = require("express");
const AppError = require("./error");
const expressiveValidator = require("expressive-validator");

const schema = require("./schema.json");

const app = express();

const logger = (req, res) => {
  throw new AppError({
    message : "Invalid Type"
  })
}

app.use(express.json());
app.use(expressiveValidator(schema, logger));

..

Documentation

Under Construction

License

This Project Is Under The MIT License Copyright (c) 2023 Aoun Alazzam

About

A Schema Based Validation For Body, Query, Params, Cookies & Headers In Express JS

Topics

Resources

License

Stars

Watchers

Forks