LoginSchema.validate(req.body) .then(() => next()) .catch(error => res.status(422).json({ [error.path]: error.message }) )
const login = async (req, res, next) => { try { const schema = Yup.object().shape({ email: Yup.string().email().required(), password: Yup.string().required().min(6), }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res.status(400).json({ error: 'Verifique os campos acima.' }); } }
schema.validate(req.body).catch(e => { error = { message: e.message, field: e.path }; })
const validate = async ({shape, what, req, res, next}) => { try { const toValidate = req[what]; const validated = await yup.object().shape(shape).validate(toValidate, {stripUnknown: true, abortEarly: false}); Object.assign(req, { [what]: validated, [`orig${what}`]: toValidate, }); next(); } catch (validationErrors) { const allErrors = validationErrors.inner.reduce((errors, currentValidation) => Object.assign(errors, { [currentValidation.path]: currentValidation.errors[0], //first error is enough for this demo }), {}); res.send(400, allErrors); } }
const create = async (req, res, next) => { try { const schema = Yup.object().shape({ name: Yup.string().required(), email: Yup.string().email().required(), password: Yup.string().required().min(6), }); await schema.validate(req.body, { abortEarly: false }); return next(); } catch (error) { return res.status(400).json({ error: 'Verifique os campos acima.' }); } }
const showDelivery = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required('Id is required') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
schema.validate(req.body).catch(e => { error = { message: e.message, field: e.path }; })
const deleteDelivery = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required('Id is required') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const deleteDeliveryman = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required('Id is required.') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const deleteRecipient = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required() }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const showRecipient = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required() }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const showDeliveryman = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required('Id is required.') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const listOrderToDeliveryman = async (req, res, next) => { try { const schema = Yup.object().shape({ id: Yup.number() .positive() .required('Delivery Id is required') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails', messages: error.inner }); } }
const showDeliveryProblem = async (req, res, next) => { try { const schema = Yup.object().shape({ deliveryId: Yup.number() .positive() .required('Id is required.') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }
const deleteDeliveryProblem = async (req, res, next) => { try { const schema = Yup.object().shape({ problemId: Yup.number() .positive() .required('Id is required.') }); await schema.validate(req.params, { abortEarly: false }); return next(); } catch (error) { return res .status(400) .json({ error: 'Validation fails.', messages: error.inner }); } }