Skip to content

Commit

Permalink
Add isRequired requirement to LengthValidator
Browse files Browse the repository at this point in the history
- Update unit tests
  • Loading branch information
qiarie committed Jun 21, 2022
1 parent 3d37280 commit 3fb13a3
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 100 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
? charSequence.length() >= minLength && charSequence.length() <= maxLength
: 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,26 +8,32 @@
public class LengthValidatorTest extends BaseTest {

@Test
public void isValidShouldReturnTrueWhenEditTextValueIsEmptyAndMinimumLengthIsZero() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
public void isValidShouldReturnTrueWhenEditTextValueIsEmptyAndMinimumLengthIsZeroAndIsNotRequired() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50, false);
Assert.assertEquals(true, validator.isValid("", 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);
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 isValidShouldReturnFalseWhenEditTextValueIsLongerThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, 20);
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, 20, false);
Assert.assertEquals(false, validator.isValid("This message is longer", false));
}

@Test
public void isValidShouldReturnTrueWhenEditTextValueIsLongerThanMinimumLengthAndShorterThanMaximumLength() {
LengthValidator validator = new LengthValidator(DEFAULT_ERROR_MSG, CONSTANT_INT_0, CONSTANT_INT_50);
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)));
}

}

0 comments on commit 3fb13a3

Please sign in to comment.