voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 242 forks source link

Show data if a oneOf does is invalid #394

Open dgmora opened 6 years ago

dgmora commented 6 years ago

Hi guys, I've checked the issues and couldn't find anything similar. I am trying to implement a multitype array with json schema.

"persons": {
  "description": null,
  "type": [
    "array"
  ],
  "items": {
    "oneOf": [
      {
        "$ref": "#/definitions/admin"
      },
      {
        "$ref": "#/definitions/plain_user"
      }
    ]
  }
}

. . .
"admin": {
  "type": "object",
  "properties": {
    "@type": {
      "description": null,
      "type": [
        "enum"
      ],
      "enum": [
        "admin"
      ]
    },
    "name": {
    . . .
    }
  }
}

The idea is that with @type and enum I indicate which type to validate against.

The problem I have is that if there's any error, say, I set name to a number, I get oneOf validation error like:

   The property '#/persons/5' of type object did not match any of the required schemas

This is normal, as the data did not match a oneOf, but it's not helpful for determining the error. I'd be happy to use a completely different approach, but I could not think of any.

So as a quick solution, would it make sense to show the data in the validation error, to at least see what went wrong? Something like:

  The property '#/persons/5' of type object did not match any of the required schemas:
  {
    "@type": "admin",
    "name": 42
  }

Thanks!