簡體   English   中英

JOI 驗證數組是否只有一個匹配項。 不超過一個

[英]JOI validate if array has only single matching item. Not more than one

例如 arrays

const first = {
    class: 'XYZ',
    people: [{
        id: 1,
        name: "John",
        isCreator: false
    }, {
        id: 2,
        name: "Alex",
        isCreator: true
    }]
};
const second = {
    class: 'XYZ',
    people: [{
        id: 1,
        name: "John",
        isCreator: false
    }, {
        id: 2,
        name: "Alex",
        isCreator: false
    }]
};
const third = {
    class: 'XYZ',
    people: [{
        id: 1,
        name: "John",
        isCreator: true
    }, {
        id: 2,
        name: "Alex",
        isCreator: true
    }]
};

我想驗證people數組中必須有一個創建者isCreator: true ),但只有一個,不能更多

我的架構如下所示:

const schema = Joi.object().keys({
    class: Joi.string().required(),
    people: Joi.array().items(
        Joi.object().keys({
            id: Joi.number().integer().positive().required(),
            name: Joi.string().alphanum().required(),
            isCreator: Joi.boolean().required()
        })
    ).min(2)
    .has(Joi.object().keys({
        id: Joi.number(),
        name: Joi.string(),
        isCreator: Joi.valid(true)  // <-- this
    })).required()
});
schema.validate(first);
schema.validate(second);
schema.validate(third);
  • “第一個”數組返回成功,這沒關系,因為只有一個創建者
  • “第二個”數組返回失敗,這沒關系,因為沒有一個創建者

  • “第三個”數組返回成功,這是錯誤的,因為有 2 個匹配項,即 2 個創建者。 實際上是我的問題

您可以使用unique並比較您的isCreator字段:

Joi.object().keys({
    class: Joi.string().required(),
    people: Joi.array().items(
        Joi.object().keys({
            id: Joi.number().integer().positive().required(),
            name: Joi.string().alphanum().required(),
            isCreator: Joi.boolean()
        })
    ).unique((a, b) => a.isCreator !== false)
})

這樣,您只能擁有一個isCreator=true的 object 。

如果您發送此 object:

{
    class: 'XYZ',
    people: [{
        id: 1,
        name: "John",
        isCreator: false
    }, {
        id: 2,
        name: "Alex",
        isCreator: true
    },{
        id: 3,
        name: "Math",
        isCreator: true
    }]
}

您將收到以下錯誤:

Validation Error: "people[2]" contains a duplicate value

我在soltex 的幫助下找到了答案。 必須做一些修改:

const schema = Joi.object().keys({
    class: Joi.string().required(),
    people: Joi.array().items(
        Joi.object().keys({
            id: Joi.number().integer().positive().required(),
            name: Joi.string().alphanum().required(),
            isCreator: Joi.boolean().required()
        })
    )
    // re-added this part
    .has(Joi.object().keys({
        id: Joi.number(),
        name: Joi.string(),
        isCreator: Joi.valid(true)
    }))
    .unique((a, b) =>
        a.isCreator !== false /* also added this --> */ && b.isCreator == a.isCreator
    )
});

現在所有可能的測試用例都完全按要求工作。 感謝@soltex 的幫助

people: [{..., isCreator: false}, {..., isCreator: false}, {..., isCreator: false}]
// is returning error "people" does not contain at least one required match 👍

people: [{..., isCreator: true}, {..., isCreator: false}, {..., isCreator: true}]
// is returning error "people[2]" contains a duplicate value 👍

people: [{..., isCreator: true}, {..., isCreator: false}, {..., isCreator: false}]
people: [{..., isCreator: false}, {..., isCreator: true}, {..., isCreator: false}]
// are passing successfully 👍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM