node-express-mongodb-jwt-rest-api-skeleton
Version:
Node.js express.js MongoDB JWT REST API - This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)
76 lines (73 loc) • 1.65 kB
JavaScript
const { validateResult } = require('../../../middleware/utils')
const validator = require('validator')
const { check } = require('express-validator')
/**
* Validates create new item request
*/
const validateCreateUser = [
check('name')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY'),
check('email')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isEmail()
.withMessage('EMAIL_IS_NOT_VALID'),
check('password')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isLength({
min: 5
})
.withMessage('PASSWORD_TOO_SHORT_MIN_5'),
check('role')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.isIn(['user', 'admin'])
.withMessage('USER_NOT_IN_KNOWN_ROLE'),
check('phone')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.trim(),
check('city')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.trim(),
check('country')
.exists()
.withMessage('MISSING')
.not()
.isEmpty()
.withMessage('IS_EMPTY')
.trim(),
check('urlTwitter')
.optional()
.custom((v) => (v === '' ? true : validator.isURL(v)))
.withMessage('NOT_A_VALID_URL'),
check('urlGitHub')
.optional()
.custom((v) => (v === '' ? true : validator.isURL(v)))
.withMessage('NOT_A_VALID_URL'),
(req, res, next) => {
validateResult(req, res, next)
}
]
module.exports = { validateCreateUser }