json-schema重复代码
json-schema repeated code
此模式在我的项目中重复出现(六次):
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
为我工作,但它只能帮助改变 object
的 properties
,但我正在尝试改变 items
array
.
有什么我想念的吗?可能是一个小的重构以使其适合可重用的模式?
您可以定义重复模式,但 items
约束除外,然后在每个变体中使用 allOf
。
您的可重用模式应该是这样的:
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
添加可重用架构和附加约束:
variation1:
allOf:
- reusable
- properties:
hits:
items:
$ref: '#/definitions/variation_schema'
此模式在我的项目中重复出现(六次):
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
为我工作,但它只能帮助改变 object
的 properties
,但我正在尝试改变 items
array
.
有什么我想念的吗?可能是一个小的重构以使其适合可重用的模式?
您可以定义重复模式,但 items
约束除外,然后在每个变体中使用 allOf
。
您的可重用模式应该是这样的:
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
添加可重用架构和附加约束:
variation1:
allOf:
- reusable
- properties:
hits:
items:
$ref: '#/definitions/variation_schema'