@@ -16,6 +16,7 @@ import au.com.dius.pact.core.model.generators.RandomStringGenerator
16
16
import au.com.dius.pact.core.model.generators.RegexGenerator
17
17
import au.com.dius.pact.core.model.generators.TimeGenerator
18
18
import au.com.dius.pact.core.model.generators.UuidGenerator
19
+ import au.com.dius.pact.core.model.matchingrules.EachKeyMatcher
19
20
import au.com.dius.pact.core.model.matchingrules.EqualsIgnoreOrderMatcher
20
21
import au.com.dius.pact.core.model.matchingrules.EqualsMatcher
21
22
import au.com.dius.pact.core.model.matchingrules.MatchingRule
@@ -28,6 +29,7 @@ import au.com.dius.pact.core.model.matchingrules.RegexMatcher
28
29
import au.com.dius.pact.core.model.matchingrules.RuleLogic
29
30
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
30
31
import au.com.dius.pact.core.model.matchingrules.ValuesMatcher
32
+ import au.com.dius.pact.core.model.matchingrules.expressions.MatchingRuleDefinition
31
33
import au.com.dius.pact.core.support.Json.toJson
32
34
import au.com.dius.pact.core.support.expressions.DataType.Companion.from
33
35
import au.com.dius.pact.core.support.json.JsonValue
@@ -2231,4 +2233,30 @@ open class PactDslJsonBody : DslPart {
2231
2233
else -> body
2232
2234
}
2233
2235
}
2236
+
2237
+ /* *
2238
+ * Applies a matching rule to each key in the object, ignoring the values.
2239
+ */
2240
+ fun eachKeyMatching (matcher : Matcher ): PactDslJsonBody {
2241
+ val path = if (rootPath.endsWith(" ." )) rootPath.substring(0 , rootPath.length - 1 ) else rootPath
2242
+ val value = matcher.value.toString()
2243
+ if (matcher.matcher != null ) {
2244
+ matchers.addRule(path, EachKeyMatcher (MatchingRuleDefinition (value, matcher.matcher!! , matcher.generator)))
2245
+ }
2246
+ if (! body.has(value)) {
2247
+ when (val body = body) {
2248
+ is JsonValue .Object -> body.add(value, JsonValue .Null )
2249
+ else -> {}
2250
+ }
2251
+ }
2252
+ return this
2253
+ }
2254
+
2255
+ /* *
2256
+ * Applies matching rules to each value in the object, ignoring the keys.
2257
+ */
2258
+ fun eachValueMatching (exampleKey : String ): PactDslJsonBody {
2259
+ val path = constructValidPath(" *" , rootPath)
2260
+ return PactDslJsonBody (" $path ." , exampleKey, this )
2261
+ }
2234
2262
}
0 commit comments