javascript - Joi 中 any.when() 的异常行为
问题描述
const Joi = require('@hapi/joi')
var schema_1 = Joi.object({
a: Joi.number().integer(),
b: Joi.number().integer()
}).when(Joi.object({
'a': Joi.number().valid(5),
'b': Joi.number().valid(10),
}), {then: Joi.any().forbidden()})
var schema_2 = Joi.object({
a: Joi.number().integer(),
b: Joi.number().integer()
}).when(Joi.object({
'a': Joi.number().valid(5),
}), {then: Joi.any().forbidden()})
var object = {
a: 5,
b: 10
}
schema_1.validate(object) // this throws ValidationError
schema_2.validate(object) // this does not throw any error
我也出现错误schema_2
为什么
没有schema_2
显示任何错误?
解决方案
不要忘记b
在schema_2
.
const Joi = require('@hapi/joi')
var schema_1 = Joi.object({
a: Joi.number().integer(),
b: Joi.number().integer()
}).when(Joi.object({
'a': Joi.number().valid(5),
'b': Joi.number().valid(10),
}), {then: Joi.any().forbidden()})
var schema_2 = Joi.object({
a: Joi.number().integer(),
b: Joi.number().integer()
}).when(Joi.object({
'a': Joi.number().valid(5),
'b': Joi.any(),
}), {then: Joi.any().forbidden()})
var object = {
a: 5,
b: 10
}
schema_1.validate(object) // this throws ValidationError
schema_2.validate(object) // this does not throw any error
这是结果。
const x = schema_1.validate(object) // this throws ValidationError
const y = schema_2.validate(object) // this does not throw any error
console.log(x)
console.log(y)
{ value: { a: 5, b: 10 },
error:
{ ValidationError: "value" is not allowed _original: { a: 5, b: 10 }, details: [ [Object] ] } }
{ value: { a: 5, b: 10 },
error:
{ ValidationError: "value" is not allowed _original: { a: 5, b: 10 }, details: [ [Object] ] } }
推荐阅读
- typescript - 在 ASP.NET Core 应用程序中编译打字稿时如何包含引导表的类型
- asp.net - 为什么 Dokku 总是重定向到默认应用程序?
- sql - Oracle 在日志中保存更新的行
- gulp - Gulp:如何将多个 .pug 文件编译成文件夹内的多个 index.html 文件
- quill - 以正确的样式显示 Quill 编辑器内容
- python - 如何使用 discord.py API 中的 discord.Reaction.count?
- javascript - chrome.storage.local 在存储时更改对象
- ios - 在 Firebase IOS 中使用自定义 SMTP 服务器
- node.js - auth0 节点,js api 检查权限
- firebase - Firebase Cloud Function 触发器上下文不包含身份验证信息