0
votes

I want to define an optional size object. If the object is present, there should be at least one of the specified properies (min or max) given. I though of something like this:

properties:
    someval:
        type: string
    size:
        type: object
        required: false
        additionalProperties: false
        minProperties: 1
        properties:
            min:
                required: false
                type: string
            max:
                required: false
                type: string

But it seems that minProperies implies that the size must be available. At least I get this validation error if I only set min or max:

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

How can I achieve the initial specification without working with an always set size object?

1

1 Answers

0
votes

You could try to separate size types like so:

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