【问题标题】:How to have two raml properties mutually exclusive?如何让两个 raml 属性互斥?
【发布时间】:2020-08-29 15:15:18
【问题描述】:

我在 raml1.0 中有一个具有 4 个属性的类型,我需要实现这种情况: 四个属性中的两个仅独占存在,因此如果其中一个存在,则另一个不应该存在,并且如果它们都发生,则会向用户抛出适当的错误消息。

例如:

types:
  TypeOne:
    description: "Need the first two properties exist only mutually exclusively"
    type: object
    additionalProperties: false
    properties:
      Prop1:
        description: "This is the first property"
        type: string
        required: true
      Prop2:
        description: "This should not exist if Prop1 exist"
        type: String
        required: true (only if Prop1 does not exist) 
      Prop3:
        description: "This is optional if Prop1 exists"
        type: string
        required: false
      Prop4:
        description: "This is optional if Prop2 exists"
        type: string
        required: false

非常感谢任何帮助。顺便说一句,这些类型中的每一个都是一个复杂的对象。我这里只是为了演示而简化了。

【问题讨论】:

    标签: raml-1.0


    【解决方案1】:

    试试这个:

    types:
      Base:
        properties:
          Prop3:
            description: "This is optional if Prop1 exists"
            type: string
            required: false
          Prop4:
            description: "This is optional if Prop2 exists"
            type: string
            required: false
      TypeOne:
        type: Base
        additionalProperties: false
        properties:
          Prop1:
            description: "This is the first property"
            type: string
            required: true
      TypeTwo:
        type: Base
        additionalProperties: false
        properties:
          Prop2:
            description: "This is the first property"
            type: string
            required: true
      MainType:
        type: TypeOne | TypeTwo
    

    联合类型的文档可以在这里找到:https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#union-type

    【讨论】:

    • 谢谢,这解决了我的问题。感谢您的帮助,乔治!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多