Land fix batch 3's R-S: the schemas describe the bytes C# writes
New contracts/cultnet/cultnet.raw-document-header.schema.json for
CultNetRawDocumentHeader - it has no payload/payloadEncoding members at all,
so headers must not $ref the record schema (which required both). The
response page's own schema now lists every property CultNetSnapshotResponseRawV1Message
declares as `required` (present on the wire, nil included) rather than only
the four that happened to be non-nullable - MessagePack-CSharp's map-mode
formatter writes every declared key regardless of null, so `headers`/`documents`
are always both present, one an array and the other an explicit null by
projection. The old `oneOf: [{required:[headers]},{required:[documents]}]`
matched both branches on every real page (both keys present) and so never
validated anything; it is now `oneOf` over each key's type (array vs null),
which is mutually exclusive. cultnet.raw-document-record.schema.json gained
schemaName/schemaVersion/schemaContentHash (present on every real record, not
previously in the schema at all - any real page would have failed
additionalProperties:false on them). fieldPredicate's two oneOf branches each
gained `not: {required: [the other]}`, so `values` and `number` cannot both
be present (previously only `required` was checked, which does not exclude
the sibling key).
Added MiniJsonSchemaValidator, a deliberately small draft-2020-12 subset
validator (type/required/additionalProperties/properties/items/const/enum/
pattern/minLength/minItems/oneOf/$ref, same-document and cross-document) -
no schema-validation dependency existed in this repo, and this only needs to
cover what contracts/cultnet/*.schema.json actually use. CultNetSchemaContractTests
decodes real wire bytes from a live CultNetDatabaseServer (header projection,
document projection, and a hop with edges) through MessagePackSerializer.ConvertToJson
and checks them against the committed schema files, plus three
fieldPredicate-shape tests pinning the values/number exclusivity.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 20:12:58 +02:00
#nullable enable
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text.Json ;
using System.Text.RegularExpressions ;
namespace GameCult.Networking.Tests
{
/// <summary>
/// A deliberately small JSON Schema (draft 2020-12) validator covering only the constructs
/// contracts/cultnet/*.schema.json actually use: object/array/string/integer/boolean/null types
/// (plain or as a ["type","null"] union), required, additionalProperties: false, properties,
2026-09-22 20:13:53 +02:00
/// items, const, enum, pattern, minLength, minItems, oneOf, not, and $ref - same-document
Land fix batch 3's R-S: the schemas describe the bytes C# writes
New contracts/cultnet/cultnet.raw-document-header.schema.json for
CultNetRawDocumentHeader - it has no payload/payloadEncoding members at all,
so headers must not $ref the record schema (which required both). The
response page's own schema now lists every property CultNetSnapshotResponseRawV1Message
declares as `required` (present on the wire, nil included) rather than only
the four that happened to be non-nullable - MessagePack-CSharp's map-mode
formatter writes every declared key regardless of null, so `headers`/`documents`
are always both present, one an array and the other an explicit null by
projection. The old `oneOf: [{required:[headers]},{required:[documents]}]`
matched both branches on every real page (both keys present) and so never
validated anything; it is now `oneOf` over each key's type (array vs null),
which is mutually exclusive. cultnet.raw-document-record.schema.json gained
schemaName/schemaVersion/schemaContentHash (present on every real record, not
previously in the schema at all - any real page would have failed
additionalProperties:false on them). fieldPredicate's two oneOf branches each
gained `not: {required: [the other]}`, so `values` and `number` cannot both
be present (previously only `required` was checked, which does not exclude
the sibling key).
Added MiniJsonSchemaValidator, a deliberately small draft-2020-12 subset
validator (type/required/additionalProperties/properties/items/const/enum/
pattern/minLength/minItems/oneOf/$ref, same-document and cross-document) -
no schema-validation dependency existed in this repo, and this only needs to
cover what contracts/cultnet/*.schema.json actually use. CultNetSchemaContractTests
decodes real wire bytes from a live CultNetDatabaseServer (header projection,
document projection, and a hop with edges) through MessagePackSerializer.ConvertToJson
and checks them against the committed schema files, plus three
fieldPredicate-shape tests pinning the values/number exclusivity.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 20:12:58 +02:00
/// ("#/$defs/name"), cross-document ("other.schema.json"), and both ("other.schema.json#/$defs/name").
/// Not a general validator: it exists for R-S (docs/cultnet-selection-cut.md, fix batch 3) to
/// decode real C# wire bytes and check them against the committed schema files, which no
/// dependency in this repo already does - see CultNetSchemaContractTests.
/// </summary>
internal sealed class MiniJsonSchemaValidator
{
private readonly string _schemaDir ;
private readonly Dictionary < string , JsonElement > _docCache = new ( StringComparer . Ordinal ) ;
public MiniJsonSchemaValidator ( string schemaDir )
{
_schemaDir = schemaDir ;
}
/// <summary>Validates <paramref name="instance"/> against the named schema file's root schema. Throws with every collected error when invalid.</summary>
public void AssertValid ( JsonElement instance , string schemaFileName )
{
var errors = Validate ( instance , schemaFileName ) ;
if ( errors . Count > 0 )
{
throw new InvalidOperationException (
$"{instance} does not satisfy {schemaFileName}:{Environment.NewLine}{string.Join(Environment.NewLine, errors)}" ) ;
}
}
/// <summary>Validates against a $defs entry of the named schema file, e.g. "cultnet.selection.schema.json#/$defs/fieldPredicate".</summary>
public List < string > Validate ( JsonElement instance , string refText )
{
var errors = new List < string > ( ) ;
var ( schema , file ) = Resolve ( refText , currentFile : "" ) ;
ValidateAgainst ( instance , schema , file , "$" , errors ) ;
return errors ;
}
private JsonElement LoadDoc ( string fileName )
{
if ( _docCache . TryGetValue ( fileName , out var cached ) ) return cached ;
var path = Path . Combine ( _schemaDir , fileName ) ;
var doc = JsonDocument . Parse ( File . ReadAllText ( path ) ) . RootElement ;
_docCache [ fileName ] = doc ;
return doc ;
}
private ( JsonElement Schema , string File ) Resolve ( string refText , string currentFile )
{
var hash = refText . IndexOf ( '#' ) ;
string file ;
string pointer ;
if ( hash < 0 )
{
file = refText ;
pointer = "" ;
}
else
{
var filePart = refText [ . . hash ] ;
file = filePart . Length = = 0 ? currentFile : filePart ;
pointer = refText [ ( hash + 1 ) . . ] ;
}
var doc = LoadDoc ( file ) ;
if ( string . IsNullOrEmpty ( pointer ) | | pointer = = "/" ) return ( doc , file ) ;
var current = doc ;
foreach ( var segment in pointer . TrimStart ( '/' ) . Split ( '/' ) )
{
var key = segment . Replace ( "~1" , "/" ) . Replace ( "~0" , "~" ) ;
current = current . GetProperty ( key ) ;
}
return ( current , file ) ;
}
private void ValidateAgainst ( JsonElement instance , JsonElement schema , string file , string path , List < string > errors )
{
if ( schema . TryGetProperty ( "$ref" , out var refProp ) )
{
var ( resolved , resolvedFile ) = Resolve ( refProp . GetString ( ) ! , file ) ;
ValidateAgainst ( instance , resolved , resolvedFile , path , errors ) ;
return ;
}
if ( schema . TryGetProperty ( "oneOf" , out var oneOf ) )
{
var matches = 0 ;
var branchErrors = new List < string > ( ) ;
foreach ( var branch in oneOf . EnumerateArray ( ) )
{
var local = new List < string > ( ) ;
ValidateAgainst ( instance , branch , file , path , local ) ;
if ( local . Count = = 0 ) matches + + ;
else branchErrors . AddRange ( local ) ;
}
if ( matches ! = 1 )
{
errors . Add ( $"{path}: oneOf matched {matches} branches (want exactly 1); branch errors: {string.Join(" | ", branchErrors)}" ) ;
}
}
2026-09-22 20:13:53 +02:00
if ( schema . TryGetProperty ( "not" , out var not ) )
{
var local = new List < string > ( ) ;
ValidateAgainst ( instance , not , file , path , local ) ;
if ( local . Count = = 0 )
errors . Add ( $"{path}: matched the \" not \ " subschema, which it must not" ) ;
}
Land fix batch 3's R-S: the schemas describe the bytes C# writes
New contracts/cultnet/cultnet.raw-document-header.schema.json for
CultNetRawDocumentHeader - it has no payload/payloadEncoding members at all,
so headers must not $ref the record schema (which required both). The
response page's own schema now lists every property CultNetSnapshotResponseRawV1Message
declares as `required` (present on the wire, nil included) rather than only
the four that happened to be non-nullable - MessagePack-CSharp's map-mode
formatter writes every declared key regardless of null, so `headers`/`documents`
are always both present, one an array and the other an explicit null by
projection. The old `oneOf: [{required:[headers]},{required:[documents]}]`
matched both branches on every real page (both keys present) and so never
validated anything; it is now `oneOf` over each key's type (array vs null),
which is mutually exclusive. cultnet.raw-document-record.schema.json gained
schemaName/schemaVersion/schemaContentHash (present on every real record, not
previously in the schema at all - any real page would have failed
additionalProperties:false on them). fieldPredicate's two oneOf branches each
gained `not: {required: [the other]}`, so `values` and `number` cannot both
be present (previously only `required` was checked, which does not exclude
the sibling key).
Added MiniJsonSchemaValidator, a deliberately small draft-2020-12 subset
validator (type/required/additionalProperties/properties/items/const/enum/
pattern/minLength/minItems/oneOf/$ref, same-document and cross-document) -
no schema-validation dependency existed in this repo, and this only needs to
cover what contracts/cultnet/*.schema.json actually use. CultNetSchemaContractTests
decodes real wire bytes from a live CultNetDatabaseServer (header projection,
document projection, and a hop with edges) through MessagePackSerializer.ConvertToJson
and checks them against the committed schema files, plus three
fieldPredicate-shape tests pinning the values/number exclusivity.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 20:12:58 +02:00
if ( schema . TryGetProperty ( "const" , out var constEl ) )
{
if ( ! JsonElementDeepEquals ( instance , constEl ) )
errors . Add ( $"{path}: expected const {constEl}, got {instance}" ) ;
}
if ( schema . TryGetProperty ( "enum" , out var enumEl ) )
{
if ( ! enumEl . EnumerateArray ( ) . Any ( candidate = > JsonElementDeepEquals ( instance , candidate ) ) )
errors . Add ( $"{path}: value {instance} is not one of the declared enum values" ) ;
}
if ( schema . TryGetProperty ( "type" , out var typeEl ) )
{
var allowed = typeEl . ValueKind = = JsonValueKind . Array
? typeEl . EnumerateArray ( ) . Select ( t = > t . GetString ( ) ! ) . ToArray ( )
: new [ ] { typeEl . GetString ( ) ! } ;
if ( ! allowed . Any ( t = > MatchesType ( instance , t ) ) )
errors . Add ( $"{path}: kind {instance.ValueKind} does not satisfy type [{string.Join(" , ", allowed)}]" ) ;
}
// Null short-circuits the value-shaped checks below (a nilled key still satisfies a
// ["...","null"] type, but has no length/properties/items of its own to check).
if ( instance . ValueKind = = JsonValueKind . Null )
{
return ;
}
if ( instance . ValueKind = = JsonValueKind . String )
{
var value = instance . GetString ( ) ! ;
if ( schema . TryGetProperty ( "minLength" , out var minLength ) & & value . Length < minLength . GetInt32 ( ) )
errors . Add ( $"{path}: string shorter than minLength {minLength.GetInt32()}" ) ;
if ( schema . TryGetProperty ( "pattern" , out var pattern ) & & ! Regex . IsMatch ( value , pattern . GetString ( ) ! ) )
errors . Add ( $"{path}: \" { value } \ " does not match pattern {pattern.GetString()}" ) ;
}
if ( instance . ValueKind = = JsonValueKind . Array )
{
var items = instance . EnumerateArray ( ) . ToArray ( ) ;
if ( schema . TryGetProperty ( "minItems" , out var minItems ) & & items . Length < minItems . GetInt32 ( ) )
errors . Add ( $"{path}: array shorter than minItems {minItems.GetInt32()}" ) ;
if ( schema . TryGetProperty ( "items" , out var itemSchema ) )
{
for ( var i = 0 ; i < items . Length ; i + + )
ValidateAgainst ( items [ i ] , itemSchema , file , $"{path}[{i}]" , errors ) ;
}
}
if ( instance . ValueKind = = JsonValueKind . Object )
{
var propertyNames = instance . EnumerateObject ( ) . Select ( p = > p . Name ) . ToHashSet ( StringComparer . Ordinal ) ;
if ( schema . TryGetProperty ( "required" , out var required ) )
{
foreach ( var name in required . EnumerateArray ( ) . Select ( r = > r . GetString ( ) ! ) )
{
if ( ! propertyNames . Contains ( name ) )
errors . Add ( $"{path}: missing required property \" { name } \ "" ) ;
}
}
var declared = schema . TryGetProperty ( "properties" , out var properties )
? properties . EnumerateObject ( ) . Select ( p = > p . Name ) . ToHashSet ( StringComparer . Ordinal )
: new HashSet < string > ( StringComparer . Ordinal ) ;
if ( schema . TryGetProperty ( "additionalProperties" , out var additional ) & &
additional . ValueKind = = JsonValueKind . False )
{
foreach ( var name in propertyNames )
{
if ( ! declared . Contains ( name ) )
errors . Add ( $"{path}: property \" { name } \ " is not declared and additionalProperties is false" ) ;
}
}
if ( properties . ValueKind = = JsonValueKind . Object )
{
foreach ( var propertySchema in properties . EnumerateObject ( ) )
{
if ( instance . TryGetProperty ( propertySchema . Name , out var propertyValue ) )
ValidateAgainst ( propertyValue , propertySchema . Value , file , $"{path}.{propertySchema.Name}" , errors ) ;
}
}
}
}
private static bool MatchesType ( JsonElement instance , string type ) = > type switch
{
"object" = > instance . ValueKind = = JsonValueKind . Object ,
"array" = > instance . ValueKind = = JsonValueKind . Array ,
"string" = > instance . ValueKind = = JsonValueKind . String ,
"boolean" = > instance . ValueKind is JsonValueKind . True or JsonValueKind . False ,
"null" = > instance . ValueKind = = JsonValueKind . Null ,
"integer" = > instance . ValueKind = = JsonValueKind . Number & & instance . TryGetInt64 ( out _ ) ,
"number" = > instance . ValueKind = = JsonValueKind . Number ,
_ = > throw new NotSupportedException ( $"Unsupported schema type \" { type } \ "." )
} ;
private static bool JsonElementDeepEquals ( JsonElement a , JsonElement b )
{
if ( a . ValueKind ! = b . ValueKind ) return false ;
return a . ValueKind switch
{
JsonValueKind . String = > a . GetString ( ) = = b . GetString ( ) ,
JsonValueKind . Number = > a . GetRawText ( ) = = b . GetRawText ( ) ,
JsonValueKind . True or JsonValueKind . False or JsonValueKind . Null = > true ,
_ = > a . GetRawText ( ) = = b . GetRawText ( )
} ;
}
}
}