You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems to be impossible to declare a matcher for a request containing a number-type, additionally to applying some regex validation to it. For example, in order to test that a number is always positive:
{
"age": 18
}
Options
The only two options available right now seem to be:
numberType("age")
// or
stringMatcher("age", "\\d+")
but former will accept negative numbers and latter will demand a string type "age": "18" instead of only number types.
Proposal
Ideally we should be able to write something like
numberType("age", "\\d+")
// or
numberType("age").andMatches("\\d+")
a pattern like latter could then also be applied to other matchers, such as decimalType, uuid, dateTime, ...
Implementation-wise, we have two problems to solve:
Request matching
Requests have to be matched. This seems simpler, since pretty much any given request can just be converted into a string and then regex matching can be applied to the existing matchers. For example, for a number, we can just use Int#toStr and then we have a string to work with
Response matching
Responses are a bit more tricky, since the code needs to be able to generate example responses based on the matcher. We have existing generators for all those types, including a sophisticated generator for given regex patterns. These two worlds have to be combined, i.e. we need to generate for example numbers that also match a regex pattern.
If designed carefully, it might be applicable to generate a string that matches the regex and then attempt to parse it as the type, for example text.toInt().
However, this imposes on the user that their regex must be exhaustive and actually include everything that existing numberType matchers etc would do. For example the following would then cause trouble:
// match positive decimals
decimalType("weight").andMatches("[^-]+")
since the regex itself "[^-]+" only represents the extra condition (and positive), but does not fully saturate the conditions to generate decimals.
The text was updated successfully, but these errors were encountered:
I do like the form of decimalType("weight").andMatches("[^-]+"), but that requires the DSL to be stateful. The andMatches method would need to know what the previous matcher was.
Motivation
(originated from a discussion in Slack: https://pact-foundation.slack.com/archives/C9UN99H24/p1661162611482749)
It seems to be impossible to declare a matcher for a request containing a number-type, additionally to applying some regex validation to it. For example, in order to test that a number is always positive:
Options
The only two options available right now seem to be:
but former will accept negative numbers and latter will demand a string type
"age": "18"
instead of only number types.Proposal
Ideally we should be able to write something like
a pattern like latter could then also be applied to other matchers, such as
decimalType
,uuid
,dateTime
, ...Implementation-wise, we have two problems to solve:
Request matching
Requests have to be matched. This seems simpler, since pretty much any given request can just be converted into a string and then regex matching can be applied to the existing matchers. For example, for a number, we can just use
Int#toStr
and then we have a string to work withResponse matching
Responses are a bit more tricky, since the code needs to be able to generate example responses based on the matcher. We have existing generators for all those types, including a sophisticated generator for given regex patterns. These two worlds have to be combined, i.e. we need to generate for example numbers that also match a regex pattern.
If designed carefully, it might be applicable to generate a string that matches the regex and then attempt to parse it as the type, for example
text.toInt()
.However, this imposes on the user that their regex must be exhaustive and actually include everything that existing
numberType
matchers etc would do. For example the following would then cause trouble:since the regex itself
"[^-]+"
only represents the extra condition (and positive), but does not fully saturate the conditions to generate decimals.The text was updated successfully, but these errors were encountered: