Node.js .when()在joi验证中,基于另一个键验证一个键
管理员将创建用户,在创建用户时,他将输入一些随机字符串作为密码。当管理员编辑现有用户时,他不需要输入任何密码,除非他想更改密码。如果管理员没有输入任何密码,那么将使用旧密码 所以我尝试使用Node.js .when()在joi验证中,基于另一个键验证一个键,node.js,validation,schema,hapijs,joi,Node.js,Validation,Schema,Hapijs,Joi,管理员将创建用户,在创建用户时,他将输入一些随机字符串作为密码。当管理员编辑现有用户时,他不需要输入任何密码,除非他想更改密码。如果管理员没有输入任何密码,那么将使用旧密码 所以我尝试使用.when()来处理这个案例。当有效负载中存在\u id时(仅当用户存在时才可能),我们应将密码字段设置为可选,否则密码是必需的 这是我的工作证明 const usersJoiSchema = Joi.object({ _id: Joi.string().optional(), firstName: J
.when()
来处理这个案例。当有效负载中存在\u id
时(仅当用户存在时才可能),我们应将密码字段设置为可选,否则密码是必需的
这是我的工作证明
const usersJoiSchema = Joi.object({
_id: Joi.string().optional(),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().required(),
password: Joi.string().when('_id', { is: Joi.exist(), then: Joi.optional(), otherwise: Joi.required() })
})
在这种情况下,我有一个方法。但出于某种奇怪的原因,它给了我
{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}
作为api的响应,服务器控制台中没有任何内容。
我尝试比较\u id
的字符串长度,而不是exists()
,我也尝试过
password: Joi.string().when('_id', { is: Joi.string().required(), then: Joi.string().optional(), otherwise: Joi.string().required() }),
password: Joi.string().when('_id', { is: Joi.string(), then: Joi.string().optional(), otherwise: Joi.string().required() }),
密码密钥,但仍然是相同的问题
如果有人对这个问题有任何想法,请帮助我。好吧,我创建了一个简单的测试,所有条件都是根据您的要求通过的。
我相信在你的代码中还有其他的东西发生了这个错误。
这是我的测试代码,请看一下
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = require('code').expect;
const Joi = require('joi');
const usersJoiSchema = Joi.object({
_id: Joi.string().optional(),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().required(),
password: Joi.string().when('_id', {is: Joi.exist(), then: Joi.optional(), otherwise: Joi.required()})
});
describe('Validator usersJoiSchema checks', () => {
it('It should save new user without id', () => {
const result = Joi.validate({
firstName: "Hello",
lastName: "World",
email: "hello@world.com",
password: "123456"
}, usersJoiSchema, {abortEarly: false});
expect(result.error).to.null();
expect(result.error).to.not.exist();
});
it('It should update user without password field when id included', () => {
const result = Joi.validate({
_id: "some-id-string-here",
firstName: "Hello",
lastName: "World",
email: "hello@world.com",
}, usersJoiSchema, {abortEarly: false});
expect(result.error).to.null();
expect(result.error).to.not.exist();
});
it('It should fail when id is empty and no password provided', () => {
const result = Joi.validate({
firstName: "Hello",
lastName: "World",
email: "hello@world.com",
}, usersJoiSchema, {abortEarly: false});
expect(result.error).to.not.null();
expect(result.error).to.exist();
});
it('It should fail when id is empty and password is also empty', () => {
const result = Joi.validate({
firstName: "Hello",
lastName: "World",
email: "hello@world.com",
password: "",
}, usersJoiSchema, {abortEarly: false});
expect(result.error).to.not.null();
expect(result.error).to.exist();
});
});
这是test命令
> lab --coverage-exclude test/data -m 5000n -a code -P "simple" "test/validators"
....
4 tests complete
Test duration: 8 ms
Assertions count: 8 (verbosity: 2.00)
No global variable leaks detected
有了以上内容,Joi将确保只要存在_id,就需要密码谢谢您的回答。我仍然不明白为什么它会给我500个错误。我使用的是完全相同的模式。路由或控制器中没有与模式验证相关的逻辑。500错误是毫无意义的。应用程序日志中的错误是什么?正如我在上面的描述中提到的,日志中没有错误。谢谢!为我工作!
_id: Joi.string(),
password: Joi.string().when("_id", {
is: Joi.exist(),
then: Joi.required()
})