All files / core/src index.ts

85.88% Statements 73/85
65.9% Branches 29/44
92.3% Functions 12/13
85.18% Lines 69/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                                        5x 5x 5x 5x 5x 1x 1x   5x 1x         5x 5x 5x               5x 5x 5x 5x 5x 5x 2x 2x 2x 2x 10x 10x 10x                         10x   2x 1x       5x 30x 30x 30x 11x   30x 30x 30x 3x   30x 15x   30x 22x   8x 8x   30x 6x   24x   5x 2x 2x 2x       5x 2x 1x   2x   5x 2x 2x 2x 2x 2x 1x   2x   13x           4x 12x   4x 8x 2x     2x      
export type Value = (number | FormDataEntryValue)[] | number | boolean | null | FormDataEntryValue;
export type Values = Partial<Record<string, Value>>;
export type Fields = Partial<Record<string, boolean>>;
export type Rules = Partial<Record<string, RulesOption>>;
 
export interface RulesOption {
  /** Validate the form's values with function. */
  validate?(value?: Value, values?: Validator['values'], field?: string): string;
}
 
export type ValidatorOption = {
  messagesShown?: boolean;
  rules?: Rules;
  initValues?: Values;
  form?: HTMLFormElement | null;
  validate?: RulesOption['validate'];
}
 
export default class Validator {
  constructor(options: ValidatorOption = {}) {
    const { form, rules, initValues, messagesShown, validate } = options;
    this.messagesShown = !!messagesShown;
    this.rules = rules || {};
    this.validate = validate;
    if (initValues) {
      this.values = { ...initValues };
      this.initValues = { ...initValues };
    }
    if (form) {
      this.setForm(form);
    }
  }
  validate?: RulesOption['validate'];
  form?: HTMLFormElement | null;
  fields: Fields = {};
  rules: Rules = {};
  values: Values = {};
  initValues?: Values;
  set resetInitValue(val: Values) {
    this.initValues = val;
    if (this.form) {
      this.setForm(this.form);
    }
  }
  messagesShown: boolean = false;
  errorMessages: Partial<Record<string, string>> = {};
  showMessages = () => this.messagesShown = true;
  hideMessages = () => this.messagesShown = false;
  getForm = () => this.form;
  setForm = (form: HTMLFormElement) => {
    this.form = form;
    this.values = {};
    const formData = new FormData(this.form);
    formData.forEach((value, key) => {
      const valArr = formData.getAll(key);
      const elm: HTMLInputElement = this.form![key];
      Iif (elm) {
        if (elm.type === 'checkbox') {
          this.values[key] = elm.checked;
        } else if (elm.multiple) {
          this.values[key] = valArr;
        } else if (elm.type) {
          this.values[key] = formData.get(key);
        } else if (valArr.length > 0 && !elm.value) {
          this.values[key] = valArr;
        } else {
          this.values[key] = elm.value;
        }
      }
      this.message(key, this.values[key], this.rules[key]);
    });
    if (!this.initValues) {
      this.initValues = this.values;
    }
  };
  /** How you define validation rules and add messages into the form. */
  message = (field: string, inputValue?: Value, options?: RulesOption) => {
    this.fields[field] = true;
    this.values[field] = inputValue;
    if (options) {
      this.rules[field] = options;
    }
    const { validate } = this.rules[field] || {};
    let msg: string = '';
    if (this.validate) {
      msg = this.validate(this.values[field], this.values, field);
    }
    if (validate) {
      msg = validate(this.values[field], this.values, field);
    }
    if (!msg) {
      delete this.errorMessages[field];
    } else {
      this.fields[field] = false;
      this.errorMessages[field] = msg;
    }
    if (this.messagesShown) {
      return this.errorMessages[field];
    }
    return;
  };
  setValues = (values: Values = {}) => {
    Eif (!this.form) {
      for (let val in values) {
        this.message(val, values[val]);
      }
    }
  }
  getValues = (): Values => {
    if (this.form) {
      this.setForm(this.form);
    }
    return this.values;
  }
  reset = (): Values => {
    this.values = { ...this.initValues };
    this.errorMessages = {};
    this.messagesShown = false;
    this.fields = {};
    if (this.form) {
      this.form.reset();
    };
    return { ...this.values };
  }
  fieldValid = (field: string) => this.fields[field] === true;
  /**
   * Returns a boolean if all the fields pass validation or not.
   * @returns Boolean
   */
  allValid() {
    for (var key in this.values) {
      this.message(key, this.values[key], this.rules[key]);
    }
    for (let key in this.fields) {
      if (this.fieldValid(key) === false) {
        return false;
      }
    }
    return true;
  }
}