Search in sources :

Example 1 with ValidationContext

use of org.summerb.validation.ValidationContext in project summerb by skarpushin.

the class AttachmentValidationStrategyImpl method validateForCreate.

@Override
public void validateForCreate(Attachment dto) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    if (ctx.validateNotEmpty(dto.getName(), Attachment.FN_NAME)) {
        ctx.validateDataLengthLessOrEqual(dto.getName(), Attachment.FN_NAME_MAXSIZE, Attachment.FN_NAME);
    }
    ctx.validateNotEmpty(dto.getArticleId(), Attachment.FN_ARTICLE_ID);
    ctx.validateNotEmpty(dto.getSize(), Attachment.FN_SIZE);
    ctx.validateNotNull(dto.getContents(), Attachment.FN_CONTENTS);
    if (ctx.getHasErrors()) {
        throw new FieldValidationException(ctx.getErrors());
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) ValidationContext(org.summerb.validation.ValidationContext)

Example 2 with ValidationContext

use of org.summerb.validation.ValidationContext in project summerb by skarpushin.

the class StringTemplateValidationUtilsTest method testValidateStringTemplate_blackbox_expectOkForValidExpression.

@Test
public void testValidateStringTemplate_blackbox_expectOkForValidExpression() throws Exception {
    StringTemplateCompiler stringTemplateCompiler = new StringTemplateCompilerlImpl();
    String stringTemplate = "Constant text plus ${vars['4444']}";
    ValidationContext ctx = new ValidationContext();
    String fieldToken = "fieldToken";
    StringTemplateValidationUtils.validateStringTemplate(stringTemplateCompiler, stringTemplate, ctx, fieldToken);
    assertEquals(false, ctx.getHasErrors());
}
Also used : StringTemplateCompilerlImpl(org.summerb.stringtemplate.impl.StringTemplateCompilerlImpl) StringTemplateCompiler(org.summerb.stringtemplate.api.StringTemplateCompiler) ValidationContext(org.summerb.validation.ValidationContext) Test(org.junit.Test)

Example 3 with ValidationContext

use of org.summerb.validation.ValidationContext in project summerb by skarpushin.

the class StringTemplateValidationUtilsTest method testValidateStringTemplate_blackbox_expectValidationErrorForWrongExpression.

@Test
public void testValidateStringTemplate_blackbox_expectValidationErrorForWrongExpression() throws Exception {
    StringTemplateCompiler stringTemplateCompiler = new StringTemplateCompilerlImpl();
    String stringTemplate = "Constant text plus ${vars['4444";
    ValidationContext ctx = new ValidationContext();
    String fieldToken = "fieldToken";
    StringTemplateValidationUtils.validateStringTemplate(stringTemplateCompiler, stringTemplate, ctx, fieldToken);
    assertEquals(true, ctx.getHasErrors());
}
Also used : StringTemplateCompilerlImpl(org.summerb.stringtemplate.impl.StringTemplateCompilerlImpl) StringTemplateCompiler(org.summerb.stringtemplate.api.StringTemplateCompiler) ValidationContext(org.summerb.validation.ValidationContext) Test(org.junit.Test)

Example 4 with ValidationContext

use of org.summerb.validation.ValidationContext in project summerb by skarpushin.

the class UserServiceImpl method validateUser.

private void validateUser(User user) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    validateEmail(user.getEmail(), ctx);
    ctx.validateDataLengthLessOrEqual(user.getDisplayName(), User.FN_DISPLAY_NAME_SIZE, User.FN_DISPLAY_NAME);
    ctx.throwIfHasErrors();
}
Also used : ValidationContext(org.summerb.validation.ValidationContext)

Example 5 with ValidationContext

use of org.summerb.validation.ValidationContext in project summerb by skarpushin.

the class UsersServiceFacadeImpl method validatePasswordChangeRequestValid.

protected User validatePasswordChangeRequestValid(String email, PasswordChange passwordChange) throws UserNotFoundException, FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    ctx.lengthEqOrGreater(passwordChange.getPassword(), 4, LoginParams.FN_PASSWORD);
    ctx.equals(passwordChange.getPassword(), SecurityMessageCodes.NEW_PASSWORD, passwordChange.getNewPasswordAgain(), SecurityMessageCodes.NEW_PASSWORD_AGAIN, PasswordChange.FN_NEW_PASSWORD_AGAIN);
    User user = null;
    if (ctx.hasText(passwordChange.getCurrentPassword(), PasswordChange.FN_CURRENT_PASSWORD)) {
        user = userService.getUserByEmail(email);
        ctx.isTrue(passwordService.isUserPasswordValid(user.getUuid(), passwordChange.getCurrentPassword()), InvalidPasswordException.ERROR_LOGIN_INVALID_PASSWORD, PasswordChange.FN_CURRENT_PASSWORD);
    }
    ctx.throwIfHasErrors();
    return user;
}
Also used : User(org.summerb.users.api.dto.User) ValidationContext(org.summerb.validation.ValidationContext)

Aggregations

ValidationContext (org.summerb.validation.ValidationContext)14 FieldValidationException (org.summerb.validation.FieldValidationException)4 Test (org.junit.Test)3 StringTemplateCompiler (org.summerb.stringtemplate.api.StringTemplateCompiler)3 StringTemplateCompilerlImpl (org.summerb.stringtemplate.impl.StringTemplateCompilerlImpl)3 User (org.summerb.users.api.dto.User)2 PSQLException (org.postgresql.util.PSQLException)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)1 DuplicateRecordValidationError (org.summerb.validation.errors.DuplicateRecordValidationError)1 UserStatus (org.summerb.webappboilerplate.security.dto.UserStatus)1 PasswordsDontMatchValidationError (org.summerb.webappboilerplate.security.ve.PasswordsDontMatchValidationError)1 RegistrationRequiredValidationError (org.summerb.webappboilerplate.security.ve.RegistrationRequiredValidationError)1