I want to validate a json input through json schema. The positive case works for intended objects and properties. But I want to validate against extra objects, parameters which are not mentioned in the schema.
Basically fail the validation if garbage data detected in the json
JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
Additional Properties The value of the additionalProperties keyword is a schema that will be used to validate any properties in the instance that are not matched by properties or patternProperties . Setting the additionalProperties schema to false means no additional properties will be allowed.
JSON Schema validation asserts constraints on the structure of instance data. An instance location that satisfies all asserted constraints is then annotated with any keywords that contain non-assertion information, such as descriptive metadata and usage hints.
If you want to only have a certain set of properties in JSON objects and refuse others:
properties
and patternProperties
,define additionalProperties
to false
:
{
"type": "object",
"properties": { "p": {}, "q": {} },
"additionalProperties": false
}
will only allow for properties p
and q
to exist in object instances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With