Quick actions

cmd+k|ctrl+k

Navigation

Languages

FormItem validate

Snippet info

Language

JavaScript

Visibility

public

Author

paulkotov

Created

2022-12-19T20:54:39.429752Z

Updated

2023-04-27T21:02:31.470341Z

const isEmptyObject = (val) => val !== null && typeof val === 'object' && !Object.keys(val).length;
const isEmpty = (val) => !Boolean(val);
const isNull = (val) => val === null;
const isUndefined = (val) => val === undefined;

const DEFAULT_PARAMS = {lang: null, display: 'kv'};

const formItemChecks = {
  check_error_format: [],
  check_error_height: {kv: 500},
  check_error_ratio: null,
  check_error_regex: '',
  check_error_size: {kv: 1},
  check_error_width: {},
  check_mandatory: true,
  check_max_characters: 57
};

const formItemLabels = {
  error_format: 'format',
  error_height: 'More than height',
  error_max_characters: 'More than max',
  error_ratio: 'More than ratio',
  error_regex: 'Not match',
  error_size: 'More than size', // ? get message
  error_width: 'More than width',
  mandatory: 'Empty field'
};

const checkRegexp = (val, reg) => reg.test(val);
const checkLessThanMax = (val, number) => val <= number;
const checkMapper = {
  check_error_regex: checkRegexp,
  check_error_width: checkLessThanMax,
  check_error_height: checkLessThanMax,
  check_error_ratio: checkLessThanMax,
  check_error_size: checkLessThanMax,
  check_max_characters: checkLessThanMax,
};

function createChecks(formItem, labels) {
  const checks = {};
  const messages = {};
  if (labels) {
    Object.keys(formItem)
      .filter((key) => key.match(/check/))
      .forEach((key) => {
          const checkKey = formItem[key];
          if (checkKey && !isEmptyObject(checkKey)) {
              checks[key] = checkKey;
          }
      });

    Object.keys(checks)
      .forEach((key) => {
        const checkKey = key.replace('check_', '');
        messages[key] = labels[checkKey];
      });
  }
  return { checks, messages };
}

function validate(val, {checks, messages}, params = DEFAULT_PARAMS) {
  const {lang, display} = params;
    // console.log({checks, messages});
  if (!Object.keys(checks).length || isEmpty(checks)) {
    return '';
  }
  if (checks.check_mandatory && (!val || isEmptyObject(val))) {
    return messages.check_mandatory;
  }

  // eslint-disable-next-line guard-for-in
  for (let key in checks) {
    const checkFn = checkMapper[key];
    const valueKey = typeof val === 'string' ? val.length : val[key.replace('check_error_', '')];
    // console.log({key, valueKey});
    if (checkFn && valueKey) {
      let initialValue;
      const checkValue = checks[key];
        // console.log({checkValue});
      switch (true) {
        case lang && !display:
          initialValue = checkValue[lang];
          break;

        case display && !lang:
          initialValue = checkValue[display];
          break;

        case lang && display:
          initialValue = checkValue[lang][display]; // ?
          break;

        default:
          initialValue = checkValue;
      }
        console.log({initialValue});
      const check = checkFn(valueKey, initialValue);
      if (!check) {
        return messages[key];
      }
    }
  }

  return '';
}

const a = createChecks(formItemChecks, formItemLabels);
// const message = validate(null, a);
// console.log(a);
/* TESTS */
// console.log('Check for empty field: ', validate(null, a));
// console.log('Check for wrong image height: ', validate({
//     height: 1000,
//     width: 600
// }, a));
console.log('Check for wrong image size: ', validate({
    height: 100,
    width: 100,
    size: 50000
}, a));
INFO