猿问

带有不允许字段的 PUT/PATCH 请求

我有一个应用程序,我正在 Symfony 4 框架中编写。我有一个 PUT/PATCH 请求,其中可能包含错误的请求字段。例如 Entities 用户不应包含字段description。在这种情况下,我想阻止请求并返回错误的请求响应。我想知道在 Symfony 4 中最好的方法是什么?


In node.js implenentation such problem looks like below: 


router.patch('/tasks/:id', async (req, res) => {

    const updates = Object.keys(req.body)

    // allowed fields

    const allowedUpdates = ['description', 'completed']

    // check if there are bad fields

    const isValidOperation = updates.every((update) => allowedUpdates.includes(update))

    if (!isValidOperation) {

        return res.status(400).send({ error: 'Invalid updates!' })

    }

    /*

    some response code

    /*

})

如何在 Symfony 4 框架中做类似的事情?


HUWWW
浏览 168回答 1
1回答
随时随地看视频慕课网APP
我要回答