gpt4 book ai didi

javascript - 如何确保一个对象数组只包含一个带有 Joi 的特定键?

转载 作者:行者123 更新时间:2023-12-05 00:32:28 25 4
gpt4 key购买 nike

我有类似的东西:

  let moduleId

moduleRackOutputs.forEach((output) => {
if (!moduleId) {
moduleId = output.moduleId
} else if (output.moduleId !== moduleId) {
errors.add([
'The drawing contains more than one module type. Multiple module types are not yet supported by the PVsyst RPA.'
])
}
})
我想将其转换为 Joi架构。我将如何做到这一点?
谢谢

最佳答案

这可以通过以下方式完成:

Joi.array()
.items(
Joi.object().keys({
moduleId: Joi.string().required(),
framingType: Joi.string().required()
})
)
.unique((a, b) => a.moduleId !== b.moduleId)
.message({
'array.unique': 'The drawing contains more than one module type...'
})
.unique((a, b) => a.framingType !== b.framingType)
.message({
'array.unique': 'The drawing contains more than one framing type...'
})

关于javascript - 如何确保一个对象数组只包含一个带有 Joi 的特定键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70623285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com