Skip to content

widgetdude/express-validator-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-validator-examples

A few different ways to implement express validator

References

  1. https://express-validator.github.io/docs/
  2. https://github.com/validatorjs/validator.js

Setup

Install the dependencies
npm install
Start the server
npm start
Make Request

Send a POST request to http://localhost:8080/examples/1 with the following body:

{
  "userId": "633065ec5cac22c34826c340",
  "event": "GameScore",
  "game": "Snake",
  "score": 88
}

Rules

Every request payload should:

  1. Have a userId and should be a valid mongo id
  2. Have a event and it should be one of ["GameScore", "GameStart", "GameEnd"]
  3. Have a game if the event is GameScore. It should be one of ["Pong", "Tetris", "Snake"]
  4. Have a score if the event is GameScore. It should be a number and be between 0 and 100.

Invalid Payload Examples

Game score > 100

{
  "userId": "633065ec5cac22c34826c340",
  "event": "GameScore",
  "game": "Snake",
  "score": 101
}

Invalid game type

{
  "userId": "633065ec5cac22c34826c340",
  "event": "GameScore",
  "game": "Pacman",
  "score": 50
}

Valid Payload

{
  "userId": "633065ec5cac22c34826c340",
  "event": "GameScore",
  "game": "Snake",
  "score": 88
}

About

A few different ways to implement express validator

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published