0

This answer from 2013 says that JSON validators (validating against a schema) should/can allow a ban unknown properties parameter to only allow properties that exist in the schema, even if the schema does not contain additionalProperties false. However as far as I can see all paths to this property seem to go cold.

Is there any validator that I can use in C# that will fail extra properties even without the additionalProperties false directive?

Jamie Kitson
  • 3,973
  • 4
  • 37
  • 50
  • Does this answer your question? [Json Schema validation: do not allow fields other than those declared in schema](https://stackoverflow.com/questions/27224310/json-schema-validation-do-not-allow-fields-other-than-those-declared-in-schema) – Drag and Drop Oct 29 '20 at 09:20
  • 1
    Sorry I have trouble understanding , are you asking : "where did the `ban unknown properties` went?" or Do you have a specific case where using `additionalProperties false` fails? – Drag and Drop Oct 29 '20 at 09:30
  • @DragandDrop I do not have control of the schema, it does not contain `additionalProperties` and I cannot add it. However I still want my validation to fail if properties exist in the JSON that do not exist in the schema. – Jamie Kitson Oct 29 '20 at 13:34
  • That's good infos. Should be in the question. Cause it was unclear why you wanted something from 2013, when other alternative where present in the thread. You can even go all the way to [mre], with 3lines json and a 5lines schema. If you are restricted to a particular set of lib it will be a nice addition too. – Drag and Drop Oct 29 '20 at 14:32

1 Answers1

1

I do not have control of the schema, it does not contain additionalProperties and I cannot add it.

You can write your own schema, that says "use this schema over here: " and then add on additional keywords to it. Note that this requires an implementation that supports the draft 2019-09 specification:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$ref": "... uri to the schema you wish to modify",
  "unevaluatedProperties": false
}

"unevaluatedProperties" is a new keyword in this draft that takes other schemas evaluated at the same data location into account -- any property that was not earlier mentioned with a "properties", "additionalProperties", "patternProperties" or "unevaluatedProperties" keyword will be considered by this "unevaluatedProperties" keyword here. Since its value is false, any such matching properties will be disallowed.

Ether
  • 53,118
  • 13
  • 86
  • 159