验证adonisjs中的对象数组5
我正在使用 adonisjs 5 开发一个restapi,并且我正在尝试验证请求主体是一系列对象,我不知道它可能包含什么属性,所以我不想指定任何属性。 有效载荷是这样的
{
"gap": [
{
"1": "small"
},
{
"2": "x"
},
{
"a": "king"
}
]
}
,其中键代表业务逻辑的东西。 我试图以几种方式进行操作,并且它们都无法
- 通过在成员函数中添加空对象
public schema = schema.create({
gap: schema.array.optional().members(schema.object().members({})),
})
来工作,但是结果是一个空对象的数组,因为Adonis忽略了验证器架构中未提供的任何额外数据 2。仅通过 schema.Object 没有成员的功能
public schema = schema.create({
gap: schema.array.optional().members(schema.object()),
})
,但这会导致错误
i'm developing a RestAPI using AdonisJs 5 and i'm trying to validate the request body to be an array of objects which i don't know what properties it may contain so i don't want to specify any properties.
the payload is something like this
{
"gap": [
{
"1": "small"
},
{
"2": "x"
},
{
"a": "king"
}
]
}
where the keys represents something for the business logic.
i tried to do it in a couple of ways and neither of them work
- by adding empty object to the members function
public schema = schema.create({
gap: schema.array.optional().members(schema.object().members({})),
})
but the result was an array of empty object since adonis neglect any extra data that wasn't provided in the validator schema
2. passing only the schema.object without the members function
public schema = schema.create({
gap: schema.array.optional().members(schema.object()),
})
but this results an error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有.anymembers()选项。
在此处阅读有关: https://docs.adonisjs.comcom /参考/验证器/架构/对象#Accept-Any-Elements
因此您的解决方案是:
There is .anyMembers() option.
Read about it here: https://docs.adonisjs.com/reference/validator/schema/object#accept-any-elements
So your solution would be: