eightsleep
Version:
eightsleep api client
92 lines • 3.89 kB
JavaScript
import BaseError from 'baseerr';
import { Features } from './validateDevice';
import { isString } from 'util';
import isValueOf from './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 BaseError {
}
export 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(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(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(isString))
throw new InvalidUserError('invalid sharingMetricsTo', { user });
if (!Array.isArray(sharingMetricsFrom))
throw new InvalidUserError('invalid sharingMetricsFrom', { user });
if (sharingMetricsFrom.length && !sharingMetricsFrom.every(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(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(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,
};
}
//# sourceMappingURL=validateUser.js.map