Skip to content

Commit

Permalink
Merge pull request #29 from WellingtonCosta/feat/new_validations
Browse files Browse the repository at this point in the history
Feat/new validations
  • Loading branch information
wellingtoncosta authored Apr 25, 2018
2 parents a2bc9c7 + ec3f59c commit c5f6ae7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ allprojects {

```groovy
dependencies {
 implementation 'com.github.WellingtonCosta.convalida:convalida:2.0.2'
 annotationProcessor 'com.github.WellingtonCosta.convalida:convalida-compiler:2.0.2'
 implementation 'com.github.WellingtonCosta.convalida:convalida:2.0.3'
 annotationProcessor 'com.github.WellingtonCosta.convalida:convalida-compiler:2.0.3'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.github.ben-manes.versions'

version = '2.0.2'
version = '2.0.3'

buildscript {
ext.versions = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public CpfValidator(EditText editText, String errorMessage, boolean autoDismiss)

@Override
public boolean isNotValid(String value) {
if(value.isEmpty() || value.length() < 11) return true;
boolean hasOnlyDigits = value.matches("\\d{11}");
boolean isNotInBlackList = !inBlackList(value);
int charAt9Position = Character.getNumericValue(value.charAt(9));
int charAt10Position = Character.getNumericValue(value.charAt(10));
boolean digit9IsValid = cpfDv(value, 1) == charAt9Position;
boolean digit10IsValid = cpfDv(value, 2) == charAt10Position;
String cpfWithoutSpecialChars = value.replace(".", "").replace("-", "");
if(cpfWithoutSpecialChars.isEmpty() || cpfWithoutSpecialChars.length() < 11) return true;
boolean hasOnlyDigits = cpfWithoutSpecialChars.matches("\\d{11}");
boolean isNotInBlackList = !inBlackList(cpfWithoutSpecialChars);
int charAt9Position = Character.getNumericValue(cpfWithoutSpecialChars.charAt(9));
int charAt10Position = Character.getNumericValue(cpfWithoutSpecialChars.charAt(10));
boolean digit9IsValid = cpfDv(cpfWithoutSpecialChars, 1) == charAt9Position;
boolean digit10IsValid = cpfDv(cpfWithoutSpecialChars, 2) == charAt10Position;
return !(hasOnlyDigits && isNotInBlackList && digit9IsValid && digit10IsValid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class CpfValidatorTest extends BaseTest {
assertEquals(validator.validate(), true);
}

@Test public void validCpfWithSpecialChars() {
CpfValidator validator = new CpfValidator(mockEditText, errorMessage, true);
when(mockEditText.getText().toString()).thenReturn("324.544.010-37");
assertEquals(validator.validate(), true);
}

@Test public void invalidCpfWithEmptyValue() {
CpfValidator validator = new CpfValidator(mockEditText, errorMessage, true);
when(mockEditText.getText().toString()).thenReturn("");
Expand Down
1 change: 0 additions & 1 deletion sample/src/main/res/layout/activity_annotation_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
android:layout_height="wrap_content"
android:hint="@string/cpf"
android:inputType="text"
android:maxLength="11"
android:maxLines="1" />

</android.support.design.widget.TextInputLayout>
Expand Down
1 change: 0 additions & 1 deletion sample/src/main/res/layout/activity_databinding_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@
android:layout_height="wrap_content"
android:hint="@string/cpf"
android:inputType="text"
android:maxLength="11"
android:maxLines="1"
app:cpfValidationErrorMessage="@{@string/cpf_not_valid}" />

Expand Down

0 comments on commit c5f6ae7

Please sign in to comment.