All files / src/helpers veasyClassUtils.js

100% Statements 11/11
100% Branches 8/8
100% Functions 4/4
100% Lines 11/11

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                  59x                   18x                     20x 20x   20x 4x         16x 4x     12x 19x 2x            
// @flow
 
import is from 'is_js';
import type {ComponentState, Schema} from "../flowTypes";
 
/**
 * Check if an object is a non-empty object
 */
export function isNonEmptyObject(obj: mixed): Boolean {
  return is.object(obj) && is.not.empty(obj);
}
 
/**
 * Return error message for checking the parameters of the constructor.
 */
export function getConstructorErrorMessage(
  paramName: string,
  value: mixed
) {
  return `[Veasy - ${paramName}] Expect: non empty object. Actual: ${String(value)}`;
}
 
/**
 * Type check for 2 parameters of the constructor
 *
 */
export function typeCheck(
  component: ComponentState,
  schema: Schema
) {
  const isComponentValid = isNonEmptyObject(component);
  const isSchemaValid = isNonEmptyObject(schema);
 
  if (!isComponentValid) {
    throw new Error(
      getConstructorErrorMessage('Parameter component', component)
    );
  }
 
  if (!isSchemaValid) {
    throw new Error(getConstructorErrorMessage('Parameter schema', schema));
  }
 
  Object.keys(schema).forEach(prop => {
    if (!isNonEmptyObject(schema[prop])) {
      throw new Error(
        getConstructorErrorMessage(`schema.${prop}`, schema[prop])
      );
    }
  });
}