user-validator


Source link: https://github.com/ShootrNetwork/user-validator

User Validator

Library to validate username, email and passwords.

Java library to validate typical user fields. Useful for validation when registering new users or logging in.

#Show me the code!!!

First, instance the validator:

UserValidator validator = UserValidator.builder().build();
 

So, how are you using it?

try {

if (validator.validateUsername(username) &&

validator.validateEmail(email) &&

validator.validatePassword(username, password)) {

 // TODO login, register or edit user 
}

}
 catch (UsernameIsNullException exception) {
 // Catch all exceptions you're interested to handle

 // TODO show something to the user!!! 
}
 

Exceptions:

  @Test (expected = InvalidEmailFormatException.class)
public void shouldThrowInvalidEmailFormatExceptionWhenEmailHasInvalidFormat() throws Exception {

  validator.validateEmail("test");

}

 @Test (expected = NullEmailException.class)
public void shouldThrowInvalidEmailFormatExceptionWhenEmailIsNull() throws Exception {

  validator.validateEmail(null);

}

 @Test (expected = InvalidPasswordException.class)
public void shouldThrowInvalidPasswordExceptionWhenInvalidPassword() throws Exception {

  validator.validatePassword("username", "username");

}

 @Test (expected = InvalidPasswordFormatException.class)
public void shouldThrowInvalidFormatPasswordExceptionWhenPasswordHasInvalidCharacters() throws Exception {

  validator.validatePassword("username", "pass word");

}

 @Test (expected = InvalidPasswordLengthException.class)
public void shouldThrowInvalidLengthPasswordExceptionPasswordIsTooLong() throws Exception {

  validator.validatePassword("username", "passwordWithMoreThanTwentyCharacters");

}

 @Test (expected = InvalidPasswordLengthException.class)
public void shouldThrowInvalidLengthPasswordExceptionPasswordIsTooShort() throws Exception {

  validator.validatePassword("username", "hi");

}

 @Test (expected = NullPasswordException.class)
public void shouldThrowNullPasswordExceptionPasswordIsNull() throws Exception {

  validator.validatePassword("username", null);

}

 @Test (expected = UsernameIsNullException.class)
public void shouldThrowUsernameIsNullExceptionWhenUsernameIsNull() throws Exception {

  validator.validateUsername(null);

}

 @Test (expected = InvalidUsernameFormatException.class)
public void shouldThrowUsernameIsNullExceptionWhenUsernameHasInvalidCharacters() throws Exception {

  validator.validateUsername("user name");

}

 @Test (expected = InvalidUsernameLengthException.class)
public void shouldThrowInvalidUsernameLengthExceptionWhenUsernameIsTooLong() throws Exception {

  validator.validateUsername("usernameLongerThan25Characters");

}

 @Test (expected = InvalidUsernameLengthException.class)
public void shouldThrowInvalidUsernameLengthExceptionWhenUsernameIsTooShort() throws Exception {

  validator.validateUsername("a");

}
 

#How are your default validations?

private static final String EMAIL_PATTERN = "[a-zA-Z0-9\\+\\._%\\-\\+]{
1,256
}
" +

"@" +

"[a-zA-Z0-9][a-zA-Z0-9\\-]{
0,64
}
" +

"(" +

"\\." +

"[a-zA-Z0-9][a-zA-Z0-9\\-]{
1,25
}
" +

")+"; private static final String USERNAME_PATTERN = "^([-_A-Za-z0-9])*$"; private static final int USERNAME_MINIMUM_LENGTH = 3; private static final int USERNAME_MAXIMUM_LENGTH = 25; private static final int PASSWORD_MINIMUM_LENGTH = 6; private static final int PASSWORD_MAXIMUM_LENGTH = 20; private static final String PASSWORD_PATTERN = "^([A-Za-z0-9_.,&%€@#~])*$"; 

#Can I customize my validations?

Sure, you can do something like this:

UserValidator.builder()

.emailPattern("here be email pattern")

.passwordMaximumLength(9)

.passwordMinimumLength(0)

.passwordPattern("here be password pattern")

.usernameMaximumLength(9)

.usernameMinimumLength(0)

.usernamePattern("here be username pattern")

.build();
 

#I want to use it!!!

  • Grab via Gradle:
compile 'com.shootr:user-validator:1.0.0'
  • Grab via Maven:
<dependency>
<groupId>com.shootr</groupId>
<artifactId>user-validator</artifactId>
<version>1.0.0</version>
<type>pom</type> </dependency>

#I want to contribute!!!

You are welcome! Please feel free to do Pull Requests or reporting issues.

#Who did this???

Arturo Jiménez

Resources

The Android Support Library for NFC.

Simplest way to get all images based on date, name, id, and other customizations.

A Magic library to take photos and select pictures in Android. In a simple way and if you need it also save the pictures in device.

A simple wrapper around most Firebase functions with RxJava.

Fluent mockito.

Gradle Plugin to check library licenses and generate license pages.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes