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

Fix: eventType passed in as array to RULES matches with wrong event #135

Open
shesupplypi opened this issue Jan 14, 2022 · 1 comment
Open

Comments

@shesupplypi
Copy link

Describe the bug

I am sure this is an edge case, but I found this behavior when testing out the code.

The eventType provided in the RULES is matching with the wrong event.type when two names are similar but differ by only the last letter.

For example, when creating a rule with an eventType:

eventType: ['something-updated', 'something-deleted']

and passing an event into the handler with this type:

      id: 'fcc12355-f339-4f71-bbed-eee646535bbb',
      type: 'something-update',

It will match that rule though the event types are not the same.

To Reproduce
Steps to reproduce the behavior:

  1. Create an event:
   {
      id: 'fcc12355-f339-4f71-bbed-eee646535bbb',
      type: 'something-update',
   }
  1. Create a rule similar to the type in the event, but with an extra letter at the end, like so:
{
      id: 'p4',
      flavor: materialize,
      eventType: ['something-update**d'**, 'something-deleted'],
      toUpdateRequest
   }
  1. Pass event as a kinesis record into the handler:
export const handler = async (event) =>
   initialize(PIPELINES, OPTIONS)
      .assemble(fromKinesis(event))
      .through(toPromise)
  1. You will see that the toUpdateRequest function is called

Expected behavior
The event.type in the event body should only match with the eventType in the RULES when the match is exact.

@shesupplypi
Copy link
Author

shesupplypi commented Jan 14, 2022

Suggested fix, in filterOnEventType:

Remove .join() in the isArray else statement

Change this code:

export const filterOnEventType = (rule, uow) => {
  if (typeof rule.eventType === 'string') {
    return uow.event.type === rule.eventType;
  } else if (rule.eventType instanceof RegExp) {
    return rule.eventType.test(uow.event.type);
  } else if (isArray(rule.eventType)) {
    return rule.eventType.join().indexOf(uow.event.type) > -1;
  } else if (isFunction(rule.eventType)) {
    return rule.eventType(uow.event.type, rule);
  } else {
    throw new Error(`Rule: ${rule.id}, has improperly configured eventType filter. Must be a string, array of string, regex or function.`);
  }
};

to this:

export const filterOnEventType = (rule, uow) => {
  if (typeof rule.eventType === 'string') {
    return uow.event.type === rule.eventType;
  } else if (rule.eventType instanceof RegExp) {
    return rule.eventType.test(uow.event.type);
  } else if (isArray(rule.eventType)) {
    return rule.eventType.indexOf(uow.event.type) > -1;
  } else if (isFunction(rule.eventType)) {
    return rule.eventType(uow.event.type, rule);
  } else {
    throw new Error(`Rule: ${rule.id}, has improperly configured eventType filter. Must be a string, array of string, regex or function.`);
  }
};

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

1 participant