|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace BabelQueue.Schema; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Validates a message's <c>data</c> block against a per-URN JSON Schema (ADR-0024). A |
| 8 | +/// hand-rolled subset of Draft-07 (zero dependencies) whose verdicts match the Go, PHP, |
| 9 | +/// Python, Node and Java validators and babelqueue-registry's <c>compat</c> linter. Supported |
| 10 | +/// keywords: <c>type</c>, <c>required</c>, <c>properties</c>, <c>additionalProperties</c>, |
| 11 | +/// <c>items</c>, <c>enum</c>, <c>const</c>, <c>minLength</c>, <c>minimum</c>; unknown keywords |
| 12 | +/// are ignored. It works on the materialized <c>object?</c> tree the codec produces. |
| 13 | +/// </summary> |
| 14 | +public static class PayloadValidator |
| 15 | +{ |
| 16 | + private static readonly IReadOnlyDictionary<string, object?> EmptyProps = |
| 17 | + new Dictionary<string, object?>(); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// The first violation of <paramref name="value"/> against <paramref name="schema"/> as |
| 21 | + /// <c>"<json-pointer>: <reason>"</c>, or <c>null</c> when it conforms. |
| 22 | + /// </summary> |
| 23 | + public static string? Validate(IReadOnlyDictionary<string, object?> schema, object? value) => |
| 24 | + ValidateNode(schema, value, string.Empty); |
| 25 | + |
| 26 | + private static string? ValidateNode(IReadOnlyDictionary<string, object?> schema, object? value, string path) |
| 27 | + { |
| 28 | + if (schema.TryGetValue("const", out var constValue) && !Equal(value, constValue)) |
| 29 | + { |
| 30 | + return Violation(path, "wrong_const"); |
| 31 | + } |
| 32 | + |
| 33 | + if (schema.TryGetValue("enum", out var enumObj) |
| 34 | + && enumObj is IReadOnlyList<object?> enumValues |
| 35 | + && !Contains(enumValues, value)) |
| 36 | + { |
| 37 | + return Violation(path, "not_in_enum"); |
| 38 | + } |
| 39 | + |
| 40 | + var type = schema.TryGetValue("type", out var t) && t is string s ? s : string.Empty; |
| 41 | + return type switch |
| 42 | + { |
| 43 | + "object" => CheckObject(schema, value, path), |
| 44 | + "array" => CheckArray(schema, value, path), |
| 45 | + "string" => CheckString(schema, value, path), |
| 46 | + "integer" => IsInteger(value) ? CheckMinimum(schema, value, path) : Violation(path, "not_an_integer"), |
| 47 | + "number" => IsNumber(value) ? CheckMinimum(schema, value, path) : Violation(path, "not_a_number"), |
| 48 | + "boolean" => value is bool ? null : Violation(path, "not_a_boolean"), |
| 49 | + "null" => value is null ? null : Violation(path, "not_null"), |
| 50 | + _ => null, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + private static string? CheckObject(IReadOnlyDictionary<string, object?> schema, object? value, string path) |
| 55 | + { |
| 56 | + if (value is not IReadOnlyDictionary<string, object?> obj) |
| 57 | + { |
| 58 | + return Violation(path, "not_an_object"); |
| 59 | + } |
| 60 | + |
| 61 | + if (schema.TryGetValue("required", out var req) && req is IReadOnlyList<object?> required) |
| 62 | + { |
| 63 | + foreach (var key in required) |
| 64 | + { |
| 65 | + if (key is string name && !obj.ContainsKey(name)) |
| 66 | + { |
| 67 | + return Violation(Join(path, name), "missing_required"); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + var properties = schema.TryGetValue("properties", out var p) |
| 73 | + && p is IReadOnlyDictionary<string, object?> props |
| 74 | + ? props |
| 75 | + : EmptyProps; |
| 76 | + var additionalAllowed = !(schema.TryGetValue("additionalProperties", out var ap) && ap is false); |
| 77 | + |
| 78 | + foreach (var member in obj) |
| 79 | + { |
| 80 | + if (properties.TryGetValue(member.Key, out var ps) && ps is IReadOnlyDictionary<string, object?> propSchema) |
| 81 | + { |
| 82 | + var found = ValidateNode(propSchema, member.Value, Join(path, member.Key)); |
| 83 | + if (found is not null) |
| 84 | + { |
| 85 | + return found; |
| 86 | + } |
| 87 | + } |
| 88 | + else if (!additionalAllowed) |
| 89 | + { |
| 90 | + return Violation(Join(path, member.Key), "additional_not_allowed"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + private static string? CheckArray(IReadOnlyDictionary<string, object?> schema, object? value, string path) |
| 98 | + { |
| 99 | + if (value is not IReadOnlyList<object?> list) |
| 100 | + { |
| 101 | + return Violation(path, "not_an_array"); |
| 102 | + } |
| 103 | + |
| 104 | + if (!(schema.TryGetValue("items", out var it) && it is IReadOnlyDictionary<string, object?> items)) |
| 105 | + { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + for (var i = 0; i < list.Count; i++) |
| 110 | + { |
| 111 | + var found = ValidateNode(items, list[i], path + "[" + i + "]"); |
| 112 | + if (found is not null) |
| 113 | + { |
| 114 | + return found; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + private static string? CheckString(IReadOnlyDictionary<string, object?> schema, object? value, string path) |
| 122 | + { |
| 123 | + if (value is not string str) |
| 124 | + { |
| 125 | + return Violation(path, "not_a_string"); |
| 126 | + } |
| 127 | + |
| 128 | + if (schema.TryGetValue("minLength", out var ml) && ml is long min && str.Length < min) |
| 129 | + { |
| 130 | + return Violation(path, "below_min_length"); |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + private static string? CheckMinimum(IReadOnlyDictionary<string, object?> schema, object? value, string path) |
| 137 | + { |
| 138 | + if (schema.TryGetValue("minimum", out var m) |
| 139 | + && TryDouble(m, out var min) |
| 140 | + && TryDouble(value, out var actual) |
| 141 | + && actual < min) |
| 142 | + { |
| 143 | + return Violation(path, "below_minimum"); |
| 144 | + } |
| 145 | + |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + // JSON numbers materialize to long (integers) or double (fractions); a whole-valued double |
| 150 | + // still counts as an integer, matching the other SDKs. bool is not long/double. |
| 151 | + private static bool IsInteger(object? value) => value switch |
| 152 | + { |
| 153 | + long or int => true, |
| 154 | + double d => !double.IsInfinity(d) && d == Math.Floor(d), |
| 155 | + _ => false, |
| 156 | + }; |
| 157 | + |
| 158 | + private static bool IsNumber(object? value) => value is long or int or double; |
| 159 | + |
| 160 | + private static bool TryDouble(object? value, out double result) |
| 161 | + { |
| 162 | + switch (value) |
| 163 | + { |
| 164 | + case long l: |
| 165 | + result = l; |
| 166 | + return true; |
| 167 | + case int i: |
| 168 | + result = i; |
| 169 | + return true; |
| 170 | + case double d: |
| 171 | + result = d; |
| 172 | + return true; |
| 173 | + default: |
| 174 | + result = 0; |
| 175 | + return false; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Type-aware equality: a long never equals a double, true never equals 1 — matching the |
| 180 | + // strict comparisons in the other SDK validators. |
| 181 | + private static bool Equal(object? a, object? b) |
| 182 | + { |
| 183 | + if (a is null || b is null) |
| 184 | + { |
| 185 | + return a is null && b is null; |
| 186 | + } |
| 187 | + |
| 188 | + return a.GetType() == b.GetType() && a.Equals(b); |
| 189 | + } |
| 190 | + |
| 191 | + private static bool Contains(IReadOnlyList<object?> values, object? value) |
| 192 | + { |
| 193 | + foreach (var item in values) |
| 194 | + { |
| 195 | + if (Equal(value, item)) |
| 196 | + { |
| 197 | + return true; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + private static string Violation(string path, string reason) => |
| 205 | + (path.Length == 0 ? "<root>" : path) + ": " + reason; |
| 206 | + |
| 207 | + private static string Join(string path, string key) => |
| 208 | + path.Length == 0 ? key : path + "." + key; |
| 209 | +} |
0 commit comments