2

I am using NewtonSoft.JSON to do some JSON schema validation. I am seeing that the validation is returning "ISValid=true" if the JSON has additional data than what is specified in the schema. Code and data is below. the schema does not have a property definition for a property called "city", the JSON data coming is has the property and a value for it. I expect the IsValid call below to return false, But it returns true. Is there a setting on the schema or on the classsuch as "Enforce strict" or something similar which will enforce that the data has all and only the dataas specified in the schema?

   public static void ValidateJsonSchema(string expectedSchema, string actualData)
   {
       JsonSchema validSchema = JsonSchema.Parse(expectedSchema);
       JObject actualJson = JObject.Parse(actualData);

       IList<string> messages;
       if (!actualJson.IsValid(validSchema, out messages))
       {
           throw new Exception("Returned data JSON schema validation failed." + messages.ToXml());
       }
   }
2
  • Have you found the answer to this question? I would like to know it too. Thank you. Commented Feb 26, 2013 at 19:29
  • Could you update tags ? Your question is not dependant of the language but only of jsonschema specifications. Commented Feb 2, 2017 at 0:01

2 Answers 2

4

Set the additionalProperties property on the schema to false so that validation will fail when additional properties are on the data you are validating.

For example if you are validating an address with street name and number but not city, then it would look like this:

{
  "title": "Address",
  "type": "object"
  "additionalProperties": false,
  "properties": {
    "streetName": {
      "type": "string"
    },
    "streetNum": {
      "type": "integer"
    }
  }
}

The above will ensure that validation will fail if any additional properties are present in the data. However, it will still pass validation if you were missing a property (eg streetName). To ensure that all properties specified are present use required on each property like this:

{
  "title": "Address",
  "type": "object"
  "additionalProperties": false,
  "properties": {
    "streetName": {
      "type": "string",
      "required": true
    },
    "streetNum": {
      "type": "integer",
      "required": true
    }
  }
}

The above will ensure both that the data contains every property and only those properties.

As a side noe, I have been unable to find any information specific to JSON.Net and schema validation, but have found the json schema site very useful for complex schema validation.

Sign up to request clarification or add additional context in comments.

Comments

2

You can also set AllowAdditionalProperties = false in validSchema object

public static void ValidateJsonSchema(string expectedSchema, string actualData)
{
   JsonSchema validSchema = JsonSchema.Parse(expectedSchema);
   validSchema.AllowAdditionalProperties = false;
   JObject actualJson = JObject.Parse(actualData);

   IList<string> messages;
   if (!actualJson.IsValid(validSchema, out messages))
   {
       throw new Exception("Returned data JSON schema validation failed." + messages.ToXml());
   }

}

1 Comment

This only works for the parent object. If you have sub-objects this will not be set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.