voxpupuli / json-schema

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

Feature Request: provide ref-like path for allOf/oneOf matches if validation fails. #298

Closed adamaig-verve closed 8 years ago

adamaig-verve commented 8 years ago

While wanting to determine programmatically why allOf and oneOf constraints are failing, I'm really wishing that the sub-schema paths that are matched could be returned. For example, given the schema below, and this input:

{
  "action_1":"call",
  "value_1":"111-2233",
  "action_2":"visit",
  "value_2":"123"
}

an error message like this would be useful: "Data does not match any schemas from /allOf/1/oneOf" (derived from http://jsonvalidate.com/)

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": ["action_1", "action_2"],
  "properties": {
    "action_1": {
      "enum": ["call", "visit"]
    },
    "value_1": {
      "type": "string"
    },
    "action_2": {
      "enum": ["call", "visit"]
    },
    "value_2": {
      "type": "string"
    }
  },
  "allOf": [{
    "type": "object",
    "oneOf": [{
      "type": "object",
      "properties": {
        "action_1": {
          "enum": ["call"]
        },
        "value_1": {
          "pattern": "^[0-9]{3}-[0-9]{4}$"
        }
      }
    }, {
      "type": "object",
      "properties": {
        "action_1": {
          "enum": ["visit"]
        },
        "value_1": {
          "pattern": "^http"
        }
      }
    }]
  }, {
    "type": "object",
    "oneOf": [{
      "type": "object",
      "properties": {
        "action_2": {
          "enum": ["call"]
        },
        "value_2": {
          "pattern": "^[0-9]{3}-[0-9]{4}$"
        }
      }
    }, {
      "type": "object",
      "properties": {
        "action_2": {
          "enum": ["visit"]
        },
        "value_2": {
          "pattern": "^http"
        }
      }
    }]
  }]
}

Currently the message returned is: "The property '#/' of type Hash did not match all of the required schemas in schema ..." Having the more specific validation failure path would allow clients to determine a closer neighborhood of failure in order to provide better feedback to users.

adamaig-verve commented 8 years ago

I realized this morning that this is already accessible via the object errors. Closing.