This commit is contained in:
2021-10-15 02:00:40 +03:00
parent 74316481f1
commit ade92e0493
15 changed files with 884 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
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 ))
}
}