要求验证者:验证IRI [英] Request validator : validate an IRI

查看:100
本文介绍了要求验证者:验证IRI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用api-platform,到目前为止,它是一个很好的工具.我想在将数据存储到数据库之前添加一些验证器.

Api平台使用IRI而不是普通ID(即使您可以更改此行为).我想知道什么是验证IRI的最佳方法.

比方说,我收到了例如"api/users/1".我想确保此IRI实际上是有效的(即,该ID存在于用户"表中).

我可以创建一个自定义验证器规则,该规则将调用该IRI并查看其是否返回有效结果,或者甚至将字符串拆分以获取ID和表,然后执行sql查询以检查其是否不为空./p>

但是我认为这两种解决方案都不是很理想.你对我有什么线索吗?

非常感谢您!

解决方案

在反序列化过程中,API平台会自动将IRI转换为引用实体的实例.

如果提供了无效的IRI(格式错误或未找到),API平台将自动引发错误并返回400(错误请求)HTTP状态.

如果要添加更多高级验证约束,则可以在根实体和相关实体(用户)上都使用Symfony验证器.例如,参见 @Valid 约束

I'm currently working with api-plateform which has been a great tool so far. I'd like to add some validator before my datas are stored into my database.

Api-platform works with IRI instead of plain id (even if you can change this behavior). I was wondering what would be the best way to validate an IRI.

Let's say I receive for instance "api/users/1". I would like to make sure that this IRI is actually valid (i.e the id exists in the table "user").

I could create a custom validator rule which would either call that IRI and see if it returns a valid result or even split the string to get the id and the table and then make an sql query to check if it's not null.

But I think both solutions are not really ideal. Do you have any clue for me ?

Thank you very much !

解决方案

API Platform will automatically convert the IRI to an instance of the referenced entity during the deserialization process.

If an invalid IRI is provided (malformed, or not found), API Platform will automatically throw an error and return a 400 (Bad Request) HTTP status.

You can use the Symfony validator both on the root entity and on the related entity (the user) if you want to add more advanced validation constraints. See for instance the @Valid constraint.

这篇关于要求验证者:验证IRI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆