Search code examples
c#.netjson-schema-validatorresolvernjsonschema

validate json schema using NjsonSchema with ref field


I tried to validate this schema using NjsonSchema but I getting error like ": 'Could not resolve the JSON path '/defs/product' because no document path is available.'"

this is my main-schema-json file:

{
  "$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"
      }
    }
  }
}

this is my common json file:

{
 "$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"
     }
   }
 }

this is my code:

using JsonSchema = NJsonSchema.JsonSchema;
string mainSchemaJson = File.ReadAllText("../../.././ImageSchema.json");

var schema = await JsonSchema.FromJsonAsync(mainSchemaJson);

Solution

  • Don't use FromJsonAsync use FromFileAsync

    code:

    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"
          }
        }
      }
    }