Repository URL to install this package:
|
Version:
1.0.0-alpha.7 ▾
|
/**
* Helper function to build an error message for a missing field
*
* @param {string} val The dotted path to the missing field
* @return {string} The error message
*/
const errorTemplate = val => `Mandatory tracking value "${val}" is missing`;
/**
* Validates tracking calls
*
* @param {Object} trackingData The data that needs to be tracked
* @return {Array.<Error>}
*/
const validateTrackingData = ({ track, page, identify }) => {
const errors = [];
// Validate mandatory fields for Track API
if (track) {
if (!track.event) {
errors.push('track.event');
}
if (!track.properties || !track.properties.category) {
errors.push('track.properties.category');
}
}
// Validate mandatory fields for Page API
if (page) {
if (!page.name) {
errors.push('page.name');
}
if (!page.properties || !page.properties.url) {
errors.push('page.properties.url');
}
if (!page.properties || !page.properties.title) {
errors.push('page.properties.title');
}
if (!page.properties || !page.properties.path) {
errors.push('page.properties.path');
}
}
// Validate mandatory fields for Identify API
if (identify) {
if (!identify.userId) {
errors.push('identify.userId');
}
}
return errors.map(e => new Error(errorTemplate(e)));
};
export { validateTrackingData };