Skip to content

Commit

Permalink
Updated readme and fixed some validation bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shabab477 committed Mar 10, 2019
1 parent 63a4870 commit 1ce4be3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.util.Log;
import android.view.View;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/shabab477/simplevalidator/LoginVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,43 +26,48 @@ public abstract class ValidationProcessor {

int min = size.min();
int max = size.max();
int sz = size.size();
fieldHolder.getField().setAccessible(true);

try {

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<FieldHolder> nullPredicate = fieldHolder -> {
fieldHolder.getField().setAccessible(true);

try {

return fieldHolder.getField().get(fieldHolder.getObject()) != null;
} catch (IllegalAccessException ex) {
ex.printStackTrace();
Expand All @@ -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;
}

Expand All @@ -100,10 +102,9 @@ public abstract class ValidationProcessor {

private static final Predicate<FieldHolder> emailPredicate = fieldHolder -> {
fieldHolder.getField().setAccessible(true);

try {

String email = fieldHolder.getField().get(fieldHolder.getObject()).toString();

if (email == null) {
return false;
} else {
Expand All @@ -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) {
Expand All @@ -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<String, String> validate(Object object) {

return validateProxy(object, object.getClass(), new HashMap<>());
}

Expand All @@ -163,9 +159,7 @@ private static HashMap<String, String> 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();
Expand All @@ -185,7 +179,6 @@ private static HashMap<String, String> 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();

Expand All @@ -200,23 +193,19 @@ private static HashMap<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public void checkObject(){
RegistrationForm form = new CompanyRegistrationForm();
Map<String, String> validate = ValidationProcessor.validate(form);

assert (validate.size() > 2);
assertEquals(2, validate.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 1ce4be3

Please sign in to comment.