Ajv条件模式基于属性值true或false

时间:2017-08-16 20:42:59

标签: ajv

"data": {
    "type": "systems",
    "attributes": {
      "display_name": "Meals",
      "is_generic": false,
      "interaction_object": "BlahBlah"
    },
    "relationships": {
      "account": {
        "data": {
          "type": "accounts",
          "id": 204
        }
      },
      "venue": {
        "data": {
          "type": "venues",
          "id": 187
        }
      }
    }
  }

}

请帮忙。 :)根据财产价值调节有麻烦。 我需要根据简单的条件验证JSON:

  • 如果attributes.is_generic === false>帐户和场地属性应存在于关系属性
  • 如果attributes.is_generic === true>只有场地应该出席

提前感谢。

准备好基本模式:

type: 'object',
  properties: {
    data: {
      properties: {
        type: { type: 'string' },
        attributes: {
          properties: {
            display_name: { type: 'string' },
            is_generic: { type: 'boolean' },
            interaction_object: { type: 'string' },
          },
          required: ['display_name', 'is_generic', 'interaction_object']
        },
        relationships: {
          properties: {
            account: {
              properties: {
                data: {
                  properties: {
                    type: { type: 'string' },
                    id: { type: 'number' },
                  },
                  required: [ 'type', 'id'],
                }
              },
              required: ['data'],
            },
            venue: {
              properties: {
                data: {
                  properties: {
                    type: { type: 'string' },
                    id: { type: 'number' },
                  },
                  required: [ 'type', 'id'],
                }
              },
              required: ['data'],
            },
          },
          required: ['venue', 'account']
        }
      },
      required: ['attributes', 'relationships']
    }
  },
  required: ['data'],
  additionalProperties: false,

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

它不是一种优雅的方式,但它确实对我有用



const availableSystemSchema2 = {
  type: 'object',
  properties: {
    data: {
        oneOf: [
          {
            properties: {
              attributes: {
                properties: {
                  display_name: { type: 'string' },
                  is_generic: { enum: [false]},
                  interaction_object: { type: 'string' },
                },
                required: ['display_name']
              },
              relationships: {
                properties: {
                  account: {
                    properties: {
                      data: {
                        properties: {
                          type: { type: 'string' },
                          id: { type: 'number' },
                        },
                        required: [ 'type', 'id'],
                      }
                    },
                    required: ['data'],
                  },
                  venue: {
                    properties: {
                      data: {
                        properties: {
                          type: { type: 'string' },
                          id: { type: 'number' },
                        },
                        required: [ 'type', 'id'],
                      }
                    },
                    required: ['data'],
                  },
                },
                required: ['venue', 'account']
              }
            },
            required: ['attributes', 'relationships']
          },
          {
            properties: {
              attributes: {
                properties: {
                  display_name: { type: 'string' },
                  is_generic: { enum: [true]},
                  interaction_object: { type: 'string' },
                },
                required: ['display_name']
              },
              relationships: {
                properties: {
                  venue: {
                    properties: {
                      data: {
                        properties: {
                          type: { type: 'string' },
                          id: { type: 'number' },
                        },
                        required: [ 'type', 'id'],
                      }
                    },
                    required: ['data'],
                  },
                },
                required: ['venue']
              }
            },
            required: ['attributes', 'relationships']
          }
        ]
      }
  },
  required: ['data'],
  additionalProperties: false,
}