daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const validator = require('../lib/validator/validator');
  2. const InvalidInputError = require('../lib/validator/InvalidInputError');
  3.  
  4. class User {
  5.   constructor(id, type, userName, firstName, lastName, email) {
  6.     this.id = id;
  7.     this.type = type;
  8.     this.userName = userName;
  9.     this.firstName = firstName;
  10.     this.lastName = lastName;
  11.     this.email = email;
  12.   }
  13.  
  14.   get id() {
  15.     return this._id;
  16.   }
  17.  
  18.   set id(id) {
  19.     const validators = [validator.validateId, validator.validateNonEmpty];
  20.     this.setWithValidation('_id', id, validators);
  21.   }
  22.  
  23.   get type() {
  24.     return this._type;
  25.   }
  26.  
  27.   set type(type) {
  28.     const validators = [validator.validateType, validator.validateNonEmpty];
  29.     this.setWithValidation('_type', type, validators);
  30.   }
  31.  
  32.   get userName() {
  33.     return this._userName;
  34.   }
  35.  
  36.   set userName(userName) {
  37.     const validators = [validator.validateUserName, validator.validateNonEmpty];
  38.     this.setWithValidation('_userName', userName, validators);
  39.   }
  40.  
  41.   get firstName() {
  42.     return this._firstName;
  43.   }
  44.  
  45.   set firstName(firstName) {
  46.     const validators = [validator.validateFirstOrLastName];
  47.     this.setWithValidation('_firstName', firstName, validators);
  48.   }
  49.  
  50.   get lastName() {
  51.     return this._lastName;
  52.   }
  53.  
  54.   set lastName(lastName) {
  55.     const validators = [validator.validateFirstOrLastName]
  56.     this.setWithValidation('_lastName', lastName, validators);
  57.   }
  58.  
  59.   get email() {
  60.     return this._email;
  61.   }
  62.  
  63.   set email(email) {
  64.     const validators = [validator.validateEmail];
  65.     this.setWithValidation('_email', email, validators);
  66.   }
  67.  
  68.   setWithValidation(key, value, validators) {
  69.     const dataIsValid = validators.every(function(validator) {
  70.       return validator(value);
  71.     })
  72.     if(!dataIsValid) {
  73.       throw new InvalidInputError(`Input Error: User input has incorrect \'${key}\' field`);
  74.     }
  75.     this[key] = value ? value : null;
  76.   }
  77.  
  78.   serializeToArray() {
  79.     return [this.type, this.id, this.userName, this.firstName, this.lastName, this.email];
  80.   }
  81.  
  82.   static deserializeToObject(arrayOfObjectValues) {
  83.     const userObject = {
  84.       id: arrayOfObjectValues[1],
  85.       userName: arrayOfObjectValues[2],
  86.       firstName: arrayOfObjectValues[3],
  87.       lastName: arrayOfObjectValues[4],
  88.       email: arrayOfObjectValues[5],
  89.       type: arrayOfObjectValues[0]
  90.     }
  91.  
  92.     return userObject;
  93.   }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top