JSPM

validatorjs-expressjs

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q27679F
  • License MIT

Validation library inspired by Laravel's Validator and support for express middleware

Package Exports

  • validatorjs-expressjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (validatorjs-expressjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Installation

The validatorjs library makes data validation in JavaScript very easy in both the browser and Node.js. This library was inspired by the Laravel framework's Validator.

Doc validatorjs

$ npm install validatorjs-expressjs

or

$ yarn add validatorjs-expressjs

Usage

Simple Usage

const express = require("express");
const validator = require("validatorjs-expressjs");
const bodyParser = require("body-parser");
const Validator = require("validatorjs");

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// IF YOU WANT TO ADD CUSTOM VALIDATE //
Validator.registerAsync("unique", async function(
  value,
  attribute,
  request,
  passes
) {
  passes();
});

app.use(
  validator({
    Validator
  })
);
// ------------------------------------

app.post("/products", [
  validator({
    rules: function(req) {
      return {
        name: "required|unique",
        price: "required|numeric"
      };
    }
  }),
  function(req, res, next) {
    res.json({ message: "submit products ok" });
  }
]);

app.use((err, req, res, next) => {
  res.status(400).json({
    message : err.message
  });
});

app.listen(3000, function() {
  console.log("web server listening on port 3000");
});

Custom Field Label

app.post("/products", [
  validator({
    rules: function(req) {
      return {
        firstName: "required",
        lastName: "required"
      };
    },
    //Custom field label
    attributeNames: req => {
      return {
        firstName: "First Name",
        lastName: "Last Name",
      };
    }
  }),
  function(req, res, next) {
    res.json({ message: "submit products ok" });
  }
]);

Error Response

{
  "message" : "The First Name field is required."
}

Separate Request File

src/request/product_create_request.js

module.exports = {
  //Rule
  rules: function(req) {
    return {
      firstName: "required",
      lastName: "required"
    };
  },
  //Custom field label
  attributeNames: req => {
    return {
      firstName: "First Name",
      lastName: "Last Name",
    };
  }
}
const productCreateRequest = require("./request/product-create.request");

app.post("/products", [
  validator(productCreateRequest),
  function(req, res, next) {
    res.json({ message: "submit products ok" });
  }
]);

License

Copyright © 2020 Released under the MIT license

Credits

validatorjs created by David Tang