prunePayload function
payload with every object member schemaJson does not know removed, where
the schema closes the object with additionalProperties: false.
Returns a new tree; payload is not modified. Members are only ever
removed, never added or rewritten, and only where the schema says the
object is closed — an open object keeps its unknown members, because the
implemented version accepts them anyway.
How the schema is followed:
propertiesrecurse;additionalPropertiesas a schema recurses into the members it governs;additionalProperties: falsedrops the rest.$refto a JSON Pointer within the schema (#/$defs/X,#) is followed. Generated schemas have every cross-file$refalready inlined.itemsrecurses into each array element.allOfbranches are applied in turn.oneOf/anyOf: the value is pruned against a branch only when exactly one branch is plausible for it — itstype,const,enumandrequiredmembers, and theconst/enumof the members it names, all fit. With more than one plausible branch, or none, the value is left untouched there: pruning against the wrong branch would remove members the right one needs, and an unpruned extra member is refused by item 2 validation rather than silently lost.
The walk takes at most 100,000 steps plus 64 per value in payload (a step
is a value visited against a subschema, or a branch test). A payload and
schema that need more are returned untouched; readForVersion refuses such
a document malformedRequest instead.
Not followed: patternProperties, prefixItems, unevaluatedProperties,
if/then/else, dependentSchemas and remote $refs — no registered
schema uses them. Members they would govern are left untouched.
Implementation
Object? prunePayload(Object? payload, String schemaJson) {
final pruner = _Pruner.forPayload(_decodeSchema(schemaJson), payload);
final out = pruner.prune(payload, pruner.root, 0);
return pruner.exhausted ? payload : out;
}