Jsontron for .NET

The NuGet package Jsontron adds the Jsontron keywords to JsonSchema.Net. After one call to MetaSchemas.Register(), every schema that JsonSchema.Net builds understands rules, assert, report and let, and their failures come back in its ordinary evaluation results. jq is evaluated by JQLite, in-process and without native dependencies.

Requirements

  • .NET 10 or later.
  • JsonSchema.Net 9 and JQLite 1, which the package brings.
  • The package implements the Jsontron 0.2 specification.

Getting it

$ dotnet add package Jsontron

A first example

using System.Text.Json;
using Json.Schema;

// Once per process: adds the keywords to JsonSchema.Net's default dialect.
Jsontron.MetaSchemas.Register();

var schema = JsonSchema.FromText(
    """
    {
      "type": "object",
      "required": [ "orders", "policy" ],
      "rules": [
        {
          "context": ".orders[] | select(.total > 1000)",
          "asserts": [
            {
              "test": ".discount >= $root.policy.minDiscount",
              "message": "order \\(.id): discount \\(.discount) is below policy"
            }
          ]
        }
      ]
    }
    """);

using var orders = JsonDocument.Parse(
    """
    {
      "policy": { "minDiscount": 0.1 },
      "orders": [
        { "id": "A-1", "total": 2500, "discount": 0.05 },
        { "id": "A-2", "total": 80, "discount": 0 }
      ]
    }
    """);

var result = schema.Evaluate(orders.RootElement, new EvaluationOptions { OutputFormat = OutputFormat.List });

Console.WriteLine(result.IsValid); // False
foreach (var detail in result.Details ?? [])
    if (detail.Errors is { } errors)
        foreach (var (keyword, message) in errors)
            Console.WriteLine($"{detail.InstanceLocation} {keyword}: {message}");
False
 rules: order A-1: discount 0.05 is below policy

JsonSchema.Net has a MetaSchemas class of its own: with using Json.Schema;, name Jsontron's in full, as above, or with an alias such as using JsontronSchemas = Jsontron.MetaSchemas;.

The rule selects the orders over 1000, so only A-1 is tested. The failure is reported at the location of the instance the schema object applies to, here the root (the empty pointer), under the keyword rules: see Validation results.

In this section

Page What it covers
Building schemas MetaSchemas.Register, the Jsontron dialect and $schema, build errors
Validation results where failures and evaluation errors appear in EvaluationResults
API reference every public type of the package

The keywords themselves are described in the guide.