javax.validation.constraints.Email 匹配无效的电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50535214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.validation.constraints.Email matching invalid email address
提问by TheCoder
I have a User
entity having email property annotated with @Email
我有一个User
带有注释的电子邮件属性的实体@Email
@Email
private String email;
I am using @Valid
(javax.validation.Valid) annotation on my Controller class. The issue is that the controller validator is passing the invalid emails. Example:
pusp@1- obviously this is an invalid email address
pusp@fake
The pattern I noticed is, the @Email
only want sometext@text, it don't care for the extensions(.com/org etc). Is it the expected behaviour? Do I need to pass my own regex implementation for @Email(regex="")
我@Valid
在我的控制器类上使用(javax.validation.Valid) 注释。问题是控制器验证器正在传递无效的电子邮件。示例:
pusp@1- 显然这是一个无效的电子邮件地址
pusp@fake
我注意到的模式是,@Email
只想要sometext@text,它不关心扩展名(.com/org 等)。这是预期的行为吗?我是否需要通过我自己的正则表达式实现@Email(regex="")
采纳答案by davidxxx
A email without .
may be considered as valid according to the validators.
In a general way, validator implementations (here it is probably the Hibernate Validator) are not very restrictive about emails.
For example the org.hibernate.validator.internal.constraintvalidators.AbstractEmailValidator
javadoc states :
.
根据验证器,没有的电子邮件可能被认为是有效的。
一般来说,验证器实现(这里可能是 Hibernate Validator)对电子邮件的限制并不大。
例如org.hibernate.validator.internal.constraintvalidators.AbstractEmailValidator
javadoc 状态:
The specification of a valid email can be found in RFC 2822and one can come up with a regular expression matching all valid email addresses as per specification. However, as this articlediscusses it is not necessarily practical to implement a 100% compliant email validator.This implementation is a trade-off trying to match most email while ignoring for example emails with double quotes or comments.
可以在RFC 2822 中找到有效电子邮件的规范,并且可以根据规范提出与所有有效电子邮件地址匹配的正则表达式。但是,正如本文所讨论的,实施 100% 合规的电子邮件验证器不一定可行。这种实现是一种权衡,试图匹配大多数电子邮件,同时忽略例如带有双引号或注释的电子邮件。
And as a side note, I noticed similarly things with HTML Validator for emails.
作为旁注,我注意到电子邮件的 HTML Validator 也有类似的事情。
So I think that the behavior that you encounter actually is which one expected.
And about your question :
所以我认为您实际遇到的行为正是您所期望的。
关于你的问题:
Do I need to pass my own regex implementation for @Email(regex="")
我是否需要为@Email(regex="") 传递我自己的正则表达式实现
Indeed. You don't have any other choice if you want to make the validation more restrictive.
As alternative, this answercreating its own validator via a constraints composition is really interesting as it is DRY (you can reuse your custom ConstraintValidator
without specified at each time the pattern as it will be included in) and it reuses the "good part" of the @Email
ConstraintValidator
:
的确。如果您想让验证更具限制性,您别无选择。
作为替代方案,这个通过约束组合创建自己的验证器的答案非常有趣,因为它是 DRY(您可以ConstraintValidator
在每次包含模式时重用您的自定义而不指定它)并且它重用了“好的部分” @Email
ConstraintValidator
:
@Email(message="Please provide a valid email address")
@Pattern(regexp=".+@.+\..+", message="Please provide a valid email address")
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface ExtendedEmailValidator {
String message() default "Please provide a valid email address";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}