import settings from 'settings';
import { get } from 'lodash/object';

export default (fields, values) => {
  const errors = {};

  fields && fields.map(field => {
    if (!field) {
      return;
    }

    const key = field.name;
    const value = get(values, field.name);
    const isRequired = field.html && field.html.required;
    const minLength = field.html && field.html.minLength;
    const maxLength = field.html && field.html.maxLength;

    if (isRequired && values && !value) {
      // test for required fields
      errors[key] = settings.requiredText;
    } else if (maxLength && value && value.length > maxLength) {
      // test against maxLength
      errors[key] = settings.maxLengthText.replace('%x', maxLength);
    } else if (minLength && value && value.length < minLength) {
      // test against minLength
      errors[key] = settings.minLengthText.replace('%x', minLength);
    }
  });

  return errors;
};

@@ -38,16 +38,21 @@
 rt %7B get
+, set
  %7D from 
@@ -68,16 +68,16 @@
 bject';%0A
-
 %0Aexport 
@@ -259,26 +259,19 @@
 values, 
-field.name
+key
 );%0A    c
@@ -518,37 +518,40 @@
 ields%0A      
+set(
 errors
-%5Bkey%5D =
+, key,
  settings.re
@@ -560,16 +560,17 @@
 iredText
+)
 ;%0A    %7D 
@@ -662,37 +662,40 @@
 ength%0A      
+set(
 errors
-%5Bkey%5D =
+, key,
  settings.ma
@@ -726,24 +726,25 @@
 , maxLength)
+)
 ;%0A    %7D else
@@ -840,21 +840,24 @@
     
+set(
 errors
-%5Bkey%5D =
+, key,
  set
@@ -900,16 +900,17 @@
 nLength)
+)
 ;%0A    %7D%0A

Fix validation bug