在 Joi 中需要有效的字符串,它必须在其他属性的数组中

Require valid string in Joi which must be in other propertie's array

我有以下问题想用 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()
});

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

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