Skip to content

Commit

Permalink
Merge pull request #635 from opensrp/634-fix-lengthvalidator
Browse files Browse the repository at this point in the history
Remove Empty Value Check for LengthValidator
  • Loading branch information
qiarie authored Jun 21, 2022
2 parents 96b4667 + 3fb13a3 commit 4994735
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vijay.jsonwizard.validators.edittext;

import android.support.annotation.NonNull;

import com.rengwuxian.materialedittext.validation.METValidator;

/**
Expand All @@ -9,17 +11,22 @@ public class LengthValidator extends METValidator {

protected int minLength = 0;
protected int maxLength = Integer.MAX_VALUE;
protected boolean isRequired;

public LengthValidator(String errorMessage, int minLength, int maxLength) {
public LengthValidator(String errorMessage, int minLength, int maxLength, boolean isRequired) {
super(errorMessage);
this.minLength = minLength;
this.maxLength = maxLength;
this.isRequired = isRequired;
}

@Override
public boolean isValid(CharSequence charSequence, boolean isEmpty) {
return charSequence != null ? !isEmpty && charSequence.length() >= minLength && charSequence.length() <= maxLength && charSequence != null
: false;

public boolean isValid(@NonNull CharSequence charSequence, boolean isEmpty) {
if (isRequired && isEmpty)
return false;
else if (!isRequired && isEmpty)
return true;
else
return (charSequence != null && charSequence.length() >= minLength && charSequence.length() <= maxLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class MaxLengthValidator extends LengthValidator {

public MaxLengthValidator(String errorMessage, int maxLength) {
super(errorMessage, EditTextFactory.MIN_LENGTH, maxLength);
public MaxLengthValidator(String errorMessage, int maxLength, boolean isRequired) {
super(errorMessage, EditTextFactory.MIN_LENGTH, maxLength, isRequired);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class MinLengthValidator extends LengthValidator {

public MinLengthValidator(String errorMessage, int minLength) {
super(errorMessage, minLength, EditTextFactory.MAX_LENGTH);
public MinLengthValidator(String errorMessage, int minLength, boolean isRequired) {
super(errorMessage, minLength, EditTextFactory.MAX_LENGTH, isRequired);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@
public class LengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnFalseWhenIsEmptyTrue() {
public void isValidShouldReturnTrueWhenEditTextValueIsEmptyAndMinimumLengthIsZeroAndIsNotRequired() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid("", true));
}

LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, true));
@Test
public void isValidShouldReturnFalseWhenEditTextValueIsEmptyAndIsRequired() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, true);
Assert.assertEquals(false, validator.isValid("", true));
}

@Test
public void isValidShouldReturnTrueWhenEditTextValueIsNotEmptyAndIsLowerThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));
}

@Test
public void isValidShouldReturnTrueWhenIsEmptyFalse() {
public void isValidShouldReturnFalseWhenEditTextValueIsLongerThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, 20, false);
Assert.assertEquals(false, validator.isValid("This message is longer", false));
}

LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
@Test
public void isValidShouldReturnTrueWhenEditTextValueIsLongerThanMinimumLengthAndShorterThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
import org.junit.Test;

public class MaxLengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnFalseWhenStringLengthGreaterThanMaxSetValue() {

MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthLessThanMaxSetValue() {

MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthEqualToMaxSetValue() {
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenTheStringIsNull() {
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MaxLengthValidator validator = new MaxLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, (validator.isValid(null, false)));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
import org.junit.Test;

public class MinLengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnTrueWhenStringLengthIsGreaterThanMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnFalseWhenStringLengthIsLessThanMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_50, false);
Assert.assertEquals(false, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenStringLengthIsEqualToMinSetValue() {

MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_16, false);
Assert.assertEquals(true, validator.isValid(DEFAULT_TEST_MESSAGE, false));

}

@Test
public void isValidShouldReturnTrueWhenTheStringIsNull() {
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1);
MinLengthValidator validator = new MinLengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_1, false);
Assert.assertEquals(false, (validator.isValid(null, false)));
}

}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=2.1.17-SNAPSHOT
VERSION_NAME=2.1.18-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down

0 comments on commit 4994735

Please sign in to comment.