Hapi/Joi validation with nested object

妖精的绣舞 提交于 2020-05-23 08:07:09

问题


I have the following validation on one of my routes:

payload: {
    keywordGroups: Joi.array().items(Joi.object().keys({
        language: Joi.string().required(),
        containsAny: Joi.array().items(Joi.string()).default([]).when('containsAll', { is: [], then: Joi.required() }),
        containsAll: Joi.array().items(Joi.string()).default([]).when('containsAny', { is: [], then: Joi.required() }),
        notContainsAll: Joi.array().items(Joi.string()).default([]),
        notContainsAny: Joi.array().items(Joi.string()).default([])
    })).required(),
}

I'm trying to make it so that containsAny or containsAll have to include at least one string. If containsAny is empty, containsAll should have at least one item. And if containsAll is empty, containsAny should at least include one item.

But it seems Joi.when doesn't really work when it comes to an array of objects.


回答1:


You need to use Joi.alternatives() otherwise you will create a circular dependency as described in this issue.

In your is condition in the when(), you need to specify a Joi type instead of just an empty array. This example works:

import * as Joi from 'joi';

var arraySchema = Joi.array().items(Joi.object().keys({
    first: Joi.array().items(Joi.number()).default([])
        .when('second', {is: Joi.array().length(0), then: Joi.array().items(Joi.number()).required().min(1)}),
    second: Joi.array().items(Joi.number()).default([])
}));

var altArraySchema = Joi.array().items(Joi.object().keys({
    first: Joi.array().items(Joi.number()).default([]),
    second: Joi.array().items(Joi.number()).default([])
        .when('first', {is: Joi.array().length(0), then: Joi.array().items(Joi.number()).required().min(1)}),
}));

var obj = [
    {
        first: [],
        second: []
    }
];

var finalSchema = Joi.alternatives(arraySchema, altArraySchema);

var result = Joi.validate(obj, finalSchema);

console.log(JSON.stringify(result, null, 2));

The variable obj will fail the validation because both first and second are empty. Making either of them non-empty will pass the validation check.



来源:https://stackoverflow.com/questions/33770804/hapi-joi-validation-with-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!