raml-org / raml-spec

RAML Specification
http://raml.org
3.87k stars 858 forks source link

Question for validating union types #643

Open sichvoge opened 7 years ago

sichvoge commented 7 years ago

Assume we have the following type definitions:

types:
  EventType:
    type: object
    properties:
      eventTypeId: 
        type:  string
      eventTypeName:
        type:  string
      eventTypeCategory:
        enum:  [alert,notification,business,device,status]
  TemperatureSensorEvent:  
    type: EventType
    properties:
       temperature: number
       unit:  
         enum:  ['metric','imperial']
  HumiditySensortEvent:  
    type: EventType
  Event:
    type: object
    properties:
      payload:
        type:  TemperatureSensorEvent | HumiditySensortEvent

And the following example:

[
  {
    "payload": {
      "eventTypeId": "1234",
      "eventTypeName": "IoTEvent",
      "eventTypeCategory": "device",
      "temperature": "2.3"
    }
  }
]

What should be the type this example validates against? TemperatureSensorEvent or HumiditySensortEvent? Is this example above valid or not?

petrochenko-pavel-a commented 7 years ago

In your example you use: TemperatureSensorEvent | TemperatureSensorEvent so it should validate against TemperatureSensorEvent type and it will not pass validation.

If you will change TemperatureSensorEvent | TemperatureSensorEvent to TemperatureSensorEvent | HumiditySensortEvent

then it will validate against HumiditySensortEvent and should pass validation with a warning about unknown property temperature

Regards, Pavel

sichvoge commented 7 years ago

OH, sorry the example should be TemperatureSensorEvent | HumiditySensortEvent. My mistake. @petrochenko-pavel-a can you also explain why it does validate against HumiditySensortEvent?

petrochenko-pavel-a commented 7 years ago

It has every HumiditySensortEvent required property and one additional property which is not defined in HumiditySensortEvent as we all agreed RAML types are open by default , but in examples we are marking unknown properties as warnings.

Regards, Pavel