繁体   English   中英

.when() 在 joi 验证中,根据另一个键验证一个键

[英].when() in joi validation, to validate one key based on another key

管理员将创建用户,同时他将输入一些随机字符串作为密码。 当管理员编辑现有用户时,除非他想更改密码,否则不需要输入任何密码。 如果管理员没有输入任何密码,则将使用旧密码。

所以我试图使用.when()来处理这种情况。 当有效载荷中存在_id(只有当用户存在时才可能) ,那么我们应该使密码字段可选,否则密码是必需的。

这是我的 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() })
})

Joi 有一个方法.when在这种情况下。 但出于某种奇怪的原因,它给了我

{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}

作为api的响应,服务器控制台中没有任何内容。 我也尝试比较_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();
    });
});

这是测试命令

> 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
  _id: Joi.string(),
  password: Joi.string().when("_id", {
     is: Joi.exist(),
     then: Joi.required()
  })

有了上述内容,Joi 将确保只要 _id 存在就需要密码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM