@Test public void validate_invalidLastName() { User user = getValidUser(); user.setLastName("$¨¨^invalid"); Throwable throwable = UserValidator.validate(user).blockingGet(); assertNotNull(throwable); assertTrue(throwable instanceof InvalidUserException); }
public static Completable validate(User user) { if (!isValid(user.getUsername(), USERNAME_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Username [%s] is not a valid value", user.getUsername()))); } if (!EmailValidator.isValid(user.getEmail())) { return Completable.error(new EmailFormatInvalidException(user.getEmail())); } if (!isValid(user.getFirstName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("First name [%s] is not a valid value", user.getFirstName()))); } if (!isValid(user.getLastName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Last name [%s] is not a valid value", user.getLastName()))); } if (!isValid(user.getDisplayName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Display name [%s] is not a valid value", user.getDisplayName()))); } if (!isValid(user.getNickName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Nick name [%s] is not a valid value", user.getNickName()))); } if (user.getExternalId() != null && user.getExternalId().length() > DEFAULT_MAX_LENGTH) { return Completable.error(new InvalidUserException(String.format("External id [%s] is not a valid value", user.getExternalId()))); } return Completable.complete(); }
UserValidator { public static Completable validate(User user) { if (!isValid(user.getUsername(), USERNAME_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Username [%s] is not a valid value", user.getUsername()))); } if (!EmailValidator.isValid(user.getEmail())) { return Completable.error(new EmailFormatInvalidException(user.getEmail())); } if (!isValid(user.getFirstName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("First name [%s] is not a valid value", user.getFirstName()))); } if (!isValid(user.getLastName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Last name [%s] is not a valid value", user.getLastName()))); } if (!isValid(user.getDisplayName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Display name [%s] is not a valid value", user.getDisplayName()))); } if (!isValid(user.getNickName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Nick name [%s] is not a valid value", user.getNickName()))); } if (user.getExternalId() != null && user.getExternalId().length() > DEFAULT_MAX_LENGTH) { return Completable.error(new InvalidUserException(String.format("External id [%s] is not a valid value", user.getExternalId()))); } return Completable.complete(); } }
UserValidator { public static Completable validate(User user) { if (!isValid(user.getUsername(), USERNAME_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Username [%s] is not a valid value", user.getUsername()))); } if (!EmailValidator.isValid(user.getEmail())) { return Completable.error(new EmailFormatInvalidException(user.getEmail())); } if (!isValid(user.getFirstName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("First name [%s] is not a valid value", user.getFirstName()))); } if (!isValid(user.getLastName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Last name [%s] is not a valid value", user.getLastName()))); } if (!isValid(user.getDisplayName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Display name [%s] is not a valid value", user.getDisplayName()))); } if (!isValid(user.getNickName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Nick name [%s] is not a valid value", user.getNickName()))); } if (user.getExternalId() != null && user.getExternalId().length() > DEFAULT_MAX_LENGTH) { return Completable.error(new InvalidUserException(String.format("External id [%s] is not a valid value", user.getExternalId()))); } return Completable.complete(); }  }
UserValidator { public static Completable validate(User user) { if (!isValid(user.getUsername(), USERNAME_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Username [%s] is not a valid value", user.getUsername()))); } if (!EmailValidator.isValid(user.getEmail())) { return Completable.error(new EmailFormatInvalidException(user.getEmail())); } if (!isValid(user.getFirstName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("First name [%s] is not a valid value", user.getFirstName()))); } if (!isValid(user.getLastName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Last name [%s] is not a valid value", user.getLastName()))); } if (!isValid(user.getDisplayName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Display name [%s] is not a valid value", user.getDisplayName()))); } if (!isValid(user.getNickName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Nick name [%s] is not a valid value", user.getNickName()))); } if (user.getExternalId() != null && user.getExternalId().length() > DEFAULT_MAX_LENGTH) { return Completable.error(new InvalidUserException(String.format("External id [%s] is not a valid value", user.getExternalId()))); } return Completable.complete(); }  static Completable validate(User user); }
UserValidator { public static Completable validate(User user) { if (!isValid(user.getUsername(), USERNAME_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Username [%s] is not a valid value", user.getUsername()))); } if (!EmailValidator.isValid(user.getEmail())) { return Completable.error(new EmailFormatInvalidException(user.getEmail())); } if (!isValid(user.getFirstName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("First name [%s] is not a valid value", user.getFirstName()))); } if (!isValid(user.getLastName(), NAME_STRICT_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Last name [%s] is not a valid value", user.getLastName()))); } if (!isValid(user.getDisplayName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Display name [%s] is not a valid value", user.getDisplayName()))); } if (!isValid(user.getNickName(), NAME_LAX_PATTERN_COMPILED)) { return Completable.error(new InvalidUserException(String.format("Nick name [%s] is not a valid value", user.getNickName()))); } if (user.getExternalId() != null && user.getExternalId().length() > DEFAULT_MAX_LENGTH) { return Completable.error(new InvalidUserException(String.format("External id [%s] is not a valid value", user.getExternalId()))); } return Completable.complete(); }  static Completable validate(User user); static final int DEFAULT_MAX_LENGTH; static final String NAME_STRICT_PATTERN; static final String NAME_LAX_PATTERN; static final String USERNAME_PATTERN; }