Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating JSON with JSON Schema in C# using Newtonsoft

Validate the JSON with JSON Schema return always true. Newtonsoft is used for validation and tested here with schema and data. It return always 'No errors found. JSON validates against the schema'.

Please find my JSON Schema.


{
  "schema": {
    "definitions": {
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "widget": { "formlyConfig": { "type": "accordion" } },
    "title": "The Root Schema",
    "required": [
      "accordion1",
      "accordion2",
      "accordion3"
    ],
    "properties": {
      "accordion1": {
        "$id": "#/properties/accordion1",
        "type": "object",
        "title": "The Accordion1 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion1/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstname pvr1"
          },
          "age": {
            "$id": "#/properties/accordion1/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 21
          }
        }
      },
      "accordion2": {
        "$id": "#/properties/accordion2",
        "type": "object",
        "title": "The Accordion2 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion2/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstName2"
          },
          "age": {
            "$id": "#/properties/accordion2/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 31
          }
        }
      },
      "accordion3": {
        "$id": "#/properties/accordion3",
        "type": "object",
        "title": "The Accordion3 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion3/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstnaem3"
          },
          "age": {
            "$id": "#/properties/accordion3/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 10
          }
        }
      }
    },
      'additionalProperties': false
  }
}


Please find JSON

{ 
   "accordion1":{ 
      "firstname":"JSON ACCORD PALANIVELRAJAN",
      "age":29
   },
   "accordion2":{ 
      "firstname":"JSON ACCORD LAKSHMANAN",
      "age":39
   },
   "accordion3":{ 
      "firstname":null,
      "age":49
   }
}

I tried to change the first name to integer and remove the first in accordion1. It return true for all cases.

Please advise.

Please find the code which validate the JSON with JSON Schema.

model is a JObject and it is a valid JSON.

JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;

like image 749
user3497702 Avatar asked Jan 26 '23 19:01

user3497702


1 Answers

JsonSchema is deprecated, and has moved to a separate package: Newtonsoft.Json.Schema. Using this package, I was able to validate your JSON against your schema (I did remove the outer schema element, as it is actually invalid, and causes schema to not validate properly - I think you might've had it in there because the old JsonSchema class could not parse the schema otherwise!), and get error messages if I changed the JSON to an invalid shape, removed required elements, or changed data to invalid types:

            string data = File.ReadAllText("data.json");
            string schema = File.ReadAllText("data.schema.json");

            var model = JObject.Parse(data);
            var json_schema = JSchema.Parse(schema);

            IList<string> messages;
            bool valid = model.IsValid(json_schema, out messages); // properly validates

I am using .NET Core 2.2, Newtonsoft.Json 12.0.2, and Newtonsoft.Json.Schema 3.0.11, just in case it matters. Please note, the Newtonsoft.Json.Schema package has limitations for commercial use - check licensing!

like image 99
CoolBots Avatar answered Feb 04 '23 09:02

CoolBots