验证adonisjs中的对象数组5

发布于 01-23 14:32 字数 735 浏览 1 评论 0原文

我正在使用 adonisjs 5 开发一个restapi,并且我正在尝试验证请求主体是一系列对象,我不知道它可能包含什么属性,所以我不想指定任何属性。 有效载荷是这样的

{
    "gap": [
        {
            "1": "small"
        },
        {
            "2": "x"
        },
        {
            "a": "king" 
        }
    ]

}

,其中键代表业务逻辑的东西。 我试图以几种方式进行操作,并且它们都无法

  1. 通过在成员函数中添加空对象
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

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2025-01-30 14:32:23

有.anymembers()选项。

在此处阅读有关: https://docs.adonisjs.comcom /参考/验证器/架构/对象#Accept-Any-Elements

因此您的解决方案是:

public schema = schema.create({
    gap: schema.array.optional().members(schema.object().anyMembers()),
  })

There is .anyMembers() option.

Read about it here: https://docs.adonisjs.com/reference/validator/schema/object#accept-any-elements

So your solution would be:

public schema = schema.create({
    gap: schema.array.optional().members(schema.object().anyMembers()),
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文