英文:
Micronaut - different validation for different operations
问题
我有一个类似于 JavaDTO 的结构:
public class myDTO {
private String name;
private Integer age;
}
我想在 Micronaut 中根据创建操作和更新操作执行不同的验证。在 Spring 中,你可以通过定义不同的 'groups' 来实现。参见这里的 stackoverflow 链接 或这里的 外部链接。因此,这看起来像这样:
public class myDTO {
@Null(groups = OnCreate.class)
@NotNull(groups = OnUpdate.class)
private String name;
@Null(groups = OnCreate.class)
@NotNull(groups = OnUpdate.class)
private Integer age;
}
在 Micronaut 中是否有类似的功能呢?
英文:
I have an JavaDTO like:
public class myDTO {
private String name;
private Integer age;
}
I want to do different validation in Micronaut by an CREATE operation and by an UPDATE operation. In Spring you can define different 'groups' therefore. See here stackoverflow link or here external link. So this looks like:
public class myDTO {
@Null(groups = OnCreate.class)
@NotNull(groups = OnUpdate.class)
private String name;
@Null(groups = OnCreate.class)
@NotNull(groups = OnUpdate.class)
private Integer age;
}
Is there something similiar for micronaut there?
答案1
得分: 1
我相信这不是Spring的功能,而更多是关于如何从javax
bean验证器验证bean。
您需要使用Hibernate Validator,其中javax.persistence.validation.group.pre-update
是适用的。
默认的Micronaut bean验证不使用Hibernate Validator。
尝试将Hibernate Validator添加为依赖项。
https://micronaut-projects.github.io/micronaut-hibernate-validator/latest/guide/index.html
英文:
I believe this is not Spring functionality but more how beans are validated from the javax
bean validator.
You need to use Hibernate Validator where javax.persistence.validation.group.pre-update
are applicable.
Default Micronaut bean validation is not using Hibernate Validator.
Try to add hibernate validator as dependency.
https://micronaut-projects.github.io/micronaut-hibernate-validator/latest/guide/index.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论