2

I am trying to validate a small bit of JSON like:

{
    "success": true,
    "message": "all's good!"
}

which works with the schema:

{
    "type": "object",
    "properties": {
        "success": { "type": "boolean" },
        "message": { "type": "string"  }
    }
}

however it fails with the schema

{
    "definitions": {
        "response": {
            "type": "object",
            "properties": {
                "success": { "type": "boolean" },
                "message": { "type": "string"  }
            }
        }
    },

    "type": { "$ref": "#/definitions/response" }
}

with the error

java.lang.AssertionError: schema resource:/json-schema/sample.schema.json was > invalid: fatal: invalid JSON Schema, cannot continue Syntax errors:

[ {
    "level" : "error",
    "message" : "value has incorrect type (found object, expected one of [array, string])",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "resource:/json-schema/sample.schema.json#",
        "pointer" : ""
    },
    "keyword" : "type",
    "found" : "object",
    "expected" : [ "array", "string" ]
} ]

level: "fatal"

are you not allowed to use a reference for a type outside the definitions section? My motivation is that this is a response to a singular case, but there are cases where this structure is nested in others as well.

If it matters I'm using json-schema-validator version 2.2.6.


PS - this is a simplified example, the actual schema is more complicated as to justify why reuse and not copying and pasting is desirable.

Sled
  • 18,541
  • 27
  • 119
  • 168
  • I am confused as to why you would declare your top level type as a definition. Can you a slightly more complete example? You can reuse definitions among properties and property values. – Adam Oct 26 '16 at 14:48
  • @AdamRosini I have one call that returns a response, but I also have a second call that does batch work returns a map of 1 ids to responses. I want to reuse the response declaration. To be able to reuse the definition AFAIK it has to be in a definition, or can you reuse non-definition type in another file? – Sled Oct 26 '16 at 15:01

3 Answers3

2

You can use "id" and "$ref".

id for identifying, e.g.:

{
    "type": "object",
      "id": "#response",
      "properties": {
        "success": { "type": "boolean" },
        "message": { "type": "string"  }
       }
  }
}

And then you use $ref, e.g.:

"some": { "$ref": "#response" }

or external ref:

"ext": { "$ref": "http://url.com#response" }

See http://json-schema.org/latest/json-schema-core.html#anchor27

Pedro
  • 1,875
  • 12
  • 15
1

The value of the type keyword must be a string of the name of one of the JSON primitave types (e.g. "string", "array", etc.), or an array of these strings. That is what the error message is saying. Keyword type must be a string or an array. The closest thing to what I think you are trying to do is this ...

{
    "definitions": {
        "response": {
            "type": "object",
            "properties": {
                "success": { "type": "boolean" },
                "message": { "type": "string"  }
            }
        }
    },
    "allOf": [{ "$ref": "#/definitions/response" }]
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
0

You should declare your definition in it's own file, and they have your types refer to that file reference. See How to manage multiple JSON schema files? for details.

Community
  • 1
  • 1
Adam
  • 2,214
  • 1
  • 15
  • 26