File size: 2,876 Bytes
bd461b8 7207ccc 3c69010 0fa1a4b a5b993f 57d2325 3a55404 ea8407d 00f2417 c1abe68 c50b0a8 1b32a5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | using System.Text.Json;
namespace SchemaPilot.Runtime;
/// <summary>
/// SchemaPilot runtime CLI. Mirrors a subset of the Python CLI so the same
/// contracts and payloads can be validated in a .NET service or edge worker.
///
/// Commands:
/// inspect <contract.json> print normalised contract
/// validate <contract.json> <payload.json> validate payload, exit 1 on failure
/// fingerprint <contract.json> print the stable contract digest
/// </summary>
public static class Program
{
public static int Main(string[] args)
{
if (args.Length == 0)
{
Console.Error.WriteLine("usage: SchemaPilot.Runtime <inspect|validate|fingerprint> <args...>");
return 64;
}
try
{
return args[0] switch
{
"inspect" => Inspect(args),
"validate" => Validate(args),
"fingerprint" => Fingerprint(args),
_ => Unknown(args[0]),
};
}
catch (FileNotFoundException ex)
{
Console.Error.WriteLine($"file not found: {ex.FileName}");
return 4;
}
catch (JsonException ex)
{
Console.Error.WriteLine($"invalid JSON: {ex.Message}");
return 5;
}
catch (FormatException ex)
{
Console.Error.WriteLine($"contract error: {ex.Message}");
return 3;
}
}
private static int Unknown(string cmd)
{
Console.Error.WriteLine($"unknown command: {cmd}");
return 64;
}
private static int Inspect(string[] args)
{
var contract = Contract.Parse(File.ReadAllText(args[1]));
var summary = new Dictionary<string, object?>
{
["name"] = contract.Name,
["version"] = contract.Version,
["fields"] = contract.Fields.Keys.ToList(),
["required"] = contract.RequiredFields.ToList(),
["fingerprint"] = Fingerprinter.Compute(contract),
};
Console.WriteLine(JsonSerializer.Serialize(summary, JsonOpts));
return 0;
}
private static int Validate(string[] args)
{
var contract = Contract.Parse(File.ReadAllText(args[1]));
using var doc = JsonDocument.Parse(File.ReadAllText(args[2]));
var result = new Validator(contract).Validate(doc.RootElement);
Console.WriteLine(JsonSerializer.Serialize(result.ToDict(), JsonOpts));
return result.Ok ? 0 : 1;
}
private static int Fingerprint(string[] args)
{
var contract = Contract.Parse(File.ReadAllText(args[1]));
Console.WriteLine(Fingerprinter.Compute(contract));
return 0;
}
private static readonly JsonSerializerOptions JsonOpts = new() { WriteIndented = true };
}
|