Matt Holmes
Matt Holmes

Reputation: 1065

Multiple schemas in the same file

I have two JSON schemas that need to share quite a bit of common definition. I would like to do this with a set of external files by reference, but I can't seem to get the latest version of Json.NET (6.0.1) to load external references. It can load externally loaded schemas using a common resolver, but it can't handle URI's of the form "someOtherSchema#somePath/to/definition". It attempts to find a schema with that exact id, completely ignoring the "#" in the middle. This is a valid URI reference as of Json Schema draft 3 (the version Json.NET is supposed to support).

So my work around is to put both schemas in the same file using the "items" collection of the schemas. So I would have something like this:

{
"id": "ourSchema",
"$schema": "http://json-schema.org/draft-03/schema",    

"items": [
    {
        "title": "Schema1",
        "type": "object",
        ...
    },
    {
        "title": "Schema2",
        "type": "object",
        ...
    }
 ]
 ...

This works and parses out correctly, and the sub schemas find their correct $ref'ed definitions and everything loads. But when I try and use one of the schemas from the items collection to validate, everything passes, even when it's clearly invalid. The code looks like this:

var reader = new JsonValidatingReader(new JsonTextReader(new StreamReader(stream))) {Schema = mySchema.Items[0]};
reader.ValidationEventHandler += (o, e) => errors.Add(e.Message);
_object = JObject.Load(reader);

Am I using this feature wrong? Is this a bug in Json.NET? At this point, I'm half tempted to make Json.NET correctly honor external references and submit a pull request, because that's the real solution to the problem. I'm just trying to get this current piece of code working so I can move on to other things in the mean time.

Upvotes: 1

Views: 1317

Answers (1)

Matt Holmes
Matt Holmes

Reputation: 1065

I figured the problem out. The schema is doing exactly what it's supposed to do, but Json.NET currently doesn't support "oneOf", which is a draft 4 construct. This was the problem, as I am using "oneOf". It doesn't fail to load the schema, but it simply ignores everything below the "oneOf".

Upvotes: 1

Related Questions