Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/composing matching rules #405

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions rust/pact_ffi/IntegrationJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,86 @@ Here the `interests` attribute would be expanded to
}
}
```

## Supporting multiple matching rules

Matching rules can be combined. These rules will be evaluated with an AND (i.e. all the rules must match successfully
for the result to be successful). The main reason to do this is to combine the `EachKey` and `EachValue` matching rules
on a map structure, but other rules make sense to combine (like the `include` matcher).

To provide multiple matchers, you need to provide an array format.

For example, assume you have an API that returns results for a document store where the documents are keyed based on some index:
```json
{
"results": {
"AUK-155332": {
"title": "...",
"description": "...",
"link": "http://....",
"relatesTo": ["BAF-88654"]
}
}
}
```

Here you may want to provide a matching rule for the keys that they conform to the `AAA-NNNNNNN...` format, as well
as a type matcher for the values.

So the resulting intermediate JSON would be something like:
```json
{
"results": {
"pact:matcher:type": [
{
"pact:matcher:type": "each-key",
"value": "AUK-155332",
"rules": [
{
"pact:matcher:type": "regex",
"regex": "\\w{3}-\\d+"
}
]
}, {
"pact:matcher:type": "each-value",
"rules": [
{
"pact:matcher:type": "type"
}
]
}
],
"AUK-155332": {
"title": "...",
"description": "...",
"link": "http://....",
"relatesTo": ["BAF-88654"]
}
}
}
```

## Supporting matching rule definitions

You can use the [matching rule definition expressions](https://docs.rs/pact_models/latest/pact_models/matchingrules/expressions/index.html)
in the `pact:matcher:type` field.

For example, with the previous document result JSON, you could then use the following for the `relatesTo` field:

```json
{
"relatesTo": {
"pact:matcher:type": "eachValue(matching(regex, '\\w{3}-\\d+', 'BAF-88654'))"
}
}
```

You can then also combine matchers:

```json
{
"relatesTo": {
"pact:matcher:type": "atLeast(1), atMost(10), eachValue(matching(regex, '\\w{3}-\\d+', 'BAF-88654'))"
}
}
```
Loading
Loading