Refactor validators

main
Ashelyn Dawn 5 years ago
parent 9ce70cefe2
commit b59134dc5e

@ -2,16 +2,15 @@ const router = require('express-promise-router')()
const parseJSON = require('body-parser').json() const parseJSON = require('body-parser').json()
const db = require('../db') const db = require('../db')
const {validationResult} = require('express-validator')
const validate = require('./middleware/validators') const validate = require('./middleware/validators')
const loginValidation = [validate.emailRestrictions, validate.passwordRestrictions] const loginValidation = [
router.post('/', parseJSON, loginValidation, async (req, res) => { validate.validEmail('email'),
const errors = validationResult(req) validate.validPassword('password'),
validate.handleApiError
if(!errors.isEmpty()) ]
return res.status(422).json({errors: errors.array()})
router.post('/', parseJSON, loginValidation, async (req, res) => {
const user = await db.user.login( const user = await db.user.login(
req.body.email, req.body.email,
req.body.password req.body.password

@ -2,20 +2,17 @@ const router = module.exports = require('express-promise-router')()
const parseJSON = require('body-parser').json() const parseJSON = require('body-parser').json()
const db = require('../db') const db = require('../db')
const {validationResult} = require('express-validator')
const validate = require('./middleware/validators') const validate = require('./middleware/validators')
router.get('/', async (req, res)=>{ router.get('/', async (req, res)=>{
res.json(req.cart); res.json(req.cart);
}) })
const cartAddValidators = [validate.countInt] const cartAddValidators = [
validate.positiveInteger('count'),
validate.handleApiError
]
router.post('/add/:item_uuid', parseJSON, cartAddValidators, async (req, res) => { router.post('/add/:item_uuid', parseJSON, cartAddValidators, async (req, res) => {
// Check form input
const errors = validationResult(req)
if(!errors.isEmpty())
return res.status(422).json({errors: errors.array()})
// Ensure session // Ensure session
if(!req.session.uuid) if(!req.session.uuid)
await db.session.create(req) await db.session.create(req)

@ -2,7 +2,6 @@ const router = module.exports = require('express-promise-router')()
const parseJSON = require('body-parser').json() const parseJSON = require('body-parser').json()
const db = require('../db') const db = require('../db')
const {validationResult} = require('express-validator')
const validate = require('./middleware/validators') const validate = require('./middleware/validators')
router.get('/', async (req, res) => { router.get('/', async (req, res) => {
@ -12,16 +11,12 @@ router.get('/', async (req, res) => {
}) })
const newCategoryValidators = [ const newCategoryValidators = [
validate.urlSlugRestrictions, validate.validUrlSlug('urlslug'),
validate.nameRestrictions, validate.requiredString('name'),
validate.descriptionRestrictions validate.requiredString('description'),
validate.handleApiError
] ]
router.post('/', parseJSON, newCategoryValidators, async (req, res) => { router.post('/', parseJSON, newCategoryValidators, async (req, res) => {
const errors = validationResult(req)
if(!errors.isEmpty())
return res.status(422).json({errors: errors.array()})
const category = await db.category.create( const category = await db.category.create(
req.body.name, req.body.name,
req.body.urlslug, req.body.urlslug,

@ -2,7 +2,6 @@ const router = module.exports = require('express-promise-router')()
const parseJSON = require('body-parser').json() const parseJSON = require('body-parser').json()
const db = require('../db') const db = require('../db')
const {validationResult} = require('express-validator')
const validate = require('./middleware/validators') const validate = require('./middleware/validators')
const upload = require('multer')({ const upload = require('multer')({
@ -20,19 +19,15 @@ router.get('/', async (req, res) => {
}) })
const newItemValidators = [ const newItemValidators = [
validate.urlSlugRestrictions, validate.validUrlSlug('urlslug'),
validate.publishedBool, validate.publishedBool,
validate.priceCentsInt, validate.positiveInteger('price_cents'),
validate.nameRestrictions, validate.requiredString('name'),
validate.descriptionRestrictions validate.requiredString('description'),
validate.handleApiError
] ]
router.post('/', parseJSON, newItemValidators, async (req, res) => { router.post('/', parseJSON, newItemValidators, async (req, res) => {
const errors = validationResult(req)
if(!errors.isEmpty())
return res.status(422).json({errors: errors.array()})
const item = await db.item.create( const item = await db.item.create(
req.body.name, req.body.name,
req.body.urlslug, req.body.urlslug,

@ -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()})
}

@ -2,16 +2,16 @@ const router = require('express-promise-router')()
const parseJSON = require('body-parser').json() const parseJSON = require('body-parser').json()
const db = require('../db') const db = require('../db')
const {validationResult} = require('express-validator')
const validate = require('./middleware/validators') const validate = require('./middleware/validators')
const registerValidation = [validate.emailRestrictions, validate.passwordRestrictions, validate.bothPasswordsMatch, validate.checkEmailNotUsed] const registerValidation = [
router.post('/', parseJSON, registerValidation, async (req, res) => { validate.unusedEmail('email'),
const errors = validationResult(req) validate.validPassword('password'),
validate.bothPasswordsMatch,
if(!errors.isEmpty()) validate.handleApiError
return res.status(422).json({errors: errors.array()}) ]
router.post('/', parseJSON, registerValidation, async (req, res) => {
const user = await db.user.register( const user = await db.user.register(
req.body.email, req.body.email,
req.body.password req.body.password

@ -10,7 +10,7 @@ export default function Table({columns, rows, foot}) {
)} )}
</tr> </tr>
</thead> </thead>
<tbody> {rows && rows.length > 0 && <tbody>
{rows.map(row=> {rows.map(row=>
<tr key={row.id} className={row.class}> <tr key={row.id} className={row.class}>
{columns.map(column=> {columns.map(column=>
@ -18,7 +18,7 @@ export default function Table({columns, rows, foot}) {
)} )}
</tr> </tr>
)} )}
</tbody> </tbody>}
{foot && <tfoot> {foot && <tfoot>
<tr> <tr>
{foot.map((item, i) => {foot.map((item, i) =>

@ -6,6 +6,8 @@ import {FormController, Input, Button} from '~/components/form'
import Table from '~/components/table' import Table from '~/components/table'
export default function Cart({cart, setCart}){ export default function Cart({cart, setCart}){
const numItems = (cart?.items) ? cart.items.length : 0
const handleRemove = id => async ev => { const handleRemove = id => async ev => {
if(ev) ev.preventDefault() if(ev) ev.preventDefault()
@ -13,12 +15,19 @@ export default function Cart({cart, setCart}){
setCart(data) setCart(data)
} }
const handleCreateTransaction = async () => {
}
return ( return (
<> <>
<Head><title>Cart</title></Head> <Head><title>Cart</title></Head>
<> <>
<h2>Cart</h2> <h2>Cart</h2>
<Table
{
numItems > 0
?<Table
columns={[ columns={[
{name: 'Item', extractor: row => row.item.name}, {name: 'Item', extractor: row => row.item.name},
{name: 'Quantity in Cart', extractor: row => row.count}, {name: 'Quantity in Cart', extractor: row => row.count},
@ -40,9 +49,21 @@ export default function Cart({cart, setCart}){
'' ''
]} ]}
/> />
// Empty cart table
:<Table
columns={[
{name: 'No items in cart'}
]}
foot={[
'Total:',
'',
'',
'$0.00'
]}
/>
}
<FormController> <FormController>
<Input label="Coupon Code" type="text" name="coupon"/>
<Button enabled={!!(cart?.items?.length)} type="submit">Proceed to Checkout</Button> <Button enabled={!!(cart?.items?.length)} type="submit">Proceed to Checkout</Button>
</FormController> </FormController>
</> </>

Loading…
Cancel
Save