我在Express.js APi项目中使用https://github.com/epoberezkin/ajv
进行表单验证。
假设我有这个枚举对象:
const layout: {
BUILDING: 'BUILDING',
FLOOR: 'FLOOR',
ROOM: 'ROOM',
},
const enums = {
structure: {
HOUSE: {
type: layout.BUILDING
},
FLAT: {
type: layout.FLOOR
},
...
}
}
在AJV模式中使用如下:
...
required: ["structure"],
properties: {
structure: {
type: "string",
enum: Object.keys(enums.structure)
}
}
...
此输入对象来自表单:
const form = {
structure: 'BUILDING'
}
我需要属性floors
或rooms
还是什么都不需要,这取决于我收到的结构类型,并且我想通过AJV模式来实现。我对if/then/else conditions
和const
很熟悉,但是如果我不想对模式进行硬编码,我需要一点点不同。
我需要在AJV模式中获取structure
属性,并执行以下操作:
if(enums.structure[form.structure].type === types.BUILDING)
// Require 'floors' and 'rooms' properties
else if(enums.structure[form.structure].type === types.FLOOR)
// Require 'rooms' property
else
// Do not require properties above
但与AJV架构中的等效。
有可能吗?