diff --git a/README.md b/README.md index 39b2152..1eba100 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ and to your project build.gradle file: ``` dependencies { - implementation 'com.github.shabab477:SimpleValidation:v1.1.0' + implementation 'com.github.shabab477:SimpleValidation:v2.0.0' } ``` diff --git a/app/src/main/java/com/shabab477/simplevalidator/AbstractVM.java b/app/src/main/java/com/shabab477/simplevalidator/AbstractVM.java index 3e7f571..f6e77b0 100644 --- a/app/src/main/java/com/shabab477/simplevalidator/AbstractVM.java +++ b/app/src/main/java/com/shabab477/simplevalidator/AbstractVM.java @@ -2,7 +2,6 @@ import android.databinding.BaseObservable; import android.databinding.Bindable; -import android.util.Log; import android.view.View; import java.util.HashMap; diff --git a/app/src/main/java/com/shabab477/simplevalidator/CommonBindingAdapter.java b/app/src/main/java/com/shabab477/simplevalidator/CommonBindingAdapter.java index 4d946a9..2590f25 100644 --- a/app/src/main/java/com/shabab477/simplevalidator/CommonBindingAdapter.java +++ b/app/src/main/java/com/shabab477/simplevalidator/CommonBindingAdapter.java @@ -9,6 +9,8 @@ public class CommonBindingAdapter { public static void setErrorMessage(EditText view, String errorMessage) { if (errorMessage != null) { view.setError(errorMessage); + } else { + view.setError(null); } } } diff --git a/app/src/main/java/com/shabab477/simplevalidator/LoginVM.java b/app/src/main/java/com/shabab477/simplevalidator/LoginVM.java index 43834df..4b7828c 100644 --- a/app/src/main/java/com/shabab477/simplevalidator/LoginVM.java +++ b/app/src/main/java/com/shabab477/simplevalidator/LoginVM.java @@ -2,6 +2,8 @@ +import android.util.Log; + import com.shabab477.simplevalidation.annotation.NotNull; import com.shabab477.simplevalidation.annotation.Size; import com.shabab477.simplevalidation.processor.ValidationProcessor; diff --git a/simplevalidation/src/main/java/com/shabab477/simplevalidation/annotation/Size.java b/simplevalidation/src/main/java/com/shabab477/simplevalidation/annotation/Size.java index 5df6659..acb7974 100644 --- a/simplevalidation/src/main/java/com/shabab477/simplevalidation/annotation/Size.java +++ b/simplevalidation/src/main/java/com/shabab477/simplevalidation/annotation/Size.java @@ -24,7 +24,7 @@ int size() default Integer.MAX_VALUE; - int min() default Integer.MAX_VALUE; + int min() default Integer.MIN_VALUE; - int max() default Integer.MIN_VALUE; + int max() default Integer.MAX_VALUE; } diff --git a/simplevalidation/src/main/java/com/shabab477/simplevalidation/processor/ValidationProcessor.java b/simplevalidation/src/main/java/com/shabab477/simplevalidation/processor/ValidationProcessor.java index 9628fb6..6e3683d 100644 --- a/simplevalidation/src/main/java/com/shabab477/simplevalidation/processor/ValidationProcessor.java +++ b/simplevalidation/src/main/java/com/shabab477/simplevalidation/processor/ValidationProcessor.java @@ -1,5 +1,6 @@ package com.shabab477.simplevalidation.processor; +import android.util.Log; import com.shabab477.simplevalidation.annotation.Email; import com.shabab477.simplevalidation.annotation.Future; @@ -25,6 +26,7 @@ public abstract class ValidationProcessor { int min = size.min(); int max = size.max(); + int sz = size.size(); fieldHolder.getField().setAccessible(true); try { @@ -32,36 +34,40 @@ public abstract class ValidationProcessor { if (Collection.class.isAssignableFrom(fieldHolder.getField().getType())) { int length = ((Collection) fieldHolder.getField().get(fieldHolder.getObject())).size(); - if (length < min || length > max) { + if (length >= min && length <= max) { - return false; + if (sz == Integer.MAX_VALUE) { + return true; + } else { + return sz == length; + } } } else if (String.class.isAssignableFrom(fieldHolder.getField().getType())) { int length = ((String) fieldHolder.getField().get(fieldHolder.getObject())).length(); - if (length < min || length > max) { - - return false; + if (length >= min && length <= max) { + if (sz == Integer.MAX_VALUE) { + return true; + } else { + return sz == length; + } } } } catch (IllegalAccessException | NullPointerException ex) { - - //ex.printStackTrace(); +// ex.printStackTrace(); return false; } finally { - fieldHolder.getField().setAccessible(false); } - return true; + return false; }; private static final Predicate nullPredicate = fieldHolder -> { fieldHolder.getField().setAccessible(true); try { - return fieldHolder.getField().get(fieldHolder.getObject()) != null; } catch (IllegalAccessException ex) { ex.printStackTrace(); @@ -78,18 +84,14 @@ public abstract class ValidationProcessor { if (fieldHolder.getField().getType().equals(java.util.Date.class)) { try { - - dateOfBirth = java.util.Date.class.cast(fieldHolder.getField().get(fieldHolder.getObject())); + dateOfBirth = java.util.Date.class.cast(fieldHolder.getField().get(fieldHolder.getObject())); } catch (IllegalAccessException e) { - e.printStackTrace(); return false; } finally { - fieldHolder.getField().setAccessible(false); } } else { - return false; } @@ -100,10 +102,9 @@ public abstract class ValidationProcessor { private static final Predicate emailPredicate = fieldHolder -> { fieldHolder.getField().setAccessible(true); - try { - String email = fieldHolder.getField().get(fieldHolder.getObject()).toString(); + if (email == null) { return false; } else { @@ -120,18 +121,14 @@ public abstract class ValidationProcessor { return compile.matcher(email).matches(); } } catch (IllegalAccessException e) { - e.printStackTrace(); return false; } finally { - fieldHolder.getField().setAccessible(false); } - }; private static String processMessage(String message, int min, int max) { - if (min == Integer.MAX_VALUE && max == Integer.MIN_VALUE) { return message == null? "Field is not valid" : message; } else if (message != null) { @@ -150,7 +147,6 @@ private static String processMessage(String message, int min, int max) { * @return A {@link Map} which has the key value pairs of the errors. The field names in {@link String} will be the key of the error in the Map and the value will be {@link String} error message */ public static HashMap validate(Object object) { - return validateProxy(object, object.getClass(), new HashMap<>()); } @@ -163,9 +159,7 @@ private static HashMap validateProxy(Object object, Class clazz, boolean hasError = false; if (field.isAnnotationPresent(Size.class)) { - if (!fieldSizePredicate.test(holder)) { - hasError = true; Size annotation = field.getAnnotation(Size.class); int max = annotation.max(); @@ -185,7 +179,6 @@ private static HashMap validateProxy(Object object, Class clazz, if (field.isAnnotationPresent(NotNull.class) && !hasError) { if (!nullPredicate.test(holder)) { - NotNull annotation = field.getAnnotation(NotNull.class); String message = annotation.message(); @@ -200,23 +193,19 @@ private static HashMap validateProxy(Object object, Class clazz, } if (field.isAnnotationPresent(Future.class) && !hasError) { - if (!dateFuturePredicate.test(holder)) { map.put(field.getName(), "Date must be in the future"); } } if (field.isAnnotationPresent(Email.class) && !hasError) { - if (!emailPredicate.test(holder)) { - - map.put(field.getName(), "Not a valid email"); + map.put(field.getName(), "Not a valid email"); } } } if (clazz.getSuperclass() != null && !Modifier.isAbstract(clazz.getSuperclass().getModifiers())) { - return validateProxy(object, clazz.getSuperclass(), map); } diff --git a/simplevalidation/src/test/java/com/shabab477/simplevalidation/ExampleUnitTest.java b/simplevalidation/src/test/java/com/shabab477/simplevalidation/ExampleUnitTest.java index c0eb06b..fdd996f 100644 --- a/simplevalidation/src/test/java/com/shabab477/simplevalidation/ExampleUnitTest.java +++ b/simplevalidation/src/test/java/com/shabab477/simplevalidation/ExampleUnitTest.java @@ -75,6 +75,6 @@ public void checkObject(){ RegistrationForm form = new CompanyRegistrationForm(); Map validate = ValidationProcessor.validate(form); - assert (validate.size() > 2); + assertEquals(2, validate.size()); } } \ No newline at end of file diff --git a/simplevalidation/src/test/java/com/shabab477/simplevalidation/TestClass.java b/simplevalidation/src/test/java/com/shabab477/simplevalidation/TestClass.java index d892ce3..ebace4b 100644 --- a/simplevalidation/src/test/java/com/shabab477/simplevalidation/TestClass.java +++ b/simplevalidation/src/test/java/com/shabab477/simplevalidation/TestClass.java @@ -8,7 +8,6 @@ public class TestClass { @Size(min = 10, max = 100, message = "Name must be within ${min} and ${max} characters") private String name; - public TestClass(String name) { this.name = name; }