Skip to content

Matcher

Matching functionality for Pact.

Matchers are used in Pact to allow for more flexible matching of data. While the consumer defines the expected request and response, there are circumstances where the provider may return dynamically generated data. In these cases, the consumer should use a matcher to define the expected data.

Attributes

Classes

ArrayContainsMatcher(variants: Sequence[_T | Matcher[_T]])

Bases: Matcher[Sequence[_T]]

Array contains matcher.

A matcher that checks if an array contains a value.

PARAMETER DESCRIPTION
variants

List of possible values to match against.

TYPE: Sequence[_T | Matcher[_T]]

Source code in src/pact/v3/match/matcher.py
def __init__(self, variants: Sequence[_T | Matcher[_T]]) -> None:
    """
    Initialize the matcher.

    Args:
        variants:
            List of possible values to match against.
    """
    self._matcher: Matcher[Sequence[_T]] = GenericMatcher(
        "arrayContains",
        extra_fields={"variants": variants},
    )

Functions

to_integration_json() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_integration_json(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_integration_json()
to_matching_rule() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_matching_rule(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_matching_rule()

EachKeyMatcher(value: Mapping[_T, Matchable], rules: list[Matcher[_T]] | None = None)

Bases: Matcher[Mapping[_T, Matchable]]

Each key matcher.

A matcher that applies a matcher to each key in a mapping.

PARAMETER DESCRIPTION
value

Example value to match against.

TYPE: Mapping[_T, Matchable]

rules

List of matchers to apply to each key in the mapping.

TYPE: list[Matcher[_T]] | None DEFAULT: None

Source code in src/pact/v3/match/matcher.py
def __init__(
    self,
    value: Mapping[_T, Matchable],
    rules: list[Matcher[_T]] | None = None,
) -> None:
    """
    Initialize the matcher.

    Args:
        value:
            Example value to match against.

        rules:
            List of matchers to apply to each key in the mapping.
    """
    self._matcher: Matcher[Mapping[_T, Matchable]] = GenericMatcher(
        "eachKey",
        value=value,
        extra_fields={"rules": rules},
    )

Functions

to_integration_json() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_integration_json(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_integration_json()
to_matching_rule() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_matching_rule(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_matching_rule()

EachValueMatcher(value: Mapping[Matchable, _T], rules: list[Matcher[_T]] | None = None)

Bases: Matcher[Mapping[Matchable, _T]]

Each value matcher.

A matcher that applies a matcher to each value in a mapping.

PARAMETER DESCRIPTION
value

Example value to match against.

TYPE: Mapping[Matchable, _T]

rules

List of matchers to apply to each value in the mapping.

TYPE: list[Matcher[_T]] | None DEFAULT: None

Source code in src/pact/v3/match/matcher.py
def __init__(
    self,
    value: Mapping[Matchable, _T],
    rules: list[Matcher[_T]] | None = None,
) -> None:
    """
    Initialize the matcher.

    Args:
        value:
            Example value to match against.

        rules:
            List of matchers to apply to each value in the mapping.
    """
    self._matcher: Matcher[Mapping[Matchable, _T]] = GenericMatcher(
        "eachValue",
        value=value,
        extra_fields={"rules": rules},
    )

Functions

to_integration_json() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_integration_json(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_integration_json()
to_matching_rule() -> dict[str, Any]
Source code in src/pact/v3/match/matcher.py
def to_matching_rule(self) -> dict[str, Any]:  # noqa: D102
    return self._matcher.to_matching_rule()

GenericMatcher(type: MatcherType, /, value: _T | Unset = UNSET, generator: Generator | None = None, extra_fields: Mapping[str, Any] | None = None, **kwargs: Matchable)

Bases: Matcher[_T]

Generic matcher.

A generic matcher, with the ability to define arbitrary additional fields for inclusion in the integration JSON object and matching rule.

PARAMETER DESCRIPTION
type

The type of the matcher.

TYPE: MatcherType

value

The value to match. If not provided, the Pact library will generate a value based on the matcher type (or use the generator if provided). To ensure reproducibility, it is highly recommended to provide a value when creating a matcher.

TYPE: _T | Unset DEFAULT: UNSET

generator

The generator to use when generating the value. The generator will generally only be used if value is not provided.

TYPE: Generator | None DEFAULT: None

extra_fields

Additional configuration elements to pass to the matcher. These fields will be used when converting the matcher to both an integration JSON object and a matching rule.

TYPE: Mapping[str, Any] | None DEFAULT: None

**kwargs

Alternative way to define extra fields. See the extra_fields argument for more information.

TYPE: Matchable DEFAULT: {}

Source code in src/pact/v3/match/matcher.py
def __init__(
    self,
    type: MatcherType,  # noqa: A002
    /,
    value: _T | Unset = UNSET,
    generator: Generator | None = None,
    extra_fields: Mapping[str, Any] | None = None,
    **kwargs: Matchable,
) -> None:
    """
    Initialize the matcher.

    Args:
        type:
            The type of the matcher.

        value:
            The value to match. If not provided, the Pact library will
            generate a value based on the matcher type (or use the generator
            if provided). To ensure reproducibility, it is _highly_
            recommended to provide a value when creating a matcher.

        generator:
            The generator to use when generating the value. The generator
            will generally only be used if value is not provided.

        extra_fields:
            Additional configuration elements to pass to the matcher. These
            fields will be used when converting the matcher to both an
            integration JSON object and a matching rule.

        **kwargs:
            Alternative way to define extra fields. See the `extra_fields`
            argument for more information.
    """
    self.type = type
    """
    The type of the matcher.
    """

    self.value: _T | Unset = value
    """
    Default value used by Pact when executing tests.
    """

    self.generator = generator
    """
    Generator used to generate a value when the value is not provided.
    """

    self._extra_fields: Mapping[str, Any] = dict(
        chain((extra_fields or {}).items(), kwargs.items())
    )

Attributes

generator = generator instance-attribute

Generator used to generate a value when the value is not provided.

type = type instance-attribute

The type of the matcher.

value: _T | Unset = value instance-attribute

Default value used by Pact when executing tests.

Functions

has_value() -> bool

Check if the matcher has a value.

Source code in src/pact/v3/match/matcher.py
def has_value(self) -> bool:
    """
    Check if the matcher has a value.
    """
    return not isinstance(self.value, Unset)
to_integration_json() -> dict[str, Any]

Convert the matcher to an integration JSON object.

This method is used internally to convert the matcher to a JSON object which can be embedded directly in a number of places in the Pact FFI.

For more information about this format, see the docs:

https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson

RETURNS DESCRIPTION
dict[str, Any]

dict[str, Any]: The matcher as an integration JSON object.

Source code in src/pact/v3/match/matcher.py
def to_integration_json(self) -> dict[str, Any]:
    """
    Convert the matcher to an integration JSON object.

    This method is used internally to convert the matcher to a JSON object
    which can be embedded directly in a number of places in the Pact FFI.

    For more information about this format, see the docs:

    > https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson

    Returns:
        dict[str, Any]:
            The matcher as an integration JSON object.
    """
    return {
        "pact:matcher:type": self.type,
        **({"value": self.value} if not isinstance(self.value, Unset) else {}),
        **(
            self.generator.to_integration_json()
            if self.generator is not None
            else {}
        ),
        **self._extra_fields,
    }
to_matching_rule() -> dict[str, Any]

Convert the matcher to a matching rule.

This method is used internally to convert the matcher to a matching rule which can be embedded directly in a Pact file.

For more information about this format, see the docs:

https://github.com/pact-foundation/pact-specification/tree/version-4

and

https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers

RETURNS DESCRIPTION
dict[str, Any]

dict[str, Any]: The matcher as a matching rule.

Source code in src/pact/v3/match/matcher.py
def to_matching_rule(self) -> dict[str, Any]:
    """
    Convert the matcher to a matching rule.

    This method is used internally to convert the matcher to a matching rule
    which can be embedded directly in a Pact file.

    For more information about this format, see the docs:

    > https://github.com/pact-foundation/pact-specification/tree/version-4

    and

    > https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers

    Returns:
        dict[str, Any]:
            The matcher as a matching rule.
    """
    return {
        "match": self.type,
        **({"value": self.value} if not isinstance(self.value, Unset) else {}),
        **self._extra_fields,
    }

IntegrationJSONEncoder

Bases: JSONEncoder

JSON encoder class for integration JSON objects.

This class is used to encode integration JSON objects to JSON.

Functions

default(o: Any) -> Any

Encode the object to JSON.

Source code in src/pact/v3/match/matcher.py
def default(self, o: Any) -> Any:  # noqa: ANN401
    """
    Encode the object to JSON.
    """
    if isinstance(o, Matcher):
        return o.to_integration_json()
    if isinstance(o, Generator):
        return o.to_integration_json()
    return super().default(o)

Matcher

Bases: ABC, Generic[_T]

Abstract matcher.

In Pact, a matcher is used to define how a value should be compared. This allows for more flexible matching of data, especially when the provider returns dynamically generated data.

This class is abstract and should not be used directly. Instead, use one of the concrete matcher classes. Alternatively, you can create your own matcher by subclassing this class.

The matcher provides methods to convert into an integration JSON object and a matching rule. These methods are used internally by the Pact library when communicating with the FFI and generating the Pact file.

Functions

to_integration_json() -> dict[str, Any] abstractmethod

Convert the matcher to an integration JSON object.

This method is used internally to convert the matcher to a JSON object which can be embedded directly in a number of places in the Pact FFI.

For more information about this format, see the docs:

https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson

RETURNS DESCRIPTION
dict[str, Any]

The matcher as an integration JSON object.

Source code in src/pact/v3/match/matcher.py
@abstractmethod
def to_integration_json(self) -> dict[str, Any]:
    """
    Convert the matcher to an integration JSON object.

    This method is used internally to convert the matcher to a JSON object
    which can be embedded directly in a number of places in the Pact FFI.

    For more information about this format, see the docs:

    > https://docs.pact.io/implementation_guides/rust/pact_ffi/integrationjson

    Returns:
        The matcher as an integration JSON object.
    """
to_matching_rule() -> dict[str, Any] abstractmethod

Convert the matcher to a matching rule.

This method is used internally to convert the matcher to a matching rule which can be embedded directly in a Pact file.

For more information about this format, see the docs:

https://github.com/pact-foundation/pact-specification/tree/version-4

and

https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers

RETURNS DESCRIPTION
dict[str, Any]

The matcher as a matching rule.

Source code in src/pact/v3/match/matcher.py
@abstractmethod
def to_matching_rule(self) -> dict[str, Any]:
    """
    Convert the matcher to a matching rule.

    This method is used internally to convert the matcher to a matching rule
    which can be embedded directly in a Pact file.

    For more information about this format, see the docs:

    > https://github.com/pact-foundation/pact-specification/tree/version-4

    and

    > https://github.com/pact-foundation/pact-specification/tree/version-2?tab=readme-ov-file#matchers

    Returns:
        The matcher as a matching rule.
    """

MatchingRuleJSONEncoder

Bases: JSONEncoder

JSON encoder class for matching rules.

This class is used to encode matching rules to JSON.

Functions

default(o: Any) -> Any

Encode the object to JSON.

Source code in src/pact/v3/match/matcher.py
def default(self, o: Any) -> Any:  # noqa: ANN401
    """
    Encode the object to JSON.
    """
    if isinstance(o, Matcher):
        return o.to_matching_rule()
    return super().default(o)