我如何指定 Hibernate“@Pattern”使用 .properties 文件或数据库中的正则表达式进行注释

发布于 12-28 16:19 字数 1646 浏览 4 评论 0原文

情况:我想根据用户属性执行 Hibernate 验证(以允许根据用户的帐户数据输入不同的验证规则) - 我认为必须可以使用 .properties 文件来指定一个特定的正则表达式,但我无法弄清楚出了什么问题:

我当前指定验证正则表达式的方法从特定接口文件中的常量中提取该正则表达式(以将所有内容放在一起)并将其作为常量插入每个变量的 @Pattern() 注释 - 例如变量 workPhone:

@Column(name = "WORK_PHONE")
@NotEmpty(message = "{ContactInfo.workPhone.notEmpty}")
@Pattern(regexp = PHONE_NUMBER_PATTERN_SL, message = "{ContactInfo.workPhone.regexp.msg}")
@Size(max = 10, message = "{ContactInfo.workPhone.size}")
protected String                workPhone;

...其中正则表达式存储在 static Final String < code>PHONE_NUMBER_PATTERN_SL 和所有 {ContactInfo.workPhone...} 调用都来自 .properties 文件:

ContactInfo.workPhone.notEmpty=Please enter your phone number.
ContactInfo.workPhone.regexp.msg=Invalid characters entered in phone. Use this format XXX-XXX-XXXX.
ContactInfo.workPhone.size=Phone can not be longer than 10 digits.

不幸的是,这种安排使验证模式在应用程序范围内(编译),正如我所言无法找到一种方法来为不同公司、地点、就业职位等中的不同用户更改它。为了能够根据此信息进行区分,我还想将正则表达式存储在属性文件中,我尝试以这种方式包含它:

ContactInfo.workPhone.regexp=\d{3}-\d{3}-\d{4}

同时在第一个代码清单的第三行的注释中包含引用:

@Pattern(regexp = "{ContactInfo.workPhone.regexp}", message = "{ContactInfo.workPhone.regexp.msg}")

然后我会针对不同的场合切换属性文件,例如允许/要求非美国电话号码格式。

问题:是否可以做我想做的事情?是否有更好的方法来指定模式(甚至可能允许数据库调用而不是属性文件)?

此外,我在这方面并不是最擅长的(因为我是从另一位开发人员那里接手的),所以如果有人只能向我指出有关使用 @Pattern 注释或其他 Hibernate 正则表达式验证标记的重点资源,那么可能会给我所有我需要的信息。

TL;DR:是否可以对 Hibernate 模式验证中使用的表达式使用动态设置或修改的值,而不是预定义和预编译的常量?

Situation: I would like to perform Hibernate Validation based upon user properties (to allow different validation rules for input based upon a user's account data) - I think it must be possible to use a .properties file to specify a particular regular expression, but I can't figure out what is wrong:

My current method of specifying a validation regex pulls that regex from a constant in a particular interface file (to keep everything together) and plugs it in as a constant in an @Pattern() annotation for each variable - e.g. for the variable workPhone:

@Column(name = "WORK_PHONE")
@NotEmpty(message = "{ContactInfo.workPhone.notEmpty}")
@Pattern(regexp = PHONE_NUMBER_PATTERN_SL, message = "{ContactInfo.workPhone.regexp.msg}")
@Size(max = 10, message = "{ContactInfo.workPhone.size}")
protected String                workPhone;

...where the regex is stored in the static final String PHONE_NUMBER_PATTERN_SL and all the {ContactInfo.workPhone...} calls come from a .properties file:

ContactInfo.workPhone.notEmpty=Please enter your phone number.
ContactInfo.workPhone.regexp.msg=Invalid characters entered in phone. Use this format XXX-XXX-XXXX.
ContactInfo.workPhone.size=Phone can not be longer than 10 digits.

Unfortunately, this arrangement makes the validation pattern application-wide (compiled), as I can't figure a way to change it for a different user in a different company, location, employment position, etc. To make it possible to differentiate based upon this info, I'd like to also store the regex in the properties file, and I try to include it this way:

ContactInfo.workPhone.regexp=\d{3}-\d{3}-\d{4}

while including the reference in the annotation from the third line in the first code listing:

@Pattern(regexp = "{ContactInfo.workPhone.regexp}", message = "{ContactInfo.workPhone.regexp.msg}")

I would then switch out the properties files for different occasions, such as to allow/require a non-U.S. telephone number format.

Question: Is it possible to do what I want to do? Is there a better way to specify the pattern (which might allow even a database call instead of a properties file)?

Additionally, I'm not the best at this (as I'm taking over from another developer), so if someone could merely point me to a focused resource concerning the use of the @Pattern annotation or other Hibernate regex validation markup, then that might give me all the info I need.

TL;DR: Is it possible to use a dynamically-set or modified value for the expression used in Hibernate Pattern Validation rather than a predefined and precompiled constant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍2025-01-04 16:19:42

在注释中,您只能引用常量表达式,因此从属性文件或数据库加载值在这里不起作用。

您可以使用 API 用于 Hibernate Validator 4.2 中引入的动态约束声明,它允许在运行时定义约束。您的示例可能如下所示:

String dynamicPattern = ...;

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( ContactInfo.class )
    .property( "workPhone", FIELD )
    .constraint( new PatternDef().regexp( dynamicPattern ) );

HibernateValidatorConfiguration config = 
    Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );

Validator validator = config.buildValidatorFactory().getValidator();

Within annotations you can only refer to constant expressions, so loading values from a property file or database wouldn't work here.

You could use the API for dynamic constraint declaration introduced in Hibernate Validator 4.2 which allows to define constraints at runtime. Your example might look like that:

String dynamicPattern = ...;

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( ContactInfo.class )
    .property( "workPhone", FIELD )
    .constraint( new PatternDef().regexp( dynamicPattern ) );

HibernateValidatorConfiguration config = 
    Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );

Validator validator = config.buildValidatorFactory().getValidator();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文