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

Matching rule for non-empty map or a field which is not null #727

Open
QMG-kazala opened this issue Jul 9, 2018 · 6 comments
Open

Matching rule for non-empty map or a field which is not null #727

QMG-kazala opened this issue Jul 9, 2018 · 6 comments

Comments

@QMG-kazala
Copy link

QMG-kazala commented Jul 9, 2018

My problem is I have a field which is a list (an array) of maps. Each map can have elements of different types (strings or sub-maps), eg.

"validatedAnswers": [
    {
      "type": "typeA",
      "answers": {
        "favourite_colour": "Blue",
        "correspondence_address": {
            "line_1": "Main St",
            "postcode": "1A 2BC",
            "town": "London"
        }
      }
    },
    {
      "type": "typeB",
      "answers": {
        "first_name": "Firstname",
        "last_name": "Lastname",
      }
    }
  ]

and we're only interested in some of those answers.

What we really need is this: pact-foundation/pact-specification#38, but it's planned for v.4. In the meantime we're trying a different approach. What I'm attempting to do now is to specify that each element of the list is a non-empty map. Another approach is to specify that each element of the list is not null. Can any of this be done using Groovy DSL?

This:

validatedAnswers minLike(1) {
     type string()
     answers {
     }
}

doesn't work because it mean answers is expected to be empty ("Expected an empty Map but received Map( [...] )", see also #298).

So what I would like to do is something like this:

.withBody {
    validatedAnswers minLike(1) {
         type string()
         answers Matchers.map()
    }
}

or:

validatedAnswers minLike(1) {
     type string()
     answers {
             keyLike 'title', notNull()
     }
}

or:

validatedAnswers minLike(1) {
     type string()
     answers notNull()
}

Can it be done?

@QMG-kazala
Copy link
Author

QMG-kazala commented Aug 6, 2018

Anyone? Anything?
Is there a chance notNull() matcher (or map() or anything similar) will be added in the future?

@uglyog
Copy link
Member

uglyog commented Aug 12, 2018

This is impossible to support with the current implementation. The behaviour is to apply the matchers at the attribute level, and you can't apply them conditionally based on a predicate.

What we need to be able to apply a set of matchers based on a predicate:

validatedAnswers minLike(1) {
  when(type == "typeA") {
    favourite_colour string()
    ...
  }
}

But this will not be possible to implement without a change to the specification.

@QMG-kazala
Copy link
Author

Oh yes!!!! Having predicate-based matchers would solve so many problems! It would be great :D
👍
Does it mean it's planned for version 4?

@uglyog
Copy link
Member

uglyog commented Aug 26, 2018

Yes it will have to be a V4 change because it will require a change to the pact file format as well as how the matchers are applied.

@alexfdz
Copy link

alexfdz commented Jul 21, 2020

@uglyog will this be included in V4?

@uglyog
Copy link
Member

uglyog commented Jul 25, 2020

@alexfdz we are going to look at having sets of matching rules applied to different objects in lists. pact-foundation/pact-specification#38 will track this.

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

4 participants