1

我在 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

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

4

1 回答 1

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

于 2020-05-14T17:04:09.887 回答