eightsleep
Version:
eightsleep api client
98 lines • 4.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const baseerr_1 = __importDefault(require("baseerr"));
const validateDevice_1 = require("./validateDevice");
const util_1 = require("util");
const isValueOf_1 = __importDefault(require("./isValueOf"));
var Genders;
(function (Genders) {
Genders["MALE"] = "male";
Genders["FEMALE"] = "female";
})(Genders || (Genders = {}));
var Sides;
(function (Sides) {
Sides["LEFT"] = "left";
Sides["RIGHT"] = "right";
Sides["SOLO"] = "solo";
})(Sides || (Sides = {}));
class InvalidUserError extends baseerr_1.default {
}
function validateUser(user) {
if (typeof user != 'object') {
throw new InvalidUserError('invalid user', { user });
}
let { userId, email, firstName, lastName, gender, dob, zip, devices, emailVerified, sharingMetricsTo, sharingMetricsFrom, timezone, notifications, features, currentDevice, } = user;
if (typeof userId !== 'string')
throw new InvalidUserError('invalid userId', { user });
if (typeof email !== 'string')
throw new InvalidUserError('invalid email', { user });
if (typeof firstName !== 'string')
throw new InvalidUserError('invalid firstName', { user });
if (typeof lastName !== 'string')
throw new InvalidUserError('invalid lastName', { user });
if (!isValueOf_1.default(Genders)(gender))
throw new InvalidUserError('invalid gender', { user });
dob = new Date(dob);
if (isNaN(dob.valueOf()))
throw new InvalidUserError('invalid dob', { user });
if (typeof zip !== 'number')
throw new InvalidUserError('invalid zip', { user });
if (!Array.isArray(devices))
throw new InvalidUserError('invalid devices', { user });
if (devices.length && !devices.every(util_1.isString))
throw new InvalidUserError('invalid devices', { user });
if (typeof emailVerified !== 'boolean')
throw new InvalidUserError('invalid emailVerified', { user });
if (!Array.isArray(sharingMetricsTo))
throw new InvalidUserError('invalid sharingMetricsTo', { user });
if (sharingMetricsTo.length && !sharingMetricsTo.every(util_1.isString))
throw new InvalidUserError('invalid sharingMetricsTo', { user });
if (!Array.isArray(sharingMetricsFrom))
throw new InvalidUserError('invalid sharingMetricsFrom', { user });
if (sharingMetricsFrom.length && !sharingMetricsFrom.every(util_1.isString))
throw new InvalidUserError('invalid sharingMetricsFrom', { user });
if (typeof timezone !== 'string')
throw new InvalidUserError('invalid timezone', { user });
if (typeof notifications !== 'object')
throw new InvalidUserError('invalid notifications', { user });
if (typeof notifications.weeklyReportEmail !== 'boolean')
throw new InvalidUserError('invalid notifications.weeklyReportEmail', {
user,
});
if (typeof notifications.sessionProcessed !== 'boolean')
throw new InvalidUserError('invalid notifications.sessionProcessed', {
user,
});
if (!Array.isArray(features))
throw new InvalidUserError('invalid features', { user });
if (features.length && !features.every(isValueOf_1.default(validateDevice_1.Features)))
throw new InvalidUserError('invalid sharingMetricsFrom', { user });
if (typeof currentDevice !== 'object')
throw new InvalidUserError('invalid currentDevice', { user });
if (typeof currentDevice.id !== 'string')
throw new InvalidUserError('invalid currentDevice.id', { user });
if (!isValueOf_1.default(Sides)(currentDevice.side))
throw new InvalidUserError('invalid currentDevice.side', { user });
return {
userId,
email,
firstName,
lastName,
gender,
dob,
zip,
devices,
emailVerified,
sharingMetricsTo,
sharingMetricsFrom,
timezone,
notifications,
features,
currentDevice,
};
}
exports.default = validateUser;
//# sourceMappingURL=validateUser.js.map