Skip to content

Support for groups used for validation annotations is missing #1374

Closed

Description

@Zeratoxx

I am using validator groups for complex objects.

Given the following DTOs / Classes:

AdvertisementDTO

public class AdvertisementDTO {
    public enum Type {
        OFFER {
            public String toString() {
                return "OFFER";
            }
        },
        REQUEST {
            public String toString() {
                return "REQUEST";
            }
        }
    }

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @NotNull(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement type cannot be null")
    private Type type;

    @Valid
    @ConvertGroup(from = ValidatorGroup.Create.class, to = ValidatorGroup.CreateSecondLevel.class)
    @NotNull(
            groups = {
                ValidatorGroup.CreateSecondLevel.class,
                Default.class,
                ValidatorGroup.Create.class
            },
            message = "Advertisement category cannot be null")
    private CategoryDTO category;

    @Valid
    @ConvertGroup(from = ValidatorGroup.Create.class, to = ValidatorGroup.CreateSecondLevel.class)
    @NotNull(
            groups = {
                ValidatorGroup.CreateSecondLevel.class,
                Default.class,
                ValidatorGroup.Create.class
            },
            message = "Advertisement user cannot be null")
    private UserDTO user;

    @NotBlank(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement title is mandatory")
    private String title;

    @NotBlank(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement title is mandatory")
    private String description;

    @Min(
            groups = {ValidatorGroup.Create.class, Default.class},
            value = 0)
    private Integer price;

    private String location;

    private Date created;
}

UserDTO

public class UserDTO {

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @Email(
            groups = ValidatorGroup.Create.class,
            regexp = "(\\w)+@*.*",
            flags = {Pattern.Flag.CASE_INSENSITIVE, Pattern.Flag.UNICODE_CASE})
    @NotBlank(groups = ValidatorGroup.Create.class, message = "User email is mandatory")
    private String email;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User password is mandatory")
    @Length(groups = ValidatorGroup.Create.class, min = 6)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User firstName is mandatory")
    @Length(groups = ValidatorGroup.Create.class, max = 255)
    private String firstName;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User lastName is mandatory")
    @Length(groups = ValidatorGroup.Create.class, max = 255)
    private String lastName;

    private String phone;
    private String location;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Date created;
}

CategoryDTO

public class CategoryDTO {

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @NotBlank(
            groups = {ValidatorGroup.Create.class},
            message = "Category name cannot be null or empty")
    private String name;

    private CategoryDTO parentCategory;

    public static CategoryDTO toCategoryDTO(Category entity) {
        CategoryMapper mapper = Mappers.getMapper(CategoryMapper.class);
        return mapper.toCategoryDTO(entity);
    }

    public static Category toCategoryEntity(CategoryDTO dto) {
        CategoryMapper mapper = Mappers.getMapper(CategoryMapper.class);
        return mapper.toCategoryEntity(dto);
    }
}

ValidatorGroup

public interface ValidatorGroup {
    interface Create {}

    interface CreateSecondLevel {}
}

JSON Payloads

POST newAdvertisement

In POST newAdvertisement, there needs to be a JSON payload like that:

{
"type":"OFFER",
"category": {
"id":5
},
"user": {
"id":4711
},
"title":"4 room apartment for rent",
"description":"It is a apartment with 4 rooms near a lake.",
"price":400,
"location":"7th Ave, NYC"
}

As you can see, for category and user, only the id is needed but the id of the Advertisement itself is missing (it gets generated by JPA).

POST newUser

On the other hand, if you want to create a new User, the JSON payload looks like that:

{
"email":"[email protected]",
"password":"secret",
"firstName":"David",
"lastName":"Herold",
"phone":"069-123456",
"location":"NYC"
}

All informations except the id.

Conclusion

In the current release, the groups are ignored.

Activity

  1. changed the title [-]Support for groups used for validation annotations[/-] [+]Support for groups used for validation annotations is missing[/+] on Dec 10, 2021
  2. bnasslahsen commented on Dec 11, 2021

    @bnasslahsen
    Collaborator

    @Zeratoxx,

    There is no plans for this support.
    Feel free to propose a PR if you feel it useful for the community, and we will have a look at it.

  3. added
    question
    on Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions