我一直在尝试使用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({})
});
所以模式的条件如下:
- 在
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小提琴。如果您能告诉我哪里做错了,我们将不胜感激。
感谢这里的REPL。我想到了如何根据同级属性值定义嵌套模式。您可以简单地为对象提供then
条件,并定义所需的属性。这是工作模式:
const schema = validator.object().keys({
user: validator.any().valid(Object.values(allowedUser)).required(),
message: validator.string(),
context: validator
.object()
.when('user', {
is: allowedUser.admin,
then: {
session: validator.required()
}
})
.when('user', {
is: allowedUser.normal,
then: {
id: validator.required()
}
})
.default({})
});