Expand description

Matching Rule definition expressions

Parser for parsing matching rule definitions into a value, matching rules and generator tuple.

The following are examples of matching rule definitions:

  • matching(type,'Name') - type matcher
  • matching(number,100) - number matcher
  • matching(datetime, 'yyyy-MM-dd','2000-01-01') - datetime matcher with format string

Primitive values

Primitive (or scalar) values can be strings (quoted with single quotes), numbers, booleans or null.

Expressions

The main types of expressions are one of the following:

matching(TYPE [, CONFIG], EXAMPLE) or matching($‘NAME’)

Expression that defines a matching rule. Each matching rule requires the type of matching rule, and can contain an optional configuration value. The final value is the example value to use.

Supported matching rules:

RuleDescriptionConfig ValueExample
equalToValue must be equal to the examplematching(equalTo, 'Example value')
typeValue must be the same type as the examplematching(type, 'Example value')
numberValue must be a numeric valuematching(number, 100.09)
integerValue must be an integer value (no decimals)matching(integer, 100)
decimalValue must be a decimal number (must have at least one significant figure after the decimal point)matching(decimnal, 100.01)
datetimeValue must match a date-time format stringFormat Stringmatching(datetime, 'yyyy-MM-dd HH:mm:ssZZZZZ', '2020-05-21 16:44:32+10:00')
dateValue must match a date format stringFormat Stringmatching(date, 'yyyy-MM-dd', '22:04')
timeValue must match a time format stringFormat Stringmatching(time, 'HH:mm', '22:04')
regexValue must match a regular expressionRegular expressionmatching(regex, '\\w{3}\\d+', 'abc123')
includeValue must include the example value as a substringmatching(include, 'testing')
booleanValue must be a booleanmatching(boolean, true)
serverValue must match the semver specificationmatching(semver, '1.0.0')
contentTypeValue must be of the provided content type. This will preform a magic test on the bytes of the value.Content typematching(contentType, 'application/xml', '<?xml?><test/>')

The final form is a reference to another key. This is used to setup type matching using an example value, and is normally used for collections. The name of the key must be a string value in single quotes.

For example, to configure a type matcher where each value in a list must match the definition of a person:

{
  "pact:match": "eachValue(matching($'person'))",
  "person": {
    "name": "Fred",
    "age": 100
  }
}

notEmpty(EXAMPLE)

Expression that defines the value the same type as the example, must be present and not empty. This is used to defined required fields.

Example: notEmpty('test')

eachKey(EXPRESSION)

Configures a matching rule to be applied to each key in a map.

For example: eachKey(matching(regex, '\$(\.\w+)+', '$.test.one'))

eachValue(EXPRESSION)

Configures a matching rule to be applied to each value in a map or list.

For example: eachValue(matching(type, 100))

Grammar

There is a grammar for the definitions in ANTLR4 format.

Structs

Reference to another attribute that defines the structure of the matching rule

Matching rule definition constructed from parsing a matching rule definition expression

Enums

Type to associate with an expression element

Functions

Determines if a sting starts with a valid matching rule definition. This is used in the case where a value can be a matching rule definition or a plain string value

Parse a matcher definition into a MatchingRuleDefinition containing the example value, matching rules and any generator. The following are examples of matching rule definitions: