From cfc6d2d5b9ed9656165670e6ec262841aade3660 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 1 Apr 2026 15:46:23 +0200 Subject: jsonmatch: add Irrelevant --- jsonmatch/interface.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'jsonmatch/interface.go') diff --git a/jsonmatch/interface.go b/jsonmatch/interface.go index de6ef81..047a4b7 100644 --- a/jsonmatch/interface.go +++ b/jsonmatch/interface.go @@ -283,3 +283,22 @@ func (f capturedField) MarshalJSON() ([]byte, error) { func (f capturedField) UnmarshalJSON(buf []byte) error { return errors.New("cannot unmarshal into jsonmatch.CaptureField()") } + +type irrelevant struct{} + +// Irrelevant returns a slot that can be placed in a jsonmatch.Object or jsonmatch.Array instance +// to ignore the contents of certain fields or array elements during an assertion. +// +// Irrelevant() slots only work inside data structures that DiffAgainst() knows how to recurse into. +// Please refer to the documentation on type Diffable for details. +func Irrelevant() any { + return irrelevant{} +} + +// MarshalJSON implements the json.Marshaler interface. +// +// This implementation ensures that `irrelevant` renders in a readable way +// when a larger value containing it is serialized for a "type mismatch" or "value mismatch" error message. +func (irrelevant) MarshalJSON() ([]byte, error) { + return []byte(`""`), nil +} -- cgit v1.2.3