使用joi对照另一个数组的长度验证一个数组的长度 [英] Validating an array's length against another array's length using joi

查看:84
本文介绍了使用joi对照另一个数组的长度验证一个数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用joi验证两个数组的长度是否相同?

Is there a way to validate that two arrays need to have the same length using joi?

这是一个例子:

Joi.object().keys({
  firstNames: Joi.array().items(Joi.string()).single(),
  lastNames: Joi.array().items(Joi.string()).single(),
});

如果这行得通,它还应该匹配两个数组的长度,因此没有 firstName 缺少 lastName .

If that was to work, it should also match both array's length so no firstName lacks a lastName.

感谢您的帮助!

推荐答案

当然有,请查看

There certainly is, take a look at .assert(). You can use it to compare the values or attributes of two properties in your object.

对于您的示例,您可以执行以下操作:

For your example you can do this:

Joi.object().keys({
  firstNames: Joi.array().items(Joi.string()).single(),
  lastNames: Joi.array().items(Joi.string()).single(),
}).assert('firstNames.length', Joi.ref('lastNames.length'));

您还可以选择提供更有用的错误消息,作为 .assert()的第三个参数.

You can also optionally provide a more helpful error message as the third parameter of .assert().

这篇关于使用joi对照另一个数组的长度验证一个数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆