简体   繁体   English

使用 Ban Unknown Properties 根据架构验证 JSON

[英]Validate JSON against schema with Ban Unknown Properties

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 .这个2013 年的答案说 JSON 验证器(针对架构进行验证)应该/可以允许禁止未知属性参数仅允许架构中存在的属性,即使架构不包含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?是否有任何我可以在 C# 中使用的验证器,即使没有additionalProperties false指令也会使额外的属性失败?

I do not have control of the schema, it does not contain additionalProperties and I cannot add it.我无法控制架构,它不包含 additionalProperties 并且我无法添加它。

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:请注意,这需要一个支持 2019-09 规范草案的实现:

{
  "$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. “unevalatedProperties”是本草案中的一个新关键字,它考虑了在同一数据位置评估的其他模式——之前没有用“properties”、“additionalProperties”、“patternProperties”或“unevaluatedProperties”关键字提及的任何属性都将在这里被这个“unevalatedProperties”关键字考虑。 Since its value is false , any such matching properties will be disallowed.由于其值为false ,因此将不允许任何此类匹配属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM