【发布时间】:2020-01-29 09:21:31
【问题描述】:
我有一个大四学生,我必须验证以下 json 数据的架构。
{
'userId': 123,
'userType': CUSTOMER
}
关于 JSON 的信息:userId 是整数,userType 是枚举['Customer','Admin','Guest']
所以问题是我想验证来自 JSON 模式的 JSON 数据,基于:
- 如果存在
userId,则需要userType。 - 如果存在
userType['Customer','Admin']但不存在 userId,则不应验证 JSON 数据。 - 但是如果
userType是['Guest']那么他们的userId 是必需的。
这里我已经达到了第 1 点,但无法达到第 2 点和第 3 点:
{
'type': 'object',
'properties': {
'user': {
'type': 'integer',
'minimum': 0
},
'userType': {
'type': 'string',
'enum': ['Customer','Admin','Guest'],
}
},
'dependencies': {
'userId': ['userType']
}
}
谁能为此建议我 json 架构解决方案?
【问题讨论】:
标签: javascript json jsonschema json-schema-validator