我想定义一个可选的size
对象。如果存在该对象,则应至少提供一个指定的属性(min
或max
)。我虽然是这样的:
properties:
someval:
type: string
size:
type: object
required: false
additionalProperties: false
minProperties: 1
properties:
min:
required: false
type: string
max:
required: false
type: string
但是似乎minProperies
意味着size
必须可用。如果仅设置最小值或最大值,至少会出现此验证错误:
<pre>TypeError: Cannot convert undefined or null to object<br> at Function.keys (<anonymous>)<br> at MinProperties.extractValue (C:\mypath\node_modules\raml-typesystem\dist\src\restrictions.js:1059:23)<br> at MinProperties.MinMaxRestriction.check (C:\mypath\node_modules\raml-typesystem\dist\src\restrictions.js:775:22)<br> at C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1564:89<br> at Array.forEach (<anonymous>)<br> at InheritedType.AbstractType.validateDirect (C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1564:37)<br> at InheritedType.AbstractType.validate (C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1612:34)<br> at C:\mypath\node_modules\raml-validate\raml-validate.js:308:31<br> at C:\mypath\node_modules\raml-validate\raml-validate.js:405:18<br> at Array.map (<anonymous>)</pre>
如何在不使用始终设置为size
的对象的情况下实现初始规范?
答案 0 :(得分:0)
您可以尝试将尺寸类型分开,例如:
#%RAML 1.0
baseUri: https://mocksvc.qax.mulesoft.com/mocks/9a2d6433-e5f4-47c6-9c41-8701efcc9d9b
title: test
version: 1
protocols: [HTTP]
mediaType: application/json
types:
MinType:
type: object
additionalProperties: false
properties:
min:
type: string
required: true
MaxType:
type: object
additionalProperties: false
properties:
max:
type: string
required: true
MyType:
type: object
properties:
someval:
type: string
size:
type: MinType | MaxType
additionalProperties: false
required: false
/newResource:
displayName: resourceName
post:
body:
type: MyType
example: {"someval": "someval", "size" :{ "max": "1"}}