|
|
@ -1,9 +1,9 @@
|
|
|
|
const {body} = require('express-validator')
|
|
|
|
const {body, validationResult} = require('express-validator')
|
|
|
|
const db = require('../../db')
|
|
|
|
const db = require('../../db')
|
|
|
|
|
|
|
|
|
|
|
|
const validators = module.exports = {}
|
|
|
|
const validators = module.exports = {}
|
|
|
|
|
|
|
|
|
|
|
|
validators.passwordRestrictions = body('password').isString().isLength({min: 8, max: 100}).withMessage('Password must be at least 8 characters')
|
|
|
|
validators.validPassword = field => body(field).isString().isLength({min: 8, max: 100}).withMessage('Password must be at least 8 characters')
|
|
|
|
|
|
|
|
|
|
|
|
validators.bothPasswordsMatch = body('password').custom((pass, {req})=>{
|
|
|
|
validators.bothPasswordsMatch = body('password').custom((pass, {req})=>{
|
|
|
|
if(pass !== req.body.password2)
|
|
|
|
if(pass !== req.body.password2)
|
|
|
@ -11,26 +11,33 @@ validators.bothPasswordsMatch = body('password').custom((pass, {req})=>{
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
validators.emailRestrictions = body('email').isString().isEmail().withMessage('Email invalid')
|
|
|
|
validators.validEmail = field => body(field).isString().isEmail()
|
|
|
|
|
|
|
|
.withMessage('Email invalid')
|
|
|
|
|
|
|
|
|
|
|
|
validators.checkEmailNotUsed = body('email').custom(async email=>{
|
|
|
|
validators.unusedEmail = field => validators.validEmail(field)
|
|
|
|
|
|
|
|
.custom(async email=>{
|
|
|
|
const user = await db.user.findByEmail(email)
|
|
|
|
const user = await db.user.findByEmail(email)
|
|
|
|
if(user) throw new Error('Email already in use')
|
|
|
|
if(user) throw new Error('Email already in use')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
validators.urlSlugRestrictions = body('urlslug').isString().isLength({min: 3, max: 20}).matches(/^[-a-z0-9_]*$/i)
|
|
|
|
validators.validUrlSlug = field => body(field).isString().isLength({min: 3, max: 20}).matches(/^[-a-z0-9_]*$/i)
|
|
|
|
.withMessage('Slug can be between 3-20 characters long, and can include letters, numbers, dash or underscore')
|
|
|
|
.withMessage('Slug can be between 3-20 characters long, and can include letters, numbers, dash or underscore')
|
|
|
|
|
|
|
|
|
|
|
|
validators.nameRestrictions = body('name').isString()
|
|
|
|
validators.requiredString = field => body(field).isString()
|
|
|
|
.withMessage('Name required. Must be a string')
|
|
|
|
.withMessage('Required')
|
|
|
|
|
|
|
|
|
|
|
|
validators.descriptionRestrictions = body('description').isString()
|
|
|
|
validators.positiveInteger = field => body(field).isInt().custom(value => value > 0)
|
|
|
|
.withMessage('Description required. Must be a string')
|
|
|
|
.withMessage('Must be a positive integer')
|
|
|
|
|
|
|
|
|
|
|
|
validators.publishedBool = body('published').isBoolean()
|
|
|
|
validators.publishedBool = body('published').isBoolean()
|
|
|
|
|
|
|
|
|
|
|
|
validators.priceCentsInt = body('price_cents').isInt().custom(price => price > 0)
|
|
|
|
validators.isUUID = field => body(field).isString().isUUID()
|
|
|
|
.withMessage('Price must be a positive integer')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validators.countInt = body('count').isInt().custom(count => count > 0)
|
|
|
|
validators.handleApiError = (req, res, next) => {
|
|
|
|
.withMessage('Count must be a positive integer')
|
|
|
|
const errors = validationResult(req)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(errors.isEmpty())
|
|
|
|
|
|
|
|
return next()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.status(422).json({errors: errors.array()})
|
|
|
|
|
|
|
|
}
|
|
|
|