all files / lib/ form.js

98.25% Statements 56/57
95.45% Branches 42/44
100% Functions 4/4
98.21% Lines 55/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112     16×   16× 12×         31× 28×     21×         16×     28×       28×         17× 17×     11×                             16×               14× 12×           12×              
import debug from 'debug';
import validator from 'validator';
import faker from 'faker';
 
const log = debug('helper:form');
 
export class Form {
  constructor(validation) { this.validation = validation; }
  fill(object, check = true) {
    for (let element in this.validation) {
      if ('undefined' !== typeof object && object[element]) {
        this.validation[element].value = object[element];
      }
      if (check) {
        this.validation[element].error = !this.isValid(element);
      }
    }
    return this.validation;
  } // end fill
  isValid(element) {
    if ("undefined" !== typeof element) { // check 1 element
      if ("function" === typeof this.validation[element].convert) { // convert
        this.validation[element].checkValue =
            this.validation[element].convert(this.validation[element].value);
        log("convert", element, this.validation[element].value, 'to',
            this.validation[element].checkValue);
      } else if ("string" === typeof this.validation[element].convert) {
        log("convert", element, this.validation[element].value, 'to',
            this.validation[element].checkValue);
        this.validation[element].checkValue =
            validator[this.validation[element].convert](
                this.validation[element].value);
      } else {
        this.validation[element].checkValue = this.validation[element].value;
      }
      // validate
      log(typeof this.validation[element].validator,
          typeof validator[this.validation[element].validator],
          typeof this.validation[element].checkValue);
 
      if ("string" === typeof this.validation[element].validator &&
          "function" === typeof validator[this.validation[element].validator] &&
          "string" ===
              typeof this.validation[element]
                  .checkValue) { // validation function
        log("validat by validator string", element);
        return validator[this.validation[element].validator](
            this.validation[element].checkValue,
            this.validation[element].options);
      } else if ("function" === typeof this.validation[element].validator &&
                 "string" ===
                     typeof this.validation[element]
                         .checkValue) { // own validation function
        log("validat by validator function", element);
        return this.validation[element].validator(
            this.validation[element].checkValue);
      } else if (this.validation[element].required &&
                 ("undefined" === typeof this.validation[element].checkValue ||
                  "" ===
                      this.validation[element]
                          .checkValue)) { // validation but no value
        log("validat required but no value", element);
        return false;
      } else Eif ("undefined" === typeof this.validation[element].validator ||
                 !this.validation[element]
                      .required) { // no validation or not required
        log("validat no validator (true)", element);
        return true; // no validation, so valid
      }
      return false;
 
    } else { // check everything
      let valid = true;
      for (let e in this.validation) {
        if (!this.isValid(e)) {
          valid = false;
        }
      }
      return valid;
    }
  } // end isValid
 
  fakeFill(element) {
 
    if (element) {
      if ('string' === typeof this.validation[element].fake) {
        this.validation[element].value =
            faker.fake(this.validation[element].fake);
        log('fake fill', element, this.validation[element].value);
        return true;
      } else if ('function' === typeof this.validation[element].fake) {
        let fake = this.validation[element].fake();
 
        this.validation[element].value = faker.fake(fake);
 
        return true;
      }
      return false;
    } else {
      let filled = true;
      for (let e in this.validation) {
        if (!this.fakeFill(e)) {
          filled = false;
        }
      }
      return filled;
    }
  }
}
 
exports.default = Form;