首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json-schema重复代码

json-schema重复代码
EN

Stack Overflow用户
提问于 2016-08-19 22:26:44
回答 1查看 738关注 0票数 1

此模式在我的项目中反复出现(六次):

代码语言:javascript
复制
        type: object
          properties:
            total:
              type: integer
              description: the count of all items that match the query
            hits:
              type: array
              description: a single page of results
              items:
                $ref: '#/definitions/{various schema}'

这种重复模式({various schema})的内部部分因每次使用而不同。我希望引用每个共享代码,而不是重复我自己。我通常使用$ref,但由于变量位,这里似乎不起作用。

我试图让anyOf为我自己工作,但它只有助于改变objectitems,但我试图改变arrayitems

我遗漏了什么吗?可能是一次小的重构,以使其适合于可重用模式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-22 08:53:34

除了items约束之外,您可以定义重复的模式,然后在每个变体中使用allOf

您的可重用模式将是这样的:

代码语言:javascript
复制
  reusable:
    type: object
      properties:
        total:
          type: integer
          description: the count of all items that match the query
        hits:
          type: array
          description: a single page of results

当您想要定义一个变体时,可以使用allOf添加可重用模式和附加约束:

代码语言:javascript
复制
variation1:
  allOf:
    - reusable
    - properties:
        hits:
          items:
            $ref: '#/definitions/variation_schema'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39048481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档