MCPcopy
hub / github.com/keploy/keploy / JSONDiffWithNoiseControl

Function JSONDiffWithNoiseControl

pkg/matcher/utils.go:111–128  ·  view source on GitHub ↗

JSONDiffWithNoiseControl compares JSON with support for both Path-based noise (e.g. "body.user.id") and Global noise (e.g. "timestamp") to be ignored everywhere.

(validatedJSON ValidatedJSON, noise map[string][]string, ignoreOrdering bool)

Source from the content-addressed store, hash-verified

109// JSONDiffWithNoiseControl compares JSON with support for both Path-based noise (e.g. "body.user.id")
110// and Global noise (e.g. "timestamp") to be ignored everywhere.
111func JSONDiffWithNoiseControl(validatedJSON ValidatedJSON, noise map[string][]string, ignoreOrdering bool) (JSONComparisonResult, error) {
112 // Split noise into Path-based (contains dots) and Global (no dots)
113 pathNoise := make(map[string][]string)
114 globalKeys := make(map[string]bool)
115
116 for k, v := range noise {
117 // If a key has no dots, treat it as a Global Key to be ignored everywhere.
118 if !strings.Contains(k, ".") {
119 globalKeys[strings.ToLower(k)] = true
120 } else {
121 // Otherwise, it's a path-specific noise (e.g. "body.data.timestamp")
122 pathNoise[k] = v
123 }
124 }
125
126 idx := buildNoiseIndex(pathNoise)
127 return matchJSONWithNoiseHandlingIndexed("", validatedJSON.expected, validatedJSON.actual, idx, globalKeys, ignoreOrdering)
128}
129
130// matchJSONWithNoiseHandlingIndexed now accepts globalKeys to skip specific keys at any depth.
131func matchJSONWithNoiseHandlingIndexed(key string, expected, actual interface{}, ni noiseIndex, globalKeys map[string]bool, ignoreOrdering bool) (JSONComparisonResult, error) {

Callers 4

CompareHTTPReqFunction · 0.92
CompareHTTPRespFunction · 0.92
MatchFunction · 0.92
handleJSONDiffFunction · 0.92

Calls 2

buildNoiseIndexFunction · 0.85

Tested by

no test coverage detected