Skip to content
Snippets Groups Projects
form.js 4.35 KiB
Newer Older
clemo's avatar
clemo committed
import debug from 'debug';
import validator from 'validator';
clemo's avatar
clemo committed
import faker from 'faker';
clemo's avatar
clemo committed

clemo's avatar
clemo committed
const log = debug('helper:form:info');
clemo's avatar
clemo committed

export class Form {
clemo's avatar
clemo committed
  /** open a form*/
clemo's avatar
clemo committed
  constructor(validation) { this.validation = validation; }
clemo's avatar
clemo committed
  /**
  *fill with content
  *@function
  *@param {object} object - object as data
  *@param {boolean} check - to veryfi the data?
  */
clemo's avatar
clemo committed
  fill(object, check = true) {
    for (let element in this.validation) {
clemo's avatar
clemo committed
      if ('undefined' !== typeof object && object[element]) {
clemo's avatar
clemo committed
        this.validation[element].value = object[element];
      }
clemo's avatar
clemo committed
      if (check) {
        this.validation[element].error = !this.isValid(element);
      }
    }
    return this.validation;
  } // end fill
clemo's avatar
clemo committed
    /** check if something is valid
    *@function
    *@param {element} - a element from the form, if null then everything will get
    *checked
    */
clemo's avatar
clemo committed
  isValid(element) {
    if ("undefined" !== typeof element) { // check 1 element
clemo's avatar
clemo committed
      if ("function" === typeof this.validation[element].convert) { // convert
clemo's avatar
clemo committed
        this.validation[element].checkValue =
            this.validation[element].convert(this.validation[element].value);
clemo's avatar
clemo committed
        log("convert", element, this.validation[element].value, 'to',
            this.validation[element].checkValue);
clemo's avatar
clemo committed
      } else if ("string" === typeof this.validation[element].convert) {
clemo's avatar
clemo committed
        log("convert", element, this.validation[element].value, 'to',
            this.validation[element].checkValue);
        this.validation[element].checkValue =
clemo's avatar
clemo committed
            validator[this.validation[element].convert](
                this.validation[element].value);
clemo's avatar
clemo committed
      } else {
        this.validation[element].checkValue = this.validation[element].value;
      }
clemo's avatar
clemo committed
      // validate
clemo's avatar
clemo committed
      log(typeof this.validation[element].validator,
          typeof validator[this.validation[element].validator],
          typeof this.validation[element].checkValue);

      if ("string" === typeof this.validation[element].validator &&
clemo's avatar
clemo committed
          "function" === typeof validator[this.validation[element].validator] &&
clemo's avatar
clemo committed
          "string" ===
              typeof this.validation[element]
                  .checkValue) { // validation function
        log("validat by validator string", element);
        return validator[this.validation[element].validator](
clemo's avatar
clemo committed
            this.validation[element].checkValue,
            this.validation[element].options);
      } else if ("function" === typeof this.validation[element].validator &&
clemo's avatar
clemo committed
                 "string" ===
                     typeof this.validation[element]
                         .checkValue) { // own validation function
clemo's avatar
clemo committed
        log("validat by validator function", element);
clemo's avatar
clemo committed
        return this.validation[element].validator(
            this.validation[element].checkValue);
clemo's avatar
clemo committed
      } 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 if ("undefined" === typeof this.validation[element].validator ||
                 !this.validation[element]
                      .required) { // no validation or not required
        log("validat no validator (true)", element);
clemo's avatar
clemo committed
        return true; // no validation, so valid
clemo's avatar
clemo committed
      }
clemo's avatar
clemo committed
      return false;
clemo's avatar
clemo committed
    } else { // check everything
clemo's avatar
clemo committed
      let valid = true;
clemo's avatar
clemo committed
      for (let e in this.validation) {
        if (!this.isValid(e)) {
clemo's avatar
clemo committed
          valid = false;
clemo's avatar
clemo committed
        }
      }
clemo's avatar
clemo committed
      return valid;
clemo's avatar
clemo committed
    }
  } // end isValid
clemo's avatar
clemo committed
  /** fill the form with fake data for uuidtests e.g.*/
clemo's avatar
clemo committed
  fakeFill(element) {

    if (element) {
clemo's avatar
clemo committed
      if ('string' === typeof this.validation[element].fake) {
clemo's avatar
clemo committed
        this.validation[element].value =
            faker.fake(this.validation[element].fake);
        log('fake fill', element, this.validation[element].value);
clemo's avatar
clemo committed
        return true;
      } else if ('function' === typeof this.validation[element].fake) {
        let fake = this.validation[element].fake();

        this.validation[element].value = faker.fake(fake);

clemo's avatar
clemo committed
        return true;
      }
      return false;
    } else {
      let filled = true;
      for (let e in this.validation) {
        if (!this.fakeFill(e)) {
          filled = false;
        }
      }
      return filled;
    }
  }
clemo's avatar
clemo committed
}

exports.default = Form;