我们可以根据请求参数向请求主体添加验证。

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

Can we add validation to the request body based on the request Parameter

问题

我有一个REST端点,它接收请求体中的对象和请求参数。当传递请求参数时,我需要验证对象的所有属性,如果没有,则参数采用分配的默认值,只需要验证2个属性。两种情况的路径都需要相同。如何实现这一点?

目前,我对对象进行模式、长度和可能值的检查,借助注释进行验证。

----- 更新类 -------

@ValidateParent(parent = "parent", child = "child")
public class anClass {

    @NotNull(groups = {FrstValidator.class, SndValidator.class})
    @Pattern(regexp = "^[a-zA-Z]{3}$",
            groups = {FrstValidator.class, SndValidator.class})
    String str1;

    @NotNull(groups = {FrstValidator.class, SndValidator.class})
    @Pattern(regexp = "^[a-zA-Z]{3}$",
            groups = {FrstValidator.class, SndValidator.class})
    String str2;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups = SndValidator.class)
    String child;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups = SndValidator.class)
    String parent;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups = SndValidator.class)
    String str3;

}

ValidateParent会检查在请求体中传递child时是否也传递了parent。

英文:

I have an REST endpoint, which takes an object in the request body and a request parameter. When the request parameter is passed, I need to validate all the attributes of the object, if not the parameter
takes the default value assigned and only 2 attributes needs to be validated. The path needs to be same for both the case. How can this be achieved?

Currently I have pattern, length and possibles values check for the objects, validated with the help of annotations.

----- Updating the Class -------

@ValidateParent(parent = "parent ", child= "child")
public class anClass{

    @NotNull(groups = {FrstValidator.class, SndValidator.class})
    @Pattern(regexp = "^[a-zA-Z]{3}$",
            groups = {FrstValidator.class, SndValidator.class})
    String str1;

    @NotNull(groups = {FrstValidator.class, SndValidator.class})
    @Pattern(regexp = "^[a-zA-Z]{3}$",
            groups = {FrstValidator.class, SndValidator.class})
    String str2;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups =  SndValidator.class)
    String child;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups = SndValidator.class)
    String parent;

    @Pattern(regexp = "^[a-zA-Z]{10}$",
            groups = SndValidator.class)
    String str3;


}

ValidateParent, checks if the parent is also passed when the child is passed in the request body.

答案1

得分: 1

以下是翻译好的内容:

验证分组:

// 验证分组仅为标记接口
private interface PartialValidation {}
private interface FullValidation {}

请求数据传输对象(DTO):

@Data
private static class Request {
    // 该字段仅在 FullValidation 验证分组下进行验证
    @NotNull(groups = FullValidation.class)
    String field1;
    // 该字段在两个验证分组下进行验证
    @NotNull(groups = {FullValidation.class, PartialValidation.class})
    String field2;
}

控制器:

// 仅当没有提供 myParam 参数时执行此端点
@PostMapping(
    value = "/validation",
    params = "!myParam")
public void partialValidation(
    @RequestParam(defaultValue = "DEFAULT") String myParam,
    // 请求根据 PartialValidation 验证分组进行验证
    @RequestBody @Validated(PartialValidation.class) Request request) {

    System.out.println("部分验证");
}

// 仅当提供了 myParam 参数时执行此端点
@PostMapping(
    value = "/validation",
    params = "myParam")
public void fullValidation(
    @RequestParam String myParam,
    // 请求根据 FullValidation 验证分组进行验证
    @RequestBody @Validated(FullValidation.class) Request request) {

    System.out.println("完整验证");
}
英文:

You can achieve your goal in an elegant "Spring way", using validation groups and two separate endpoints, distinguished by presence of the request parameter:

Validation groups:

// Validation groups are just marker interfaces
private interface PartialValidation {}
private interface FullValidation {}

Request DTO:

@Data
private static class Request {
    // This field will be validated only for FullValidation validation group
    @NotNull(groups = FullValidation.class)
    String field1;
    // This field will be validated for both validation groups
    @NotNull(groups = {FullValidation.class, PartialValidation.class})
    String field2;
}

Controller:

// This endpoint is executed only if there is no myParam preprovided
@PostMapping(
    value = "/validation",
    params = "!myParam")
public void partialValidation(
    @RequestParam(defaultValue = "DEFAULT") String myParam,
    // Request is validated against the PartialValidation group
    @RequestBody @Validated(PartialValidation.class) Request request) {

    System.out.println("Partial validation");
}

// This endpoint is executed only if there is myParam preprovided
@PostMapping(
    value = "/validation",
    params = "myParam")
public void fullValidation(
    @RequestParam String myParam,
    // Request is validated against the FullValidation group
    @RequestBody @Validated(FullValidation.class) Request request) {

    System.out.println("Full validation");
}

huangapple
  • 本文由 发表于 2020年10月17日 04:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64396227.html
匿名

发表评论

匿名网友

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

确定