tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 262 forks source link

missing property "name", but which "name" if nested #272

Closed littlebee closed 5 years ago

littlebee commented 5 years ago

I have a schema with nested "required"s like this:

{
  "$id": "http://json-schema.org/draft-04/schema#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "version": "0.2.0",
  "type": "object",
  "title": "exampleNested",
  "description": "Example validation for nested schema",
  "required": ["author", "editor"],
  "additionalProperties": true,
  "properties": {
    "author": {
      "$ref": "#/definitions/person"
    },
    "editor": {
      "$ref": "#/definitions/person"
    },
    "description": {
      "$ref": "#/definitions/descriptionText"
    }
  },
  "definitions": {
    "person": {
      "type": "object",
      "required": ["name"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "$ref": "#/definitions/personName"
        },
        "email": {
          "$ref": "#/definitions/personEmail"
        }
      }
    },
    "personName": {
      "type": "string",
      "minLength": 2,
      "pattern": "[a-zA-Z]"
    },
    "personEmail": {
      "type": "string",
      "minLength": 2,
      "pattern":
        "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"
    },
    "descriptionText": {
      "type": "string",
      "minLength": 1
    }
  }
}

when I validate JSON missing just one of the two required 'name' attributes, like this:

{
  "description":  "example description",
  "author": {
      "name": "Me",
      "email": "me@memyselfandi.org"
  },
  "editor": {
      "email": "editor@someplaceeditorswork.com"
  }
}

The error is 'requires property "name"'

Is there anyway to indicate which of the two "name"s are missing?

littlebee commented 5 years ago

nevermind. It looks like the stack attribute on the errors returned has a more complete error message.

Thank you for this awesome lib!