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

ucheck

v1.3.0

Published

express req.body validator

Downloads

18

Readme

ucheck

a smart express req.body validator

#Install Install the library with npm install ucheck

var uCheck = require('ucheck');

#Basic Usage


var x = [

        {
            param: 'person.username',
            label: 'Username',
            required: true,
            type: 'string',
            length: { min: 3 , max: 16},
            regex: /^[a-z0-9_-]{3,16}$/
        },{
            param: 'person.password',
            label: 'Password',
            required: true,
            type: 'string',
            length: { min:6, max: 20},
            regex: /^[a-z0-9_-]{6,20}$/
        },{
            param: 'teamname',
            label: 'Team Name',
            required: true,
            type: 'string',
            length: {min: 3, max: 16},
            regex: /^[a-z0-9_-]{3,16}$/
        },{
            param: 'teamsecret',
            label: 'Team Secret',
            required: true,
            type: 'string',
            length: {min: 15, max: 30},
            regex: /^[a-z0-9_-]{15,30}$/
        }
]


//create instance and pass the array x to be validated
let ucheck = new uCheck.validate(req).scenario(x);

//returns true if there were any validation errors
if(ucheck.hasErrors()){
    
    //returns human readable error messages
    res.status(400).send({error: ucheck.getErrors()});
    return false;

} else {
    //validation is OK! 
    //do something
}

example of a returned error array

[
   {
      "msg":"invalid format",
      "param":"person.username",
      "label":"Username"
   },
   {
      "msg":"invalid format",
      "param":"person.password",
      "label":"Password"
   },
   {
      "msg":"invalid format",
      "param":"teamsecret",
      "label":"Team Secret"
   },
   {
      "msg":"invalid length 8 to 12 characters required",
      "param":"username",
      "label":"Username"
   },
   {
      "msg":"invalid length 7 to 15 characters required",
      "param":"teamsecret",
      "label":"Team Secret"
   }
]

#Validationg Options

  • param {string}: the name of the param in req.body

//pass the name of the param in req.body
{
    param: 'name'
}

//nested objects can be reached using 
{
    param: 'person.name'
}
  • label {string}: how the param will appear in error messages

{
    param: 'teamname'
    label: 'Team Name' // will be shown in error message
}

  • required {boolean}: check the presence of the param & will push error if not exists

{
    param: "email",
    label: "Email"
    required: true // or false
}
  • type {string}: check the type of param
{
    param: "companyName",
    label: "Company Name"
    type: "string" // or number|boolean|array|object
}

{
    param: "list",
    label: "Todo List"
    type: "array" // or number|boolean|string|object
}
  • length {object} : check the length of the param
{
    param: "title",
    label: "Message Title",
    length: { min:12,  max: 24} // error if value < min OR  value > max
}
  • regex {regex}: test the param aganist regex
{
    param: "link",
    lable: "linkedin URL",
    regex: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
}

//can also pass string

{
    param: "link",
    lable: "linkedin URL",
    regex: "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
}
  • oneOf {[array of strings]}

{
    param: "gender",
    label: "Gender",
    oneOf: ["male", "female", "other"]
}
  • canParse {[array of strings]}

{
    param: "money",
    label: "Cash",
    canParse: "int"
}

{
    param: "money",
    label: "Cash",
    canParse: "float"
}

{
    param: "daet",
    label: "submit date",
    canParse: "date"
}

#Methods

  • validate: accepts express req object parsed with body parser
  • scenario: validates the validation array and updated hasError & getErrors with results
  • hasError: return boolean
  • getErrors: returns error array
  • setMsgs: accepts key & value objects to override default error messages

#Use Custom Error Messages - useing setMsgs()

let ucheck = new uCheck.validate(req).setMsgs(

        {

            "missingParam": "خطأ لم يتم العثور",
            "invalidFormat":"无效的格式",
            "invalidLength":"invalid length",
            "missingOpts":"Missing Options"
        
        }

    ).scenario(x);

#Current Supported Error Keys

  • "missingParam": triggered when a param not found
  • "invalidFormat" : triggered when regex fail
  • "invalidLength" : when length is not correct
  • "missingOpts": when options are missing