使用带有 ref 字段的 NjsonSchema 验证 json 模式

问题描述 投票:0回答:1

我尝试使用 NjsonSchema 验证此架构,但收到类似“:‘无法解析 JSON 路径 '/defs/product',因为没有可用的文档路径。'" 的错误。

这是我的 main-schema-json 文件:

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Image", "type": "object", "required": [ "identifier", "altText", "products", ], "properties": { "identifier": { "type": "string", "title": "Image identifier" }, "altText": { "type": "string", "title": "image alt text" }, "products": { "type": "array", "title": "list of products with markers", "items": { "$ref": "/defs/product" } } } }
这是我常用的json文件:

{ "$id": "https://inter-ikea.atlassian.net/defs/product", "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Product", "type": "object", "required": [ "dot", ], "properties": { "dot": { "title": "Dot coordinates and product", "type": "object", "required": [ "top", "left" ], "top": { "type": "number", "description": "0-1 distance from top" }, "left": { "type": "number", "description": "0-1 distance from left" } } }
这是我的代码:

using JsonSchema = NJsonSchema.JsonSchema; string mainSchemaJson = File.ReadAllText("../../.././ImageSchema.json"); var schema = await JsonSchema.FromJsonAsync(mainSchemaJson);
    
c# .net json-schema-validator resolver njsonschema
1个回答
0
投票
代码:

var schema = await JsonSchema.FromFileAsync("../../.././ImageSchema.json"); string json = @" { ""personInfo"": { ""name"": ""John"", ""age"": 30 }, ""contactInfo"": { ""email"": ""[email protected]"", ""address"": ""123 Street"" } }"; var validationErrors = schema.Validate(json); // Validate if (validationErrors.Count == 0) { Console.WriteLine("Validation successful."); } else { Console.WriteLine("Validation errors:"); foreach (var error in validationErrors) { Console.WriteLine($"- {error}"); } }
mainschema.json:

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Image", "type": "object", "required": [ "identifier", "altText", "products", ], "properties": { "identifier": { "type": "string", "title": "Image identifier" }, "altText": { "type": "string", "title": "image alt text" }, "products": { "type": "array", "title": "list of products with markers", "items": { "$ref": "./products.json" } } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.