Micronaut – 不同操作的不同验证

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年5月5日 01:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定