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

validator-chain-util

v1.0.4

Published

validator-chain-util is a wrapper that can call validator.js through a chain or schema, and supports wildcards. The usage is similar to express-validator.

Downloads

10

Readme

Overview

npm version license GitHub Workflow Status Coverage Status GitHub Workflow Status

validator-chain-util is a utility library for the validator.js project. You can verify data through chain functions, or use schema to verify data. It supports wildcard attribute selectors, you can use wildcards to validate data inside arrays or objects.

Index

Getting Started

Getting Started

Installation

# NPM 
npm install validator-chain-util

# Yarn
yarn add validator-chain-util

Usage

JavaScript

const { valid,validatorCheck} = require('validator-chain-util');  // only chain
const { valid,validatorSchema,validatorCheck } = require('validator-chain-util'); // chain and schema

TypeScript

import valid,{validatorCheck} from 'validator-chain-util'; // only chain
import valid,{validatorSchema,validatorCheck} from 'validator-chain-util'; // chain and schema

Examples

Test Object

const testObj = {
  name: 'test',
  age: 18,
  email: '[email protected]',
  files: [
    {
      name: 'test1',
      size: 100,
    },
    {
      name: 'test2',
      size: 200,
    },
  ],
}

Use Chain Function


const emailCheck = valid("email").isEmail().run(testObj)
// {pass: true, msg: '',data:[]}

const nameCheck = valid("name")
  .notEmpty()
  .isLength({ min: 5 })
  .withMessage("name is too short")
  .run(testObj)
// {
//   pass: false,
//   msg: 'name is too short',
//   data: [ { pass: false, fun: '_isLength', msg: 'name is too short' } ]
// }

console.log(valid("age").isInt().run(testObj))
// {pass: true, msg: '',data:[]}

/* Optional field check */
console.log(valid("phone")
  .optional() // If the field is not present, skip the check
  .isMobilePhone("en-US")
  .run(testObj))
// {pass: true, msg: '',data:[]}

/* Check the data in the array */
console.log(
  valid("files.1.name")
  .notEmpty()
  .isLength({ min: 5 })
  .withMessage("name is too short")
  .run(testObj))
// { pass: true, msg: '', data: [] }

/* If you just want to check a data */
console.log(valid().isEmail().run("[email protected]"))
// { pass: true, msg: '', data: [] }

/* use validatorCheck */
validatorCheck(emailCheck) // true
validatorCheck(nameCheck) // false
validatorCheck([emailCheck, nameCheck]) // false

Use Schema

const schema = {
  name: valid().notEmpty().isLength({ min: 5 }),
  age: valid().isInt(),
  email: valid().isEmail(),
  files: valid().isArray()
}

const checkResult = validatorSchemaCheck(schema, testObj)
// [
//   { pass: false, msg: '_isLength', data: [ [Object] ] },
//   { pass: true, msg: '', data: [] },
//   { pass: true, msg: '', data: [] },
//   { pass: true, msg: '', data: [] }
// ]

/* use validatorCheck */
validatorCheck(checkResult) // false

Advanced Usage

Wildcard

valid("files.*.name")
  .notEmpty()
  .isLength({ min: 5 })
  .withMessage("name is too short")
  .run(testObj)
// { pass: true, msg: '', data: [] }

valid("files.*")
  .isObject()
  .run(testObj)
// { pass: true, msg: '', data: [] }

valid("a.*.*").isInt().run({ a: { b: { c: 1 } } })
// { pass: true, msg: '', data: [] }

/* schema */
const schema = {
  files: valid().isArray(),
  "files.*.name": valid().notEmpty().isLength({ min: 5 }),
  "files.*.size": valid().isInt(),
}

validatorSchemaCheck(schema, testObj)
// [
//   { pass: true, msg: '', data: [] },
//   { pass: true, msg: '', data: [] },
//   { pass: true, msg: '', data: [] }
// ]
validatorCheck(validatorSchemaCheck(schema, testObj)) // true

Bail Function

bail() will stop checking if the current check fails

valid("name")
  .isLength({ min: 6 })
  .withMessage("name length must > 6")
  .isLength({ min: 5 })
  .withMessage("name length must > 5")
  .run(testObj)
// {
//   pass: false,
//   msg: 'name length must > 6',
//   data: [ 
//            { pass: false, fun: '_isLength', msg: 'name length must > 6' }
//            { pass: false, fun: '_isLength', msg: 'name length must > 5' }
//         ]
// }

valid("name")
  .isLength({ min: 6 })
  .withMessage("name length must > 6")
  .bail()  // If the current check fails, stop checking
  .isLength({ min: 5 })
  .withMessage("name length must > 5")
  .run(testObj)
// {
//   pass: false,
//   msg: 'name length must > 6',
//   data: [{ pass: false, fun: '_isLength', msg: 'name length must > 6' }]
// }

Optional / Allow Null / notEmpty

optional() and allowNull() are different, optional() will skip the check if the field is not present, allowNull() will skip check the field if the field is present, but the value is null. notEmpty() will check the field is "". | optional() | allowNull() | notEmpty() | input value | | ---------- | ----------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | false | false | false | :x:undefined must value error, stop checking :x:null must value error, stop checking :arrow_forward: "" check next function :arrow_forward:normal value check next function | | true | false | false | :white_check_mark:undefined skip all check :x:null must value error, stop checking :arrow_forward: "" check next function :arrow_forward:normal value check next function | | false | true | false | :x:undefined must value error, stop checking :white_check_mark:null skip all check :arrow_forward: "" check next function :arrow_forward:normal value check next function | | false | false | true | :x:undefined must value error, stop checking :x:null must value error, stop checking :x:"" _notEmpty error, then check next :arrow_forward:normal value check next function | | true | true | false | :white_check_mark:undefined skip all check :white_check_mark:null skip all check :arrow_forward: "" check next function :arrow_forward:normal value check next function | | false | true | true | :x:undefined must value error, stop checking :white_check_mark:null skip all check :x:"" _notEmpty error, then check next :arrow_forward:normal value check next function | | true | false | true | :white_check_mark:undefined skip all check :x:null must value error, stop checking :x:"" _notEmpty error, then check next :arrow_forward:normal value check next function | | true | true | true | :white_check_mark:undefined skip all check :white_check_mark:null skip all check :x:"" _notEmpty error, then check next :arrow_forward:normal value check next function |

valid("email").isEmail().run({})
// {
//   pass: false,
//   msg: 'must value',
//   data: [ { pass: false, fun: '', msg: 'must value' } ]
// }

/* Optional Filed */
// If the field is not present, skip the check
// If the field is null,cannot skip the check
valid("email").optional().isEmail().run({})
// { pass: true, msg: '', data: [] }
valid("email").optional().isEmail().run({email:null})
// {
//   pass: false,
//   msg: 'must value',
//   data: [ { pass: false, fun: '', msg: 'must value' } ]
// }

/* Allow Null */
// If the field is not present, cannot skip the check
// If the field is null, skip the check
valid("email").allowNull().isEmail().run({})
// {
//   pass: false,
//   msg: 'must value',
//   data: [ { pass: false, fun: '', msg: 'must value' } ]
// }
valid("email").allowNull().isEmail().run({email:null})
// { pass: true, msg: '', data: [] }

/* notEmpty */
// If the field is empty (is ""), the check will fail
valid("email").notEmpty().isEmail().run({email:""})
// {
//   pass: false,
//   msg: '_notEmpty',
//   data: [ { pass: false, fun: '_notEmpty', msg: '_notEmpty' } ]
// }

Custom Validator

valid().customer((value:string,rawValue:any):boolean=>{
  if(value === 'test'){
    return true
  }
  return false
}).run(testObj)

// value is the input value to string
// rawValue is the input value to raw type

Custom Error Message

valid("name")
.isLength({min:5})
.withMessage('name is too short')
.contains('test')
.withMessage('name must contains test')
.run(testObj)

Change Log

View Change Log

Thanks