简体   繁体   English

JOI 验证数组是否只有一个匹配项。 不超过一个

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

example arrays :例如 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
    }]
};

I want to validate that there must be a creator ( isCreator: true ) in people array, but only one, not more我想验证people数组中必须有一个创建者isCreator: true ),但只有一个,不能更多

My schema looks like this:我的架构如下所示:

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);
  • "first" array is returning success which is ok as there's only one creator “第一个”数组返回成功,这没关系,因为只有一个创建者
  • "second" array is returning failure which is ok as there's not a single creator “第二个”数组返回失败,这没关系,因为没有一个创建者

But

  • "third" array is returning success which is wrong as there're 2 matching items ie 2 creators. “第三个”数组返回成功,这是错误的,因为有 2 个匹配项,即 2 个创建者。 And this is my issue actually实际上是我的问题

You can use unique and compare your isCreator field:您可以使用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)
})

This way, you can only have one object with isCreator=true .这样,您只能拥有一个isCreator=true的 object 。

If you send this object:如果您发送此 object:

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

You will get the following error:您将收到以下错误:

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

I have found my answer with the help of soltex's one .我在soltex 的帮助下找到了答案。 Have to make some amendments:必须做一些修改:

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
    )
});

Now all possible test cases are working absolutely as required.现在所有可能的测试用例都完全按要求工作。 Thanks for the help @soltex感谢@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