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

Match number type with Regex #1600

Open
Zabuzard opened this issue Aug 26, 2022 · 1 comment
Open

Match number type with Regex #1600

Zabuzard opened this issue Aug 26, 2022 · 1 comment

Comments

@Zabuzard
Copy link
Contributor

Zabuzard commented Aug 26, 2022

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:

{
  "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.

@rholshausen
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants