【发布时间】:2017-06-02 17:09:12
【问题描述】:
我想验证以下 json 架构,我正在使用 Ajv npm 包。
{
"email": "xyzmail@gmail.com",
"phone": "1112223334",
"country_code": "91"
}
我只想要 email,或者只想要 phone 和 country_code,或者应该有三个属性。
我尝试过 oneOf、allOf、anyOf 也尝试过嵌套主题,但在某些情况下它可以工作,而在某些情况下它不起作用。
我试过下面的代码
{
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"maxLength": constants.LENGTHS.EMAIL.MAX
},
"phone": {
"type": "string",
"pattern": constants.REGEX.PHONE,
"maxLength": constants.LENGTHS.PHONE.MAX
},
"country_code": {
"type": "string",
"pattern": constants.REGEX.COUNTRY_CODE,
"maxLength": constants.LENGTHS.COUNTRY_CODE.MAX
}
},
"anyOf": [
{
"required": ["email"],
},
{
"required": ["phone", "country_code"],
},
{
"required": ["email", "phone", "country_code"]
},
],
"additionalProperties": false
}
【问题讨论】: