Skip to content

Commit

Permalink
fix: add email validation in HS
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Jan 9, 2025
1 parent 57fc4fe commit 364ac25
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"unset-value": "^2.0.1",
"uuid": "^9.0.1",
"valid-url": "^1.0.9",
"validator": "^13.12.0",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions src/v0/destinations/hs/HSTransform-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ConfigurationError,
InstrumentationError,
} = require('@rudderstack/integrations-lib');
const validator = require('validator');
const { MappedToDestinationKey, GENERIC_TRUE_VALUES } = require('../../../constants');
const {
defaultPostRequestConfig,
Expand Down Expand Up @@ -72,6 +73,11 @@ const addHsAuthentication = (response, Config) => {
const processIdentify = async ({ message, destination, metadata }, propertyMap) => {
const { Config } = destination;
let traits = getFieldValueFromMessage(message, 'traits');
// since hubspot does not allow imvalid emails, we need to
// validate the email before sending it to hubspot
if (traits?.email && !validator.isEmail(traits.email)) {
throw new InstrumentationError(`Email ${traits.email} is invalid`);
}
const mappedToDestination = get(message, MappedToDestinationKey);
const operation = get(message, 'context.hubspotOperation');
const externalIdObj = getDestinationExternalIDObjectForRetl(message, 'HS');
Expand Down
82 changes: 82 additions & 0 deletions test/integrations/destinations/hs/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5425,4 +5425,86 @@ export const data = [
},
},
},
{
name: 'hs',
description:
'[HS] (New API v3) - throw error if invalid email is provided in traits for identify call',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
description:
'[HS] (New API v3) - throw error if invalid email is provided in traits for identify call',
message: {
channel: 'web',
context: {
traits: {
email: 'incorrect-email',
firstname: 'Test Hubspot',
},
library: {
name: 'RudderLabs JavaScript SDK',
version: '1.0.0',
},
},
type: 'identify',
messageId: 'e8585d9a-7137-4223-b295-68ab1b17dad7',
originalTimestamp: '2025-01-01T09:35:31.289Z',
anonymousId: '00000000000000000000000000',
userId: '12345',
properties: '',
integrations: {
All: true,
},
sentAt: '2019-10-14T11:15:53.296Z',
},
destination: {
Config: {
authorizationType: 'newPrivateAppApi',
hubID: '',
apiKey: '',
accessToken: 'dummy-access-token',
apiVersion: 'newApi',
lookupField: 'lookupField',
eventFilteringOption: 'disable',
blacklistedEvents: [
{
eventName: '',
},
],
whitelistedEvents: [
{
eventName: '',
},
],
},
Enabled: true,
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
error: 'Email incorrect-email is invalid',
statTags: {
destType: 'HS',
errorCategory: 'dataValidation',
errorType: 'instrumentation',
feature: 'processor',
implementation: 'native',
module: 'destination',
},
statusCode: 400,
},
],
},
},
},
];

0 comments on commit 364ac25

Please sign in to comment.