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(api-service): email 'if' condition truthy values #7551

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

ChmaraX
Copy link
Contributor

@ChmaraX ChmaraX commented Jan 21, 2025

What changed? Why was the change needed?

Evaluate true and false conditions from payload based on JS evaluation.

Copy link

linear bot commented Jan 21, 2025

@@ -241,7 +241,7 @@ export function mergeCommonObjectKeys(
sourceValue as Record<string, unknown>
);
} else {
merged[key] = sourceValue ?? targetValue;
merged[key] = key in source ? sourceValue : targetValue;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check presence of key in payload instead of a value - If I put null in preview payload as a value of some attribute (e.g. to check ifShowKey condition), I want it to not get it overwritten by placeholder string

Copy link

netlify bot commented Jan 21, 2025

Deploy Preview for dev-web-novu ready!

Name Link
🔨 Latest commit 57dea43
🔍 Latest deploy log https://app.netlify.com/sites/dev-web-novu/deploys/678faf7f59a473000848cf46
😎 Deploy Preview https://deploy-preview-7551.dashboard.novu-staging.co
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jan 21, 2025

Deploy Preview for dashboard-v2-novu-staging ready!

Name Link
🔨 Latest commit 57dea43
🔍 Latest deploy log https://app.netlify.com/sites/dashboard-v2-novu-staging/deploys/678faf7fe9a9b90008bdf544
😎 Deploy Preview https://deploy-preview-7551.dashboard-v2.novu-staging.co
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

return value.toLowerCase() === 'true';
}
private stringToBoolean(value: string): boolean {
const normalized = value.toLowerCase().trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value.toLowerCase() could throw an exception if we value null/undefined/false/true and on everything that is not a string etc..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will always be a string, the function stringToBoolean accepts only string as an argument.

The reason is because parseLiquid() function returns always string:

  private async evaluateShowCondition(
    variables: FullPayloadForRender,
    node: MailyJSONContent & { attrs: { [MailyAttrsEnum.SHOW_IF_KEY]: string } }
  ): Promise<boolean> {
    const { [MailyAttrsEnum.SHOW_IF_KEY]: showIfKey } = node.attrs;
    const parsedShowIfValue = await this.parseLiquid(showIfKey, variables);

    return this.stringToBoolean(parsedShowIfValue);
  }


return false;
try {
return Boolean(JSON.parse(value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the usecase for executing JSON.parse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I would remove the JSON.parse and just do Boolean(value), some originally falsy values (like zeroes in the example) would resolve to true because it would be treated a string, but they should be false.

Boolean('0') // returns true (incorrect: because `0` as a number is falsy)
Boolean(JSON.parse('0')) // returns false

Boolean('0e0') // returns true
Boolean(JSON.parse('0e0')) // returns false

Boolean('-0') // returns true
Boolean(JSON.parse('-0')) // returns false
...

The issue we currently have is that the liquid parser always returns string, we can't distinguish if user typed 100 or "100" in the payload, we always assume it was number for now.

Copy link
Contributor

@LetItRock LetItRock Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also pass here the normalized value? also in the catch block

{ value: 'true', desc: 'string "true"' },
{ value: 'yes', desc: 'string "yes"' },
{ value: {}, desc: 'empty object' },
{ value: [], desc: 'empty array' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the tests where the normalization will come into play

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

Successfully merging this pull request may close these issues.

4 participants