Looking for a bit of advice on how to handle strin...
# general
d
Looking for a bit of advice on how to handle stringified json inside of a message body and regex against a couple of the keys. What I was trying to do is something like this
Copy code
it(`consumes a valid ${type} message`, async () => messagePact
         .expectsToReceive(`a valid ${provider} ${type} message`)
         .withContent({ Body: regex('tenantId', 'tenantId') })
         .withContent({ Body: regex('userId', 'userId') })
         .withContent({ Body: regex('verb', 'verb') })
         .withContent({ Body: regex('actor', 'actor') })
But the contract will expect the Body to be
Body: 'actor'
Copy code
{
      "contents": {
        "Body": "actor"
      },
      "description": "a valid assessment-artefact-service xapi message",
      "matchingRules": {
        "body": {
          "$.Body": {
            "combine": "AND",
            "matchers": [
              {
                "match": "regex",
                "regex": "tenantId"
              },
              {
                "match": "regex",
                "regex": "userId"
              }
...
If I manually delete the contents from the contracts, then the pact is valid and passes. I've got about 300 queues running like this and refactoring an entire architecture isn't feasible.
This would be the code I'd have to refactor, if it helps (JS)
Copy code
async function sendRecordServiceXapiLookupMessage({ logger, sqs }, {
  tenantId, contentId, artefactId, activityId, activityDetails,
}) {
  const message = JSON.stringify({
    source: process.env.SERVICE_NAME,
    type: ContentType.Assessment,
    tenantId,
    contentId,
    artefactId,
    activityId,
    activityDetails,
    timestamp: DateTime.local().toISO(),
  });
  <http://logger.info|logger.info>(`Request to record-service to send update xapi lookup for artefact with id ${artefactId.toString()}`);
  await sqs.sendMessage({
    QueueUrl: process.env.SQS_RECORD_STORE_XAPI_LOOKUP_QUEUE,
    MessageBody: message,
  }).promise();
}
m
Rather than
Copy code
.withContent({ Body: regex('tenantId', 'tenantId') })
         .withContent({ Body: regex('userId', 'userId') })
         .withContent({ Body: regex('verb', 'verb') })
         .withContent({ Body: regex('actor', 'actor') })
You just do something like his
Copy code
.withContent({ tenantId: regex('tenantId', 'tenantId'), userId: regex(/someregexfortheuser/, 'userId'),  } ...
i.e. the entire payload should be in a single
withContent
call. Also, are you sure
Body
is the correct key? It looks like the shape of the payload is:
Copy code
{
  source: "source",
  tenantId: "tenantId",
  ...
}
Also the regex matcher use looks incorrect, it would be something like this
Copy code
{
   tenantId: regex(/regex/, 'example that matches the regex')
   ...
}