c# - 使用 Newtonsoft 在 C# 中使用 JSON Schema 验证 JSON
问题描述
使用 JSON Schema 验证 JSON 始终返回 true。Newtonsoft 用于验证,并在此处使用模式和数据进行测试。它总是返回'未发现错误。JSON 验证架构'。
请找到我的 JSON 架构。
{
"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
}
}
请找到 JSON
{
"accordion1":{
"firstname":"JSON ACCORD PALANIVELRAJAN",
"age":29
},
"accordion2":{
"firstname":"JSON ACCORD LAKSHMANAN",
"age":39
},
"accordion3":{
"firstname":null,
"age":49
}
}
我试图将名字更改为整数并删除手风琴1中的第一个。它在所有情况下都返回 true。
请指教。
请找到使用 JSON Schema 验证 JSON 的代码。
model 是一个 JObject,它是一个有效的 JSON。
JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;
解决方案
JsonSchema
已弃用,并已移至单独的包:Newtonsoft.Json.Schema。使用这个包,我能够根据您的架构验证您的 JSON(我确实删除了外部schema
元素,因为它实际上是无效的,并导致架构无法正确验证 - 我认为您可能已经在那里拥有它,因为旧JsonSchema
类否则无法解析模式!),如果我将 JSON 更改为无效形状、删除所需元素或将数据更改为无效类型,则会收到错误消息:
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
我正在使用 .NET Core 2.2、Newtonsoft.Json 12.0.2 和 Newtonsoft.Json.Schema 3.0.11,以防万一。请注意,Newtonsoft.Json.Schema 包对商业用途有限制 - 检查许可!
推荐阅读
- java - 如何将 JHawk(插件)安装到 Eclipse 中
- excel - 当日期在excel中的另一个单元格上更新时,我想增加一个单元格上的数字
- javascript - 没有 parseInt,Vue 的共享状态怎么不会更新?
- c++ - 为什么在通过引用返回运算符赋值时取消引用“this”指针?
- java - 为什么 LinkedHashSet 不支持基于索引的操作,因为它维护其元素的顺序
- javascript - 使用带有粘性导航栏的标签会将我带到错误的地方
- java - 比较法在某些情况下违反其总合同
- r - 使用 R 解决 Lucky 26 游戏
- python - 从下载文件安装模块/包
- firebase - Flutter For Web 从 Firestore 获取数据