0.0.3 • Published 2 years ago

body-validator-v2 v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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": "pop@armenia.com",
  "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

ParamTypePredefined
nameString
typeString'String', 'Number', 'Date', 'Boolean', 'Email', 'URL', 'Mongo', 'Array'
optionsObjectLook bottom table
requiredBoolean
validatorValidator object
OptionFor typeTypeDescriptionPredefined
minNumberNumberIf field type is Number - min value
maxNumberNumberIf field type is Number - max value
minRecordsArrayNumberIf field type is Array - min array length
maxRecordsArrayNumberIf field type is Array - max array length
arrayValuesTypeArrayStringIf field type is Array - type of records'String', 'Number', 'Date', 'Boolean', 'Email', 'URL', 'Mongo', 'Array'
arrayValuesOptionsArrayObjectIf field type is Array - validate options for recordsSame like in this table
minSymbolsStringNumberIf field type is String - min symbols
maxSymbolsStringNumberIf field type is String - max symbols
canBeEmptyStringBooleanIf field type is String - can be empty string?
allowSpacesStringBooleanIf field type is String - allow to have spaces?
maxWordsStringNumberIf field type is String - max number of words
blackListStringArrayIf field type is String - symbols that are forbidden
includeStringStringIf field type is String - what type of characters it may containlettersOnly', 'numbersOnly', 'lettersAndNumbers'
enumStringArrayIf 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) => {})
0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago