【发布时间】:2020-09-22 23:29:29
【问题描述】:
我一直在尝试使用 Joi(旧版本 14.3.1)创建架构,不幸的是,我被卡住了。我尝试了多种方法来定义架构,但结果总是在架构定义中出现错误。
这是我的架构定义:
const allowedUser = {
admin: 'ADMIN',
normal: 'NORMAL'
};
const schema = validator
.object()
.keys({
user: validator.any().valid(Object.values(allowedUser)).required(),
context: validator.object().default({})
})
.when('user', {
is: allowedUser.admin,
then: validator.when(validator.object({ context: validator.exist() }), {
then: validator.object().keys({
session: validator.string().required()
})
})
})
.when('user', {
is: allowedUser.normal,
then: validator.when(validator.object({ context: validator.exist() }), {
then: validator.object().keys({
id: validator.string().required()
})
})
});
我尝试的另一个架构定义:
const schema = validator.object().keys({
user: validator.any().valid(Object.values(allowedUser)).required(),
context: validator
.object()
.when('user', {
is: allowedUser.admin,
then: validator.when('context', {
is: validator.exist(),
then: validator.object().keys({
session: validator.string().required()
})
})
})
.when('user', {
is: allowedUser.normal,
then: validator.when('context', {
is: validator.exist(),
then: validator.object().keys({
id: validator.string().required()
})
})
})
.default({})
});
所以schema的条件如下:
- 在
user类型ADMIN的情况下:如果定义了context属性,它应该包含属性session。如果context属性未定义,则应将默认设置为{} - 在
user类型NORMAL的情况下:如果定义了context属性,它应该包含属性id。如果context属性未定义,则应将值默认设置为{} - 隐式情况是如果
context属性包含未知属性,它也应该失败
当我运行代码时,我总是在架构定义中遇到错误,如下所示:
AssertionError [ERR_ASSERTION]: Cannot merge type object with another type: alternatives
我的最小示例代码如下:
const validator = require('joi');
const allowedUser = {
admin: 'ADMIN',
normal: 'NORMAL'
};
const bodyWhenUndefined = {
user: 'ADMIN',
message: 'Hi'
};
const bodyWhenValidDefined = {
user: 'ADMIN',
message: 'Hi'
};
const bodyWhenDefinedEmpty = {
user: 'ADMIN',
message: 'Hi 2',
context: {}
};
const bodyWhenDefinedWrong = {
user: 'admin',
message: 'Hi 3',
context: {abc: 'wrong property'}
};
const schema = validator
.object()
.keys({
user: validator.any().valid(Object.values(allowedUser)).required(),
context: validator.object().default({})
})
.when('user', {
is: allowedUser.admin,
then: validator.when(validator.object({ context: validator.exist() }), {
then: validator.object().keys({
session: validator.string().required()
})
})
})
.when('user', {
is: allowedUser.normal,
then: validator.when(validator.object({ context: validator.exist() }), {
then: validator.object().keys({
id: validator.string().required()
})
})
});
const resultSuccess = validator.validate(bodyWhenValidDefined, schema);
console.log("This is with success in validation",resultSuccess.value);
const result = validator.validate(bodyWhenUndefined, schema);
console.log("This is with default context property",result.value);
const resultWithEmptyError = validator.validate(bodyWhenDefinedEmpty, schema);
console.log("This should come with error with required property session",resultWithEmptyError.error);
const resultWithWrongProp = validator.validate(bodyWhenDefinedWrong, schema);
console.log("This should come with error with required property session",resultWithWrongProp.error);
我已经创建了一个有效的 REPL fiddle here 帮助告诉我哪里做错了。
【问题讨论】: