DONE
This commit is contained in:
32
services/ValidationService.js
Normal file
32
services/ValidationService.js
Normal 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 ))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user