A few different ways to implement express validator
https://express-validator.github.io/docs/
https://github.com/validatorjs/validator.js
npm install
npm start
Send a POST request to http://localhost:8080/examples/1
with the following body:
{
"userId": "633065ec5cac22c34826c340",
"event": "GameScore",
"game": "Snake",
"score": 88
}
Every request payload should:
- Have a
userId
and should be a valid mongo id - Have a
event
and it should be one of ["GameScore", "GameStart", "GameEnd"] - Have a
game
if theevent
isGameScore
. It should be one of ["Pong", "Tetris", "Snake"] - Have a
score
if theevent
isGameScore
. It should be a number and be between 0 and 100.
Game score > 100
{
"userId": "633065ec5cac22c34826c340",
"event": "GameScore",
"game": "Snake",
"score": 101
}
Invalid game type
{
"userId": "633065ec5cac22c34826c340",
"event": "GameScore",
"game": "Pacman",
"score": 50
}
{
"userId": "633065ec5cac22c34826c340",
"event": "GameScore",
"game": "Snake",
"score": 88
}