亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Joi 數組減速器?

Joi 數組減速器?

SMILET 2023-04-14 15:05:07
我有一組 Joi 字符串:const item = Joi.string().max(50) const items = Joi.array().items(item).max(20)每個單獨的項目最多可以有 50 個字符,并且最多有 20 個項目。到目前為止一切順利,但是......我還必須驗證數組中所有字符串的總長度不超過 200 個字符。純粹在 Joi 中有可能嗎?
查看完整描述

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


查看完整回答
反對 回復 2023-04-14
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號