RAML 1.0:在可选对象中定义 minProperties

Posted

技术标签:

【中文标题】RAML 1.0:在可选对象中定义 minProperties【英文标题】:RAML 1.0: define minProperties inside optional object 【发布时间】:2019-03-30 21:43:37 【问题描述】:

我想定义一个可选的size 对象。如果对象存在,则应至少给出一个指定的属性(minmax)。我想到了这样的事情:

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> &nbsp; &nbsp;at Function.keys (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at MinProperties.extractValue (C:\mypath\node_modules\raml-typesystem\dist\src\restrictions.js:1059:23)<br> &nbsp; &nbsp;at MinProperties.MinMaxRestriction.check (C:\mypath\node_modules\raml-typesystem\dist\src\restrictions.js:775:22)<br> &nbsp; &nbsp;at C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1564:89<br> &nbsp; &nbsp;at Array.forEach (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at InheritedType.AbstractType.validateDirect (C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1564:37)<br> &nbsp; &nbsp;at InheritedType.AbstractType.validate (C:\mypath\node_modules\raml-typesystem\dist\src\typesystem.js:1612:34)<br> &nbsp; &nbsp;at C:\mypath\node_modules\raml-validate\raml-validate.js:308:31<br> &nbsp; &nbsp;at C:\mypath\node_modules\raml-validate\raml-validate.js:405:18<br> &nbsp; &nbsp;at Array.map (&lt;anonymous&gt;)</pre>

如何在不使用始终设置的size 对象的情况下实现初始规范?

【问题讨论】:

【参考方案1】:

您可以尝试像这样分隔尺寸类型:

#%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"

【讨论】:

谢谢!我会在明年恢复工作后立即检查 -.-*。只有一个问题:现在哪个设置允许指定 MaxType AND MinType?

以上是关于RAML 1.0:在可选对象中定义 minProperties的主要内容,如果未能解决你的问题,请参考以下文章

如何在 RAML 1.0 中覆盖对象数组属性类型

RAML 1.0,映射类型复杂的正则表达式

JUnit如何在可选和对象上断言True / Equals

RAML 1.0 Example DataType 的具体示例

如何在可选类型中使用 numpy

RAML 1.0 - 单个响应的多个示例