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

body-validations

v1.0.47

Published

```js // Basic rule const request_body = { name: "Souksavanh", age: 23, some_id: "61c3fc57c312253a7e1da187", gender: "male", phone_number: 526839374, contact_detail: { gmail: "[email protected]", whatsapp: "563434234", }, contac

Downloads

8

Readme

// Basic rule
const request_body = {
  name: "Souksavanh",
  age: 23,
  some_id: "61c3fc57c312253a7e1da187",
  gender: "male",
  phone_number: 526839374,
  contact_detail: {
    gmail: "[email protected]",
    whatsapp: "563434234",
  },
  contact_detail_else: {
    gmail: "[email protected]",
  },
  empty_object: ["1", "2"],
};

/*

    First rule string: requied optional objectid
    Second: Error message
    Third: value type or validation [string,object,number,object,array,enum,exist]

*/
const rule = {
  name: "required|name must be string|string",
  age: "required|name must be number|number",
  some_id: "required|name must be ojectid|ojectid",
  gender: "required|name must be string|enum:male,female",
  phone_number: "required|name must be string|exist:users", // users is model name base on mongoose model SUPPORT mongoose only / Mongodb
  "office_id:_id": "objectid|name must be string|exist:offices", // will use _id to find exist data, offices is model name base on mongoose model SUPPORT mongoose only / Mongodb
  contact_detail: "required|name must be string|object:gmail/string,whatsapp/required-string", //or
  contact_detail_else: "required|name must be string|object:gmail/string,whatsapp/string",
  array_data: "required|name must be array|array",
};

const exclude_body = await validate({ rule, req: req, exclude_body: true,type:"body" }); //or type = [body,query]
/*
 const rule = {
    name: "Souksavanh",
    age: 23,
    some_id: "61c3fc57c312253a7e1da187",
    gender: "male",
    phone_number: 526839374,
    contact_detail: {
        gmail: "[email protected]",
        whatsapp: "563434234",
    },
    contact_detail_else: {
        gmail: "[email protected]",
    },
    empty_object: ["1", "2"],
};
*/

const no_exclude_body = await validate({ rule, body: request_body });

/* return everything in object even nothing rule set
 const rule = {
    name: "Souksavanh",
    name1: "Souksavanh",
    name2: "Souksavanh",
    name2: "Souksavanh",
    age: 23,
    some_id: "61c3fc57c312253a7e1da187",
    gender: "male",
    phone_number: 526839374,
    contact_detail: {
        gmail: "[email protected]",
        whatsapp: "563434234",
    },
    contact_detail_else: {
        gmail: "[email protected]",
    },
    empty_object: ["1", "2"],
};
*/


# Using with async-api-response and helper async middleware with ExpressJS

const Res = require("async-api-response")
const async_middleware = (handler) => {
    return async (req, res, next) => {
      const resp = new Res(res);
      try {
        await handler(req, res, next);
      } catch (ex) {
        return resp.catch({ error: ex });
      }
    };
}

const simple_test = async_middleware(async (req,res)=> {
    const resp = new Res(res)
    const no_exclude_body = await validate({ rule, req, });
  return resp.response({data:"some_data"});
})

router.get("/",simple_test)