Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Joi = require('joi-browser');
const VALID_TOURS = ["private", "addons", "customize", "search", "default", "sync", "library", "singlesearch", "performance", "screenshots"];
const baseKeys = {
client_id: Joi.string().required(),
addon_version: Joi.string().required(),
locale: Joi.string().required(),
session_id: Joi.string(),
page: Joi.valid(["about:home", "about:newtab"])
};
const BasePing = Joi.object().keys(baseKeys).options({allowUnknown: true});
const OverlaySessionPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
session_duration: Joi.number().integer(),
event: Joi.valid("overlay_session").required(),
category: Joi.valid("overlay_interactions").required()
}));
const OverlayHideTourEventPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
event: Joi.valid("overlay-hide-tour").required(),
tour_id: Joi.valid(VALID_TOURS),
notification_impression: Joi.number().integer(),
category: Joi.valid("overlay_interactions").required()
}));
const OverlayClickTourEventPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
event: Joi.valid("overlay-nav-click").required(),
tour_id: Joi.valid(VALID_TOURS),
notification_impression: Joi.number().integer(),
category: Joi.valid("overlay_interactions").required()
}));
const OverlayClickCTAEventPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
event: Joi.valid("overlay-cta-click").required(),
tour_id: Joi.valid(VALID_TOURS),
notification_impression: Joi.number().integer(),
category: Joi.valid("overlay_interactions").required()
}));
const NotificationSessionPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
session_duration: Joi.number().integer(),
event: Joi.valid("notificaiton_session").required(),
category: Joi.valid("notificaiton_interactions").required()
}));
const NotificationClickCTAEventPing = Joi.object().keys(Object.assign({}, baseKeys, {
session_id: baseKeys.session_id.required(),
page: baseKeys.page.required(),
event: Joi.valid("notification-cta-click").required(),
tour_id: Joi.valid(VALID_TOURS),
notification_impression: Joi.number().integer(),
category: Joi.valid("notificaiton_interactions").required()
}));
function chaiAssertions(_chai, utils) {
const {Assertion} = _chai;
Assertion.addMethod("validate", function(schema, schemaName) {
const {error} = Joi.validate(this._obj, schema, {allowUnknown: false});
this.assert(
!error,
`Expected to be ${schemaName ? `a valid ${schemaName}` : "valid"} but there were errors: ${error}`
);
});
const assertions = {
/**
* assert.validate - Validates an item given a Joi schema
*
* @param {any} actual The item to validate
* @param {obj} schema A Joi schema
*/
validate(actual, schema, schemaName) {
new Assertion(actual).validate(schema, schemaName);
},
/**
* isUserEventAction - Passes if the item is a valid UserEvent action
*
* @param {any} actual The item to validate
*/
isUserEventAction(actual) {
new Assertion(actual).validate(UserEventAction, "UserEventAction");
}
};
Object.assign(_chai.assert, assertions);
}
module.exports = {
baseKeys,
BasePing,
OverlaySessionPing,
OverlayHideTourEventPing,
OverlayClickTourEventPing,
OverlayClickCTAEventPing,
NotificationSessionPing,
NotificationClickCTAEventPing,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment