1 回答

TA貢獻1712條經驗 獲得超3個贊
看起來您可以使用any.custom 方法并向您傳遞自定義驗證邏輯。
基于該文檔,我們首先需要創建一個函數來驗證接受兩個參數的字符串數組,即“值”和“助手”對象。
const contentsLength = (value, helpers) => {
? // do a map reduce to calculate the total length of strings in the array
? const len = value.map((v) => v.length).reduce((acc, curr) => acc + curr, 0);
? // make sure that then length doesn't exceed 20, if it does return an error using
? // the message method on the helpers object?
? if (len > 200) {
? ? return helpers.message(
? ? ? "the contents of the array must not exceed 200 characters"
? ? );
? }
? // otherwise return the array since it's valid
? return value;
};
現在將它添加到您的items架構中
const items = Joi.array().items(item).max(20).custom(contentsLength);
添加回答
舉報