33 lines
758 B
JavaScript
33 lines
758 B
JavaScript
import { setResp400 } from '../helpers/http.js'
|
|
import { extractProps } from '../helpers/utils.js'
|
|
import {webFields} from '../repositories/schema.js'
|
|
|
|
/**
|
|
*
|
|
* @param schema
|
|
* @returns {(function(*, *, *): Promise<*|undefined>)|*}
|
|
*/
|
|
export const id = (schema) => async (req, res, next) => {
|
|
|
|
let objToValidate
|
|
|
|
if (req.method === 'POST' || req.method === 'PATCH') {
|
|
|
|
if (req.method === 'PATCH'){
|
|
webFields.id = null
|
|
}
|
|
|
|
objToValidate = extractProps(webFields, req.body)
|
|
}else{
|
|
const { id } = req.params
|
|
objToValidate = id
|
|
}
|
|
|
|
try {
|
|
req.validatedData = await schema.validate(objToValidate)
|
|
next()
|
|
} catch (error) {
|
|
return res.status(400).json(setResp400(error.name || null, error.message || null ))
|
|
}
|
|
}
|