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 🙏

© 2026 – Pkg Stats / Ryan Hefner

body-validator-v2

v0.0.3

Published

Object validator created for express

Readme

body-validator-v2

Description

Validate objects, designed to validate req.body object in Express

Installation

npm i body-validator-v2

Usage

// No ES6
const { Validator } = require('body-validator-v2')

// ES6
import { Validator } from 'body-validator-v2'

For example will validate object for hockey player

{
  "name": "Simeon Mladenov",
  "number": 13,
  "team": "61d9f9c62411e95f925d7c5e",
  "profilePhoto": "https://my.photos.net/1.jpg",
  "email": "[email protected]",
  "position": "guard",
  "birthDate": "1980-04-01",
  "photos": [
    "https://my.photos.net/1.jpg",
    "https://my.photos.net/2.jpg",
  ],
  "previousTeams": [
    {
      "team": "61d9f9c62411e95f925d7c5e",
      "beginYear": "2000-04-01",
      "endYear": "2001-04-01"
    },
    {
      "team": "61d9f9c62411e95f925d7c5e",
      "beginYear": "2001-04-01",
      "endYear": "2002-04-01"
    }
  ],
  "address": {
    "city": "Sofia",
    "str": "2 None Str."
  }
}

First create Validator

const playerValidator = new Validator()

// Validate primitive fields
playerValidator.addField({ name: 'name', type: 'String', options: { maxWords: 2, allowSpaces: true, include: 'lettersOnly' }, required: true })
playerValidator.addField({ name: 'number', type: 'Number', options: { min: 0, max: 99 }, required: true })
playerValidator.addField({ name: 'team', type: 'Mongo', required: true })
playerValidator.addField({ name: 'profilePhoto', type: 'URL' })
playerValidator.addField({ name: 'email', type: 'Email', required: true })
playerValidator.addField({ name: 'position', type: 'String', options: { enum: ['goalie', 'guard', 'attacker'] }, required: true })
playerValidator.addField({ name: 'birthDate', type: 'Date', required: true })

// Validate array of primitives
playerValidator.addField({ name: 'photos', type: 'Array', options: { minRecords: 1, arrayValuesType: 'URL' }, required: true })

// Validate array of object - in this case we create new validator for objects in array
const addressValidator = new Validator()
addressValidator.addField({ name: 'team', type: 'Mongo', required: true })
addressValidator.addField({ name: 'beginYear', type: 'Date', required: false })
addressValidator.addField({ name: 'endYear', type: 'Date', required: false })

playerValidator.addField({ name: 'previousTeams', type: 'Array', options: { minRecords: 1 }, validator: addressValidator })

// Validate nested object
playerValidator.addField({ name: 'address.city', type: 'String', required: true })
playerValidator.addField({ name: 'address.str', type: 'String', required: true })

Validate object (body)

// Validate all fields in object (body)
const check = playerValidator.validate(body)
console.log(check)
>>> { success: true, errors: null }

// Validate single field from object (body)
const checkSingle = playerValidator.validateSingle('address.city', 3)
console.log(checkSingle)
>>> { success: false, errors: "'3' is not a 'string' type!" }

// Validate few specified fields from object (body)
const checkFewFields = playerValidator.validateFields('name number previousTeams address.city', body, true)
console.log(checkFewFields)
>>> { success: true, errors: null }

Validator methods

  • addField - Add field to validator
  • validate - Validate all fields in object
  • validateSingle - Validate single field from object
  • validateFields - Validate few specified fields from object

addField

Param | Type | Predefined -------------| ------------------|------------------------------------------------------------------------- name | String | type | String | 'String', 'Number', 'Date', 'Boolean', 'Email', 'URL', 'Mongo', 'Array' options | Object | Look bottom table required | Boolean | validator | Validator object |

Option | For type | Type | Description | Predefined --------------------|------------|-----------|--------------------------------------------------------------------|-------------------------------------------------------------- min | Number | Number | If field type is Number - min value | max | Number | Number | If field type is Number - max value | minRecords | Array | Number | If field type is Array - min array length | maxRecords | Array | Number | If field type is Array - max array length | arrayValuesType | Array | String | If field type is Array - type of records | 'String', 'Number', 'Date', 'Boolean', 'Email', 'URL', 'Mongo', 'Array' arrayValuesOptions | Array | Object | If field type is Array - validate options for records | Same like in this table minSymbols | String | Number | If field type is String - min symbols | maxSymbols | String | Number | If field type is String - max symbols | canBeEmpty | String | Boolean | If field type is String - can be empty string? | allowSpaces | String | Boolean | If field type is String - allow to have spaces? | maxWords | String | Number | If field type is String - max number of words | blackList | String | Array | If field type is String - symbols that are forbidden | include | String | String | If field type is String - what type of characters it may contain | lettersOnly', 'numbersOnly', 'lettersAndNumbers' enum | String | Array | If field type is String - can only be from predefined values |

Using as middleware in Express

import express from 'express'
const router = express.Router()

// First param - if validator find error - error status (default is 422)
// Second param - check required option (default is false)
router.post('/create', playerValidator.middleware(422, true), (req, res) => {})