lodash#isPlainObject JavaScript Examples

The following examples show how to use lodash#isPlainObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: hash-utils.js    From ThreatMapper with Apache License 2.0 7 votes vote down vote up
export function hashDifferenceDeep(A, B) {
  // If the elements have exactly the same content, the difference is an empty object.
  // This could fail if the objects are both hashes with different permutation of keys,
  // but this case we handle below by digging in recursively.
  if (stableStringify(A) === stableStringify(B)) return {};

  // Otherwise, if either element is not a hash, always return the first element
  // unchanged as this function only takes difference of hash objects.
  if (!isPlainObject(A) || !isPlainObject(B)) return A;

  // If both elements are hashes, recursively take the difference by all keys
  const rawDiff = mapValues(A, (value, key) => hashDifferenceDeep(value, B[key]));

  // ... and filter out all the empty values.
  return omitBy(rawDiff, value => isEmpty(value));
}
Example #2
Source File: data.js    From dynamic-form-render with MIT License 6 votes vote down vote up
formatByProps = (data, props, propsMap = {}) => {
  if (
    !isObjectLike(data) ||
    !props ||
    !isPlainObject(props) ||
    !isPlainObject(propsMap)
  ) {
    return data
  }

  const map = Object.assign({}, props, propsMap)
  if (isArray(data)) {
    return data.map(item => {
      if (!isPlainObject(item)) {
        return item
      }
      let replaceObj = cloneDeep(item)
      for (const key in map) {
        if (map.hasOwnProperty(key) && key !== map[key]) {
          replaceObj[key] = item[map[key]]
        }
      }
      return replaceObj
    })
  }
  const result = cloneDeep(data)
  for (const key in map) {
    if (map.hasOwnProperty(key) && key !== map[key]) {
      result[key] = data[map[key]]
    }
  }
  return result
}
Example #3
Source File: utils.js    From jafar with MIT License 6 votes vote down vote up
export function validateField(id, model, resources) {
  return new Promise(async (resolve) => {
    const field = model.fields[id];

    // calc require term
    let { required } = field;
    if (field.requireTerm) {
      required = await evaluateTerm(id, model, resources, 'requireTerm', 'required');
    }

    // handle empty value
    if (isFieldEmpty(id, model)) {
      const errors = !required ? [] : [{
        name: 'required',
        message: hooks.emptyMessage(getEmptyMessageProps(id, model)),
      }];
      resolve({
 errors, required, empty: true, invalid: errors.length > 0,
});
      return;
    }

    // run validators when value not empty
    const errors = [];
    const asyncValidators = [];

    (field.validators || []).forEach((validator) => {
      const validatorFunc = resources.validators[validator.name].func;
      const { defaultArgs } = resources.validators[validator.name];

      const props = getFieldValidatorFuncProps(id, model, validator.name, defaultArgs);
      const result = validatorFunc(props);
      if (isPromise(result)) {
        asyncValidators.push({
 id, validatorName: validator.name, defaultArgs, promise: result,
});
      } else {
        const valid = isPlainObject(result) ? result.valid : result;
        const dynamicArgs = isPlainObject(result) ? result.args : undefined;
        if (!valid) {
          const validatorName = validator.name;
          errors.push({
            name: validatorName,
            message: getFieldErrorMessage(id, model, resources, validatorName, defaultArgs, dynamicArgs),
          });
        }
      }
    });

    Promise.all(asyncValidators.map(x => x.promise)).then((values) => {
      values.forEach((result, index) => {
        const valid = isPlainObject(result) ? result.valid : result;
        const dynamicArgs = isPlainObject(result) ? result.args : undefined;
        if (!valid) {
          const item = asyncValidators[index];
          errors.push({
            name: item.validatorName,
            message: getFieldErrorMessage(id, model, resources, item.validatorName, item.defaultArgs, dynamicArgs),
          });
        }
      });
      resolve({
 required, errors, empty: false, invalid: errors.length > 0,
});
    });
  });
}
Example #4
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// Check if fields are defined properly
function checkFieldsDefinedAsObject(model) {
  // Is fields attribute are objects
  if (!model.fields
    || !isPlainObject(model.fields)
    || Object.keys(model.fields).length === 0) {
      throwError(ERROR_PREFIX, errors.MISSING_FIELDS, { model });
  }
}
Example #5
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// check that all fields if they defined formatter / parser
// then it should be defined in resources.conversions as well
function checkConversions(model, resources) {
  ['formatter', 'parser'].forEach((conversionName) => {
    const fieldId = Object.keys(model.fields).find(id => model.fields[id][conversionName]
      && (!resources.conversions
        || !isPlainObject(resources.conversions[model.fields[id][conversionName].name])
        || !isFunction(resources.conversions[model.fields[id][conversionName].name].func)));
    if (fieldId) {
      const { name } = model.fields[fieldId][conversionName];
      throwError(ERROR_PREFIX, errors.MISSING_CONVERSION, { model, resources }, [fieldId, conversionName, name]);
    }
  });
}
Example #6
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// check that all fields if they defined dependenciesChange - it should be defined in resources.dependenciesChanges object as well
function checkFieldsDependenciesChange(model, resources) {
  const fieldId = Object.keys(model.fields).find(id => model.fields[id].dependenciesChange
    && (!resources.dependenciesChanges
      || !isPlainObject(resources.dependenciesChanges[model.fields[id].dependenciesChange.name])
      || !isFunction(resources.dependenciesChanges[model.fields[id].dependenciesChange.name].func)));
  if (fieldId) {
    const { name } = model.fields[fieldId].dependenciesChange;
    throwError(ERROR_PREFIX, errors.MISSING_DEPENDENCIES_CHANGE, { model, resources }, [fieldId, name]);
  }
}
Example #7
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// check that all fields if they defined validators - then validator.name should be defined in resources.validators as well,
// with func and message
function checkValidators(model, resources) {
  const fieldId = Object.keys(model.fields).find(id => model.fields[id].validators
    && model.fields[id].validators.find(validator => isUndefined(resources.validators)
      || !isPlainObject(resources.validators[validator.name])
      || !isFunction(resources.validators[validator.name].func)
      || !isFunction(resources.validators[validator.name].message)));
  if (fieldId) {
    const validatorName = model.fields[fieldId].validators.find(validator => isUndefined(resources.validators)
      || !isPlainObject(resources.validators[validator.name])
      || !isFunction(resources.validators[validator.name].func)
      || !isFunction(resources.validators[validator.name].message));
    throwError(ERROR_PREFIX, errors.MISSING_VALIDATOR, { model, resources }, [fieldId, validatorName]);
  }
}
Example #8
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// check that all fields if they defined terms (excludeTerm / disableTerm / requireTerm) - then term.name should be defined
// in resources.terms as well
function checkTerms(model, resources) {
  ['excludeTerm', 'disableTerm', 'requireTerm'].forEach((termName) => {
    const fieldId = Object.keys(model.fields).find(id => model.fields[id][termName]
      && getTermsFuncNames(model.fields[id][termName]).find(name => !resources.terms
        || !isPlainObject(resources.terms[name])
        || !isFunction(resources.terms[name].func)));
    if (fieldId) {
      const name = getTermsFuncNames(model.fields[fieldId][termName]).find(name => !resources.terms
        || !isFunction(resources.terms[name]));
      throwError(ERROR_PREFIX, errors.MISSING_TERM, { model, resources }, [fieldId, termName, name]);
    }
  });
}
Example #9
Source File: verification.js    From jafar with MIT License 6 votes vote down vote up
// Check if hooks are defined properly
function checkHooks(resources) {
  if (!resources.hooks || !isPlainObject(resources.hooks)) {
    throwError(ERROR_PREFIX, errors.MISSING_HOOKS, { resources });
  }
  const supportedHooks = Object.keys(hooks);
  const hook = Object.keys(resources.hooks).find(key => !supportedHooks.includes(key));
  if (hook) {
    throwError(ERROR_PREFIX, errors.INVALID_HOOK, { resources }, [hook, supportedHooks]);
  }
}
Example #10
Source File: verification.js    From jafar with MIT License 5 votes vote down vote up
// check that model.data is an object
function checkData(model) {
  if (!model.data || !isPlainObject(model.data)) {
    throwError(ERROR_PREFIX, errors.MISSING_DATA, { model });
  }
}