Search in sources :

Example 1 with Email

use of jakarta.validation.constraints.Email in project jsonschema-generator by victools.

the class JakartaValidationModule method resolveStringPattern.

/**
 * Determine a given text type's pattern.
 *
 * @param member the field or method to check
 * @return specified pattern (or null)
 * @see Pattern
 */
protected String resolveStringPattern(MemberScope<?, ?> member) {
    if (member.getType().isInstanceOf(CharSequence.class)) {
        Pattern patternAnnotation = this.getAnnotationFromFieldOrGetter(member, Pattern.class, Pattern::groups);
        if (patternAnnotation != null) {
            // @Pattern annotation was found, return its (mandatory) regular expression
            return patternAnnotation.regexp();
        }
        Email emailAnnotation = this.getAnnotationFromFieldOrGetter(member, Email.class, Email::groups);
        if (emailAnnotation != null && !".*".equals(emailAnnotation.regexp())) {
            // non-default regular expression on @Email annotation should also be considered
            return emailAnnotation.regexp();
        }
    }
    return null;
}
Also used : Pattern(jakarta.validation.constraints.Pattern) Email(jakarta.validation.constraints.Email)

Example 2 with Email

use of jakarta.validation.constraints.Email in project hibernate-validator by hibernate.

the class PredefinedScopeValidatorFactoryTest method testUnavailableInitializedLocale.

@Test
public void testUnavailableInitializedLocale() {
    Locale defaultLocale = Locale.getDefault();
    try {
        Locale georgianLocale = new Locale("ka", "GE");
        Locale.setDefault(georgianLocale);
        Validator validator = getValidatorWithInitializedLocale(georgianLocale);
        Set<ConstraintViolation<Bean>> violations = validator.validate(new Bean("", "invalid"));
        assertThat(violations).containsOnlyViolations(violationOf(Email.class).withProperty("email").withMessage("must be a well-formed email address"));
    } finally {
        Locale.setDefault(defaultLocale);
    }
}
Also used : Locale(java.util.Locale) Email(jakarta.validation.constraints.Email) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) Test(org.testng.annotations.Test)

Example 3 with Email

use of jakarta.validation.constraints.Email in project hibernate-validator by hibernate.

the class PredefinedScopeValidatorFactoryTest method testUninitializedLocale.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000250:.*")
public void testUninitializedLocale() {
    Locale defaultLocale = Locale.getDefault();
    try {
        Locale.setDefault(Locale.FRANCE);
        ValidatorFactory validatorFactory = getValidatorFactoryWithInitializedLocale(Locale.ENGLISH);
        Validator validator = validatorFactory.usingContext().messageInterpolator(new MessageInterpolator() {

            @Override
            public String interpolate(String messageTemplate, Context context, Locale locale) {
                return validatorFactory.getMessageInterpolator().interpolate(messageTemplate, context, locale);
            }

            @Override
            public String interpolate(String messageTemplate, Context context) {
                return validatorFactory.getMessageInterpolator().interpolate(messageTemplate, context, Locale.GERMAN);
            }
        }).getValidator();
        Set<ConstraintViolation<Bean>> violations = validator.validate(new Bean("", "invalid"));
        assertThat(violations).containsOnlyViolations(violationOf(Email.class).withProperty("email").withMessage("doit être une adresse électronique syntaxiquement correcte"));
    } finally {
        Locale.setDefault(defaultLocale);
    }
}
Also used : Locale(java.util.Locale) Email(jakarta.validation.constraints.Email) ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 4 with Email

use of jakarta.validation.constraints.Email in project hibernate-validator by hibernate.

the class PredefinedScopeValidatorFactoryTest method testExistingInitializedLocale.

@Test
public void testExistingInitializedLocale() {
    Locale defaultLocale = Locale.getDefault();
    try {
        Locale.setDefault(Locale.FRANCE);
        Validator validator = getValidatorWithInitializedLocale(Locale.FRANCE);
        Set<ConstraintViolation<Bean>> violations = validator.validate(new Bean("", "invalid"));
        assertThat(violations).containsOnlyViolations(violationOf(Email.class).withProperty("email").withMessage("doit être une adresse électronique syntaxiquement correcte"));
    } finally {
        Locale.setDefault(defaultLocale);
    }
}
Also used : Locale(java.util.Locale) Email(jakarta.validation.constraints.Email) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) Test(org.testng.annotations.Test)

Aggregations

Email (jakarta.validation.constraints.Email)4 ConstraintViolation (jakarta.validation.ConstraintViolation)3 Validator (jakarta.validation.Validator)3 Locale (java.util.Locale)3 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)3 Test (org.testng.annotations.Test)3 MessageInterpolator (jakarta.validation.MessageInterpolator)1 ValidatorFactory (jakarta.validation.ValidatorFactory)1 Pattern (jakarta.validation.constraints.Pattern)1