Which schema draft does Newtonsoft 12 use?

Viewed 79

I am working on a remote PC that only has Newtonsoft.Json.dll 12.0.3.23909 and PowerShell 5. I cannot install new software or transfer files as this tool will be used on other PCs and I can't ship other software with mine.

I am trying to validate my json againist a schema. However, regardless of what I do, IsValid is always returning true.

When my schema was draft 6, I used to get this error:

Exception calling "Parse" with "1" argument(s): "Can not convert Array to Boolean."

I changed it to be draft 3. and used "required": true instead of arrays. But now, validation always pass even if I remove a required item. I wonder what draft can I use with these obsolete methods?

[Newtonsoft.Json.Schema.JsonSchema]::Parse($Schema)
[Newtonsoft.Json.Linq.JObject]::Parse($Json)
[Newtonsoft.Json.Schema.Extensions]::IsValid($json, $schema)

This is my new draft 3 schema. It validates fine online. But in the code, it doesn't catch any errors.

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "$ref": "#/definitions/ToShareSchema",
  "definitions": {
    "ToShareSchema": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "SC": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/SCI"
          },
          "minItems": 1,
          "required": true
        },
        "MT": {
          "$ref": "#/definitions/MT",
          "required": true
        }
      },
      "title": "ToShareSchema"
    },
    "MT": {
      "type": "object",
      "required": true,
      "additionalProperties": false,
      "properties": {
        "comps": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "required": true,
          "minItems": 1,
          "uniqueItems": true
        },
        "list": {
          "type": "array",
          "items": {
            "type": "string",
            "format": "email"
          },
          "required": true,
          "minItems": 1,
          "uniqueItems": true
        }
      },
      "title": "MT"
    },
    "SCI": {
      "type": "object",
      "required": true,
      "additionalProperties": false,
      "properties": {
        "SO": {
          "type": "string",
          "minLength": 1,
          "pattern": "^[^\\s]*$"
        },
        "Bo": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "pattern": "^[^\\s]*$",
            "enum": ["One", "Two"]
          },
          "minItems": 1
        },
        "Me": {
          "type": "string",
          "minLength": 2,
          "required": true
        },
        "DR": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "minItems": 1,
          "uniqueItems": true,
          "required": true
        }
      },
      "oneOf": [
        {
          "required": ["SO"]
        },
        {
          "required": ["DR"]
        }
      ],
      "title": "SCI"
    }
  }
}
1 Answers

The base library (Newtonsoft.Json) only supports draft 4. To get later drafts, you have to use Newtonsoft.Json.Schema, which is has a paid license.

Alternatively, you can use my library for free: JsonSchema.Net, which supports all versions since draft 6.

Draft 4 is 9 years old and 4 versions behind at this point. I definitely recommend using a newer version.

Related