Skip to content

Commit

Permalink
feat(validation): tougher rules on validate (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-lu authored Mar 3, 2021
1 parent e979351 commit 0fb26ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
15 changes: 14 additions & 1 deletion packages/utils/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ export const isValidEvent = (event: Event): boolean => {
return false;
}

if (event.device_id === undefined && event.user_id === undefined) {
const hasDeviceId = event.device_id !== undefined;
const hasUserId = event.user_id !== undefined;

if (!hasDeviceId && !hasUserId) {
logger.warn('Invalid event: expected at least one of device or user id');
return false;
}

if (hasDeviceId && typeof event.device_id !== 'string') {
logger.warn('Invalid event: expected device id to be a string if present');
return false;
}

if (hasUserId && typeof event.user_id !== 'string') {
logger.warn('Invalid event: expected user id to be a string if present');
return false;
}

return true;
};
38 changes: 28 additions & 10 deletions packages/utils/test/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,54 @@ import { isValidEvent } from '../src/validate';
describe('isValidEvent', () => {
it('should pass on valid events with device id', () => {
const validEvent: Event = {
event_type: 'NOT_A_REAL_EVENT_TYPE',
device_id: 'NOT_A_REAL_DEVICE_ID',
event_type: 'VALID_BUT_FAKE_EVENT_TYPE',
device_id: 'VALID_BUT_FAKE_DEVICE_ID',
};

expect(isValidEvent(validEvent)).toBe(true);
});

it('should pass on valid events with user id', () => {
const validEvent: Event = {
event_type: 'NOT_A_REAL_EVENT_TYPE',
user_id: 'NOT_A_REAL_USER_ID',
event_type: 'VALID_BUT_FAKE_EVENT_TYPE',
user_id: 'VALID_BUT_FAKE_USER_ID',
};

expect(isValidEvent(validEvent)).toBe(true);
});

it('should fail on valid events with no user or device id', () => {
const validEvent: Event = {
event_type: 'NOT_A_REAL_EVENT_TYPE',
const invalidEvent: Event = {
event_type: 'VALID_BUT_FAKE_EVENT_TYPE',
} as any;

expect(isValidEvent(validEvent)).toBe(false);
expect(isValidEvent(invalidEvent)).toBe(false);
});

it('should fail on valid events with an invalid event type', () => {
const validEvent: Event = {
const invalidEvent: Event = {
event_type: 3,
user_id: 'NOT_A_REAL_USER_ID',
user_id: 'VALID_BUT_FAKE_USER_ID',
} as any;

expect(isValidEvent(invalidEvent)).toBe(false);
});

it('should fail on valid events with an invalid user id', () => {
const invalidEvent: Event = {
event_type: 'VALID_BUT_FAKE_EVENT_TYPE',
user_id: 3,
} as any;

expect(isValidEvent(invalidEvent)).toBe(false);
});

it('should fail on valid events with an invalid device id', () => {
const invalidEvent: Event = {
event_type: 'VALID_BUT_FAKE_EVENT_TYPE',
device_id: 3,
} as any;

expect(isValidEvent(validEvent)).toBe(false);
expect(isValidEvent(invalidEvent)).toBe(false);
});
});

0 comments on commit 0fb26ce

Please sign in to comment.