All files createValidationRulesFromInput.js

100% Statements 13/13
100% Branches 18/18
100% Functions 3/3
100% Lines 12/12
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      1x 22x 14x 56x 56x 11x 11x           14x     8x 6x     3x   2x        
import React from "react";
import validationRules from "./validationRules";
 
const createValidationRulesFromInput = (Element, newValidationRules = {}) => {
  if (Element && Element.props && !Element.props.children) {
    Object.keys(validationRules).forEach(rule => {
      const value = Element.props[rule] || Element.props.type === rule;
      if (value) {
        newValidationRules[Element.props.name] = (newValidationRules[Element.props.name] || []);
        newValidationRules[Element.props.name].push(
          `${rule}${typeof value !== "boolean" ? `:${value}` : ""}`,
        );
      }
    });
 
    return newValidationRules;
  }
 
  if (!Element || !Element.props) {
    return newValidationRules;
  }
 
  React.Children.forEach(Element.props.children, c => createValidationRulesFromInput(c, newValidationRules));
 
  return newValidationRules;
};
 
export default createValidationRulesFromInput;