citeright-sdk-js
Version:
An SDK to connect to the CiteRight API.
121 lines (120 loc) • 5.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var validator = require("validator");
var PasswordChangeDTO_1 = require("./PasswordChangeDTO");
var RegistrationRequestDTO = /** @class */ (function () {
function RegistrationRequestDTO(email, password, firstname, lastname, companyName, planId, mpAnonymousId) {
if (email === void 0) { email = ''; }
if (password === void 0) { password = ''; }
if (firstname === void 0) { firstname = ''; }
if (lastname === void 0) { lastname = ''; }
if (companyName === void 0) { companyName = ''; }
if (planId === void 0) { planId = ''; }
if (mpAnonymousId === void 0) { mpAnonymousId = ''; }
this.email = email;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.companyName = companyName;
this.planId = planId;
this.mpAnonymousId = mpAnonymousId;
this.errors = [];
}
RegistrationRequestDTO.createFromAny = function (input) {
var dto = new RegistrationRequestDTO();
for (var _i = 0, _a = Object.getOwnPropertyNames(new RegistrationRequestDTO()); _i < _a.length; _i++) {
var property = _a[_i];
dto[property] = input[property] ? input[property] : undefined;
}
return dto;
};
RegistrationRequestDTO.prototype.isValid = function () {
this.errors = [];
this.trimAllFields();
this.verifyAllFieldsPresent();
this.normalizeEmail();
this.validatePassword();
return !!!this.errors.length;
};
RegistrationRequestDTO.prototype.getErrors = function () {
return this.errors;
};
/**
* This function will validate the supplied password. Note that although the PasswordChangeDTO is able to
* generate a detailed list of errors, we are not using that here, becuase it is assumed that there will be
* a detailed check in the UI and this is just a safeguard.
*/
RegistrationRequestDTO.prototype.validatePassword = function () {
// Make sure the user typed a password that meets our standards
var passwordChangeDTO = new PasswordChangeDTO_1.PasswordChangeDTO();
passwordChangeDTO.newPassword = this.password;
passwordChangeDTO.confirmPassword = this.password;
var validation = passwordChangeDTO.validate();
if (!validation.isValid) {
this.errors.push('Invalid password');
}
};
/**
* Some people tend to leave spaces before and after words unnecessarily. Here we ALSO make sure none of the fields
* are left undefined, as "undefined" will crash the validator.
*/
RegistrationRequestDTO.prototype.trimAllFields = function () {
this.email = this.email ? validator.trim(this.email) : '';
this.firstname = this.firstname ? validator.trim(this.firstname) : '';
this.lastname = this.lastname ? validator.trim(this.lastname) : '';
this.companyName = this.companyName ? validator.trim(this.companyName) : '';
this.planId = this.planId ? validator.trim(this.planId) : '';
this.mpAnonymousId = this.mpAnonymousId ? validator.trim(this.mpAnonymousId) : '';
};
/**
* This function will add to the error list for every missing field. Note that the exception here is email because
* there is a whole other function dedicated to validating the email address supplied.
*/
RegistrationRequestDTO.prototype.verifyAllFieldsPresent = function () {
if (!this.firstname) {
this.errors.push('Missing field: firstname');
}
if (!this.lastname) {
this.errors.push('Missing field: lastname');
}
if (!this.companyName) {
this.errors.push('Missing field: companyName');
}
if (!this.password) {
this.errors.push('Missing field: password');
}
if (!this.planId) {
this.errors.push('Missing field: planId');
}
if (!this.mpAnonymousId) {
this.errors.push('Missing field: mpAnonymousId');
}
};
RegistrationRequestDTO.prototype.normalizeEmail = function () {
if (!this.email || !validator.isEmail(this.email)) {
this.errors.push('Invalid or missing email address');
}
else {
// Let people use sub addresses if they want.
// We just want the emails to be deliverable.
var normalizeOptions = {
gmail_convert_googlemaildotcom: false,
gmail_remove_dots: false,
gmail_remove_subaddress: false,
icloud_lowercase: false,
icloud_remove_subaddress: false,
outlookdotcom_remove_subaddress: false,
yahoo_lowercase: false,
yahoo_remove_subaddress: false,
};
if (this.email) {
var validEmail = validator.normalizeEmail(this.email, normalizeOptions);
if (validEmail) {
this.email = validEmail;
}
}
}
};
return RegistrationRequestDTO;
}());
exports.RegistrationRequestDTO = RegistrationRequestDTO;