prunePayload function

Object? prunePayload(
  1. Object? payload,
  2. String schemaJson
)

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:

  • properties recurse; additionalProperties as a schema recurses into the members it governs; additionalProperties: false drops the rest.
  • $ref to a JSON Pointer within the schema (#/$defs/X, #) is followed. Generated schemas have every cross-file $ref already inlined.
  • items recurses into each array element.
  • allOf branches are applied in turn.
  • oneOf / anyOf: the value is pruned against a branch only when exactly one branch is plausible for it — its type, const, enum and required members, and the const / enum of 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;
}