【问题标题】:Ajv conditional schema based on property value true or false基于属性值 true 或 false 的 Ajv 条件模式
【发布时间】:2017-08-16 20:42:58
【问题描述】:
"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,

谢谢你的帮助

【问题讨论】:

    标签: ajv


    【解决方案1】:

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

    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,
    }

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2018-07-26
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多