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

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

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

问题

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

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

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

  1. @ValidateParent(parent = "parent", child = "child")
  2. public class anClass {
  3. @NotNull(groups = {FrstValidator.class, SndValidator.class})
  4. @Pattern(regexp = "^[a-zA-Z]{3}$",
  5. groups = {FrstValidator.class, SndValidator.class})
  6. String str1;
  7. @NotNull(groups = {FrstValidator.class, SndValidator.class})
  8. @Pattern(regexp = "^[a-zA-Z]{3}$",
  9. groups = {FrstValidator.class, SndValidator.class})
  10. String str2;
  11. @Pattern(regexp = "^[a-zA-Z]{10}$",
  12. groups = SndValidator.class)
  13. String child;
  14. @Pattern(regexp = "^[a-zA-Z]{10}$",
  15. groups = SndValidator.class)
  16. String parent;
  17. @Pattern(regexp = "^[a-zA-Z]{10}$",
  18. groups = SndValidator.class)
  19. String str3;
  20. }

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 -------

  1. @ValidateParent(parent = "parent ", child= "child")
  2. public class anClass{
  3. @NotNull(groups = {FrstValidator.class, SndValidator.class})
  4. @Pattern(regexp = "^[a-zA-Z]{3}$",
  5. groups = {FrstValidator.class, SndValidator.class})
  6. String str1;
  7. @NotNull(groups = {FrstValidator.class, SndValidator.class})
  8. @Pattern(regexp = "^[a-zA-Z]{3}$",
  9. groups = {FrstValidator.class, SndValidator.class})
  10. String str2;
  11. @Pattern(regexp = "^[a-zA-Z]{10}$",
  12. groups = SndValidator.class)
  13. String child;
  14. @Pattern(regexp = "^[a-zA-Z]{10}$",
  15. groups = SndValidator.class)
  16. String parent;
  17. @Pattern(regexp = "^[a-zA-Z]{10}$",
  18. groups = SndValidator.class)
  19. String str3;
  20. }

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

答案1

得分: 1

以下是翻译好的内容:

验证分组:

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

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

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

控制器:

  1. // 仅当没有提供 myParam 参数时执行此端点
  2. @PostMapping(
  3. value = "/validation",
  4. params = "!myParam")
  5. public void partialValidation(
  6. @RequestParam(defaultValue = "DEFAULT") String myParam,
  7. // 请求根据 PartialValidation 验证分组进行验证
  8. @RequestBody @Validated(PartialValidation.class) Request request) {
  9. System.out.println("部分验证");
  10. }
  11. // 仅当提供了 myParam 参数时执行此端点
  12. @PostMapping(
  13. value = "/validation",
  14. params = "myParam")
  15. public void fullValidation(
  16. @RequestParam String myParam,
  17. // 请求根据 FullValidation 验证分组进行验证
  18. @RequestBody @Validated(FullValidation.class) Request request) {
  19. System.out.println("完整验证");
  20. }
英文:

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:

  1. // Validation groups are just marker interfaces
  2. private interface PartialValidation {}
  3. private interface FullValidation {}

Request DTO:

  1. @Data
  2. private static class Request {
  3. // This field will be validated only for FullValidation validation group
  4. @NotNull(groups = FullValidation.class)
  5. String field1;
  6. // This field will be validated for both validation groups
  7. @NotNull(groups = {FullValidation.class, PartialValidation.class})
  8. String field2;
  9. }

Controller:

  1. // This endpoint is executed only if there is no myParam preprovided
  2. @PostMapping(
  3. value = "/validation",
  4. params = "!myParam")
  5. public void partialValidation(
  6. @RequestParam(defaultValue = "DEFAULT") String myParam,
  7. // Request is validated against the PartialValidation group
  8. @RequestBody @Validated(PartialValidation.class) Request request) {
  9. System.out.println("Partial validation");
  10. }
  11. // This endpoint is executed only if there is myParam preprovided
  12. @PostMapping(
  13. value = "/validation",
  14. params = "myParam")
  15. public void fullValidation(
  16. @RequestParam String myParam,
  17. // Request is validated against the FullValidation group
  18. @RequestBody @Validated(FullValidation.class) Request request) {
  19. System.out.println("Full validation");
  20. }

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:

确定