首页 > 解决方案 > 要求 Joi 中的有效字符串必须在其他属性的数组中

问题描述

我想用 Joi 模式解决以下问题:

有属性locales(arra)和属性default_locale(string),后者必须在locales数组中。

我怎样才能做到这一点?到目前为止,我有以下内容:

Joi.object().keys({
    locales:
        Joi.array()
        .items(
            Joi.string()
            .valid(AllLocales)) // must be any of available locales
        .required(),
    default_locale: // this should be any of the values from locales
        Joi.string()
        .required()
});

标签: javascriptjoi

解决方案


我发现我可以简单地使用refhttps://github.com/hapijs/joi/blob/v14.3.1/API.md#refkey-options)。

default_locale: Joi.string().valid(Joi.ref('locales')).required()

推荐阅读